Skip to content

Fix GH-25380: Do not cache stateless closures with heap runtime cache - #25545

Open
prateekbhujel wants to merge 1 commit into
php:PHP-8.6from
prateekbhujel:fix/gh25380-stateless-closure-heap-rt-cache
Open

prateekbhujel wants to merge 1 commit into
php:PHP-8.6from
prateekbhujel:fix/gh25380-stateless-closure-heap-rt-cache

Conversation

@prateekbhujel

Copy link
Copy Markdown
Contributor

Closes GH-25380

ZEND_DECLARE_LAMBDA_FUNCTION caches stateless closures in EX(run_time_cache) via CACHE_POLYMORPHIC_PTR and pushes a reference onto EG(lambda_cache), which is only released at request shutdown.

When the declaring function (EX(func)) has ZEND_ACC_HEAP_RT_CACHE (via Closure::bind()/bindTo() with a scope change, Closure::call(), or closures inside trait methods reused across multiple classes), its runtime cache is efree()d when the outer closure or call() finishes. Subsequent calls allocate a fresh zeroed runtime cache, miss CACHED_POLYMORPHIC_PTR, and push another unreachable closure onto EG(lambda_cache) for the rest of the request.

Skip stateless closure caching when EX(func)->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE is set. Because the check sits on the cache-miss write path (and heap runtime caches are zero-initialized), normal arena-cached ZEND_DECLARE_LAMBDA_FUNCTION cache hits pay no extra cost.

Checked sibling caches as well: ZEND_CALLABLE_CONVERT (EG(callable_convert_cache)) and ZEND_CALLABLE_CONVERT_PARTIAL (EG(partial_function_application_cache)) deduplicate in request HashTables and already stay bounded across repeated Closure::bind() calls.

Verification

  • Zend/tests/closures/: 139 passed, 1 skipped (zend_test)
  • Zend/tests/closures/gh25380.phpt: passed with both opcache.enable_cli=0 and opcache.enable_cli=1
  • ext/opcache/tests/: 1027 passed, 46 skipped, 2 expected fail
  • Zend/tests/: 5562 passed, 178 skipped, 1 expected fail (new_oom.phpt only failed locally due to an unquoted space in the local workspace directory path)

@ArtUkrainskiy

Copy link
Copy Markdown
Contributor

I reproduced this on current PHP-8.6. The bind, call and trait cases keep about 418 bytes per call until the end of the request, and nothing with the PR.

With opcache on there's one more case. A plain method that creates a closure on every call, with a static fn inside it, leaks the same way. It's 8.4 MB per 20,000 calls of get(), and 0 without opcache and on 8.5:

class Repo {
    public function get(array $rows) {
        return array_map(function ($r) { return (static fn ($x) => $x * 2)($r); }, $rows);
    }
}

Under opcache every closure declared in a method gets its own heap run-time cache when it's created, so the PR fixes this too. Could you add it to the test?

Change in cycles per call against PHP-8.6 (negative = fewer cycles):

case PHP-8.6, cycles per call (GCC) GCC 15 clang 21
Closure::bind() on every call 1,937 −66% −69%
->call() on every call 1,838 −68% −71%
closure in a trait method, second class 2,096 −61% −64%
closure bound once, called many times 190 +104% +73%
method with a static fn, no closure around it 189 −2% +3%

The one cost is a closure bound once and then called many times. It used to hit the cache and now creates the static closure on every call, like 8.5 did. The last row has the same instruction count, so that ±3% is code layout, not the change. Zend/tests/closures and ext/opcache/tests give the same results as PHP-8.6 under ASan, except gh25380.phpt, which fails without the fix. LGTM.

ZEND_DECLARE_LAMBDA_FUNCTION caches closures in the declaring op_array's
runtime cache and pushes a reference onto EG(lambda_cache), which is only
released at request shutdown. When the declaring function has
ZEND_ACC_HEAP_RT_CACHE (Closure::bind/bindTo with scope changes,
Closure::call, or closures inside trait methods reused across classes),
its runtime cache is freed with the closure or call, leaving unreachable
closures accumulating in EG(lambda_cache) for the rest of the request.

Skip stateless closure caching when EX(func) uses ZEND_ACC_HEAP_RT_CACHE.
@prateekbhujel
prateekbhujel force-pushed the fix/gh25380-stateless-closure-heap-rt-cache branch from 663f1f5 to 763c491 Compare November 21, 2026 04:21
@prateekbhujel

Copy link
Copy Markdown
Contributor Author

Thanks for reproducing, benchmarking, and finding that opcache method-closure case, @ArtUkrainskiy! I've added it to Zend/tests/closures/gh25380.phpt and verified it passes with opcache off, on, and JIT enabled.

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.

2 participants