Repository navigation
Share closure run time cache when possible for improved closure performance #25683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -906,28 +906,34 @@ static void zend_create_closure_ex( | |
|
|
||
| /* Runtime cache is scope-dependent, so we cannot reuse it if the scope changed */ | ||
| ptr = ZEND_MAP_PTR_GET(func->op_array.run_time_cache); | ||
| if (!ptr | ||
| if ((func->common.fn_flags & (ZEND_ACC_CLOSURE|ZEND_ACC_FAKE_CLOSURE)) == ZEND_ACC_CLOSURE) { | ||
| /* Closures cannot store their scope in func->common.scope, as it may be immutable. | ||
| * To work around this, we store the scope associated with a shared runtime | ||
| * cache in a slot just before the op_array's cache. | ||
| * This allows closures created in the same scope to share a single runtime cache. | ||
| * Invariant: A closure's cache is always either such a shared cache or a heap cache. */ | ||
| if (!ptr) { | ||
| void **mem = zend_arena_alloc(&CG(arena), sizeof(void *) + func->op_array.cache_size); | ||
| mem[0] = scope; | ||
| ptr = mem + 1; | ||
| memset(ptr, 0, func->op_array.cache_size); | ||
| ZEND_MAP_PTR_SET(func->op_array.run_time_cache, ptr); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With OPcache on, two closures from the same factory partially apply a trait method and then its alias. The alias calls |
||
| } else if ((func->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE) | ||
| || ((zend_class_entry **)ptr)[-1] != scope) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extensions can initialize this cache through |
||
| ptr = NULL; | ||
| } | ||
| } else if (!ptr | ||
| || func->common.scope != scope | ||
| || (func->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE) | ||
| ) { | ||
| if (!ptr | ||
| && (func->common.fn_flags & ZEND_ACC_CLOSURE) | ||
| && (func->common.scope == scope || | ||
| !(func->common.fn_flags & ZEND_ACC_IMMUTABLE))) { | ||
| /* If a real closure is used for the first time, we create a shared runtime cache | ||
| * and remember which scope it is for. */ | ||
| if (func->common.scope != scope) { | ||
| func->common.scope = scope; | ||
| } | ||
| ptr = zend_arena_alloc(&CG(arena), func->op_array.cache_size); | ||
| ZEND_MAP_PTR_SET(func->op_array.run_time_cache, ptr); | ||
| closure->func.op_array.fn_flags &= ~ZEND_ACC_HEAP_RT_CACHE; | ||
| } else { | ||
| /* Otherwise, we use a non-shared runtime cache */ | ||
| ptr = emalloc(func->op_array.cache_size); | ||
| closure->func.op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE; | ||
| } | ||
| /* Functions and methods share their own runtime cache only for their own scope */ | ||
| ptr = NULL; | ||
| } | ||
| if (!ptr) { | ||
| /* Otherwise, we use a non-shared runtime cache */ | ||
| ptr = emalloc(func->op_array.cache_size); | ||
| memset(ptr, 0, func->op_array.cache_size); | ||
| closure->func.op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE; | ||
| } | ||
| ZEND_MAP_PTR_INIT(closure->func.op_array.run_time_cache, ptr); | ||
| } else { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On ARMv7,
mem + 1shifts the cache by four bytes, misaligning the eight-bytezvalslots used byZEND_RECV_INIT. Round the prefix up toZEND_MM_ALIGNMENTand store the scope immediately before the aligned cache.