Skip to content

Allow Closures to reuse their run_time_cache when opcache is enabled - #18556

Draft
arnaud-lb wants to merge 3 commits into
php:masterfrom
arnaud-lb:gh10782
Draft

arnaud-lb wants to merge 3 commits into
php:masterfrom
arnaud-lb:gh10782

Conversation

@arnaud-lb

Copy link
Copy Markdown
Member

Possible alternative fix for GH-10782

This implements the approach described in #10782 (comment):

may be storing "scope" in the first slot of the closure's run_time_cache

Unfortunately this results in a small regression on the Symfony benchmark (0.20% wall time, 0.03% icount).

@bwoebi bwoebi left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not only is it a CPU time regression, but it also leaks memory on the arena (it's just not reported as a leak, because arena).
Every time the scope changes (e.g. an inherited function called on different extending objects), a new run time cache is allocated.

If you want to meaningfully do this, you'll have to store the closures run_time_cache in e.g. class_mutable_data of the scope they belong to.

Otherwise, I think Dmitrys suggestion was to do this just once. I.e. store the scope for the first time the runtime cache is allocated (i.e. when ZEND_MAP_PTR_GET() is NULL) and otherwise heap allocate.
This would still cause repeated observer initializations, but make it consistent between opcache and non-opcache.

@arnaud-lb

arnaud-lb commented May 14, 2025 •

Copy link
Copy Markdown
Member Author

Yeah, I planned to analyze the regression and check for mistakes, because I didn't expected that.

My goal was only to have a similar reuse rate with opcache than without, but indeed there may be ways to improve that even more.

it also leaks memory on the arena [...]. Every time the scope changes (e.g. an inherited function called on different extending objects), a new run time cache is allocated.

I'm not sure what you mean here. Do you have an example where the scope of a closure changes? (without binding it explicitly). All I can think of is traits, but even in that case I allocate on the arena only once.

With the current changes, I never allocate on the arena more than once per Closure.

Otherwise, I think Dmitrys suggestion was to do this just once. I.e. store the scope for the first time the runtime cache is allocated (i.e. when ZEND_MAP_PTR_GET() is NULL) and otherwise heap allocate.

That's what I was targeting, but it's possible I've made mistakes that prevent this.

@bwoebi

bwoebi commented May 14, 2025

Copy link
Copy Markdown
Member
			} else if (!ptr
			 && func->common.fn_flags & ZEND_ACC_CLOSURE) {

I see what I missed - the !ptr and the closure check are on two different lines and missed that they belong to the same condition.

@ArtUkrainskiy ArtUkrainskiy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds to a void *, which is a GNU extension. I don't think MSVC accepts it.

Comment thread Zend/zend_closures.c
* and remember which scope it is for. The scope is stored in
* an extra slot before the run_time_cache. */
size_t extra_slot_size = sizeof(void*);
ptr = zend_arena_alloc(&CG(arena), func->op_array.cache_size + extra_slot_size) + extra_slot_size;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ptr = zend_arena_alloc(&CG(arena), func->op_array.cache_size + extra_slot_size) + extra_slot_size;
ptr = (void **) zend_arena_alloc(&CG(arena), func->op_array.cache_size + extra_slot_size) + 1;

@ArtUkrainskiy

Copy link
Copy Markdown
Contributor

I measured this on Laravel 13 and Symfony Demo, with the PR ported onto master (0f55811b). Without OpenTelemetry, private run-time cache allocations on Laravel's / page drop from 260 to 4, with 288 closures created per request. In a Laravel worker loop with OpenTelemetry, observer inits drop from 187 to 4.3 per request.

These runs used a Ryzen 9 5950X, with OPcache on and JIT off. The table shows median changes in cycles per request against master: 10 alternating passes for the pages, 8 for the worker. Negative means fewer cycles. The OpenTelemetry configuration is the extension with 20 no-op hooks.

page master, cycles per request (GCC) GCC 15.2 Clang 21.1 GCC + OpenTelemetry
Laravel / (Blade page) 8.5 M +0.5% +0.5% +1.2%
Laravel /users (100 models as JSON) 57.7 M +1.3% −0.6% +1.0%
Symfony Demo /en/blog/ 29.8 M +1.5% +0.4% +1.1%
Laravel worker loop, per request 27.4 M +0.7% −0.6% −1.3%

Instructions go down 0.1–1.0% in every row, but cycles mostly go up.

With GCC, code placement appears to be a substantial contributor. The PR moves the code after zend_closures.c by 128 bytes. A master build padded to reproduce that shift costs 0.5–1.6% on its own, with effectively unchanged instruction counts. GCC master against itself stays within 0.2% at the median.

Against the padded build, the PR ranges from −0.8% to +0.6% across two series, and the direction is not consistent between them. So the placement control reproduces a slowdown of comparable size, but I couldn't isolate a stable remaining cycle effect. I also tried moving the allocation into a cold function; that variant was still 0.2–1.1% slower than master.

Release and ASan/UBSan runs showed no new test failures relative to master. The PR's observer test fails on master with OPcache and passes with the PR. Apart from the void * portability issue I commented on, I don't have a concrete implementation change to suggest from these measurements.

Details and scripts are in the report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants