Repository navigation
Fix GH-25380: Do not cache stateless closures with heap runtime cache - #25545
prateekbhujel wants to merge 1 commit into
Conversation
|
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 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):
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.
663f1f5 to
763c491
Compare
|
Thanks for reproducing, benchmarking, and finding that opcache method-closure case, @ArtUkrainskiy! I've added it to |
Closes GH-25380
ZEND_DECLARE_LAMBDA_FUNCTIONcaches stateless closures inEX(run_time_cache)viaCACHE_POLYMORPHIC_PTRand pushes a reference ontoEG(lambda_cache), which is only released at request shutdown.When the declaring function (
EX(func)) hasZEND_ACC_HEAP_RT_CACHE(viaClosure::bind()/bindTo()with a scope change,Closure::call(), or closures inside trait methods reused across multiple classes), its runtime cache isefree()d when the outer closure orcall()finishes. Subsequent calls allocate a fresh zeroed runtime cache, missCACHED_POLYMORPHIC_PTR, and push another unreachable closure ontoEG(lambda_cache)for the rest of the request.Skip stateless closure caching when
EX(func)->common.fn_flags & ZEND_ACC_HEAP_RT_CACHEis set. Because the check sits on the cache-miss write path (and heap runtime caches are zero-initialized), normal arena-cachedZEND_DECLARE_LAMBDA_FUNCTIONcache hits pay no extra cost.Checked sibling caches as well:
ZEND_CALLABLE_CONVERT(EG(callable_convert_cache)) andZEND_CALLABLE_CONVERT_PARTIAL(EG(partial_function_application_cache)) deduplicate in request HashTables and already stay bounded across repeatedClosure::bind()calls.Verification
Zend/tests/closures/: 139 passed, 1 skipped (zend_test)Zend/tests/closures/gh25380.phpt: passed with bothopcache.enable_cli=0andopcache.enable_cli=1ext/opcache/tests/: 1027 passed, 46 skipped, 2 expected failZend/tests/: 5562 passed, 178 skipped, 1 expected fail (new_oom.phptonly failed locally due to an unquoted space in the local workspace directory path)