-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Make internal run_time_cache a persistent allocation #15040
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
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 |
---|---|---|
|
@@ -735,14 +735,16 @@ static void compiler_globals_ctor(zend_compiler_globals *compiler_globals) /* {{ | |
compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL); | ||
compiler_globals->map_ptr_size = 0; | ||
compiler_globals->map_ptr_last = global_map_ptr_last; | ||
if (compiler_globals->map_ptr_last) { | ||
compiler_globals->internal_run_time_cache = NULL; | ||
if (compiler_globals->map_ptr_last || zend_map_ptr_static_size) { | ||
/* Allocate map_ptr table */ | ||
compiler_globals->map_ptr_size = ZEND_MM_ALIGNED_SIZE_EX(compiler_globals->map_ptr_last, 4096); | ||
void *base = pemalloc(compiler_globals->map_ptr_size * sizeof(void*), 1); | ||
void *base = pemalloc((zend_map_ptr_static_size + compiler_globals->map_ptr_size) * sizeof(void*), 1); | ||
compiler_globals->map_ptr_real_base = base; | ||
compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(base); | ||
memset(base, 0, compiler_globals->map_ptr_last * sizeof(void*)); | ||
memset(base, 0, (zend_map_ptr_static_size + compiler_globals->map_ptr_last) * sizeof(void*)); | ||
} | ||
zend_init_internal_run_time_cache(); | ||
} | ||
/* }}} */ | ||
|
||
|
@@ -785,6 +787,10 @@ static void compiler_globals_dtor(zend_compiler_globals *compiler_globals) /* {{ | |
compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL); | ||
compiler_globals->map_ptr_size = 0; | ||
} | ||
if (compiler_globals->internal_run_time_cache) { | ||
pefree(compiler_globals->internal_run_time_cache, 1); | ||
compiler_globals->internal_run_time_cache = NULL; | ||
} | ||
} | ||
/* }}} */ | ||
|
||
|
@@ -1115,6 +1121,10 @@ zend_result zend_post_startup(void) /* {{{ */ | |
} | ||
compiler_globals->map_ptr_real_base = NULL; | ||
compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL); | ||
if (compiler_globals->internal_run_time_cache) { | ||
pefree(compiler_globals->internal_run_time_cache, 1); | ||
} | ||
bwoebi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
compiler_globals->internal_run_time_cache = NULL; | ||
if ((script_encoding_list = (zend_encoding **)compiler_globals->script_encoding_list)) { | ||
compiler_globals_ctor(compiler_globals); | ||
compiler_globals->script_encoding_list = (const zend_encoding **)script_encoding_list; | ||
|
@@ -1198,6 +1208,9 @@ void zend_shutdown(void) /* {{{ */ | |
CG(script_encoding_list_size) = 0; | ||
} | ||
#endif | ||
zend_map_ptr_static_last = 0; | ||
zend_map_ptr_static_size = 0; | ||
|
||
zend_destroy_rsrc_list_dtors(); | ||
|
||
zend_unload_modules(); | ||
|
@@ -1297,9 +1310,9 @@ ZEND_API void zend_activate(void) /* {{{ */ | |
init_executor(); | ||
startup_scanner(); | ||
if (CG(map_ptr_last)) { | ||
memset(CG(map_ptr_real_base), 0, CG(map_ptr_last) * sizeof(void*)); | ||
memset((void **)CG(map_ptr_real_base) + zend_map_ptr_static_size, 0, CG(map_ptr_last) * sizeof(void*)); | ||
} | ||
zend_init_internal_run_time_cache(); | ||
zend_reset_internal_run_time_cache(); | ||
zend_observer_activate(); | ||
} | ||
/* }}} */ | ||
|
@@ -1984,6 +1997,9 @@ void free_estring(char **str_p) /* {{{ */ | |
} | ||
/* }}} */ | ||
|
||
ZEND_API size_t zend_map_ptr_static_size; | ||
ZEND_API size_t zend_map_ptr_static_last; | ||
Comment on lines
+2000
to
+2001
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. Any reason these are mutable globals as opposed to thread-locals? 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. That's the point - they're supposed to be allocated fully during startup. And startup is the same across all threads. |
||
|
||
ZEND_API void zend_map_ptr_reset(void) | ||
{ | ||
CG(map_ptr_last) = global_map_ptr_last; | ||
|
@@ -1996,15 +2012,36 @@ ZEND_API void *zend_map_ptr_new(void) | |
if (CG(map_ptr_last) >= CG(map_ptr_size)) { | ||
/* Grow map_ptr table */ | ||
CG(map_ptr_size) = ZEND_MM_ALIGNED_SIZE_EX(CG(map_ptr_last) + 1, 4096); | ||
CG(map_ptr_real_base) = perealloc(CG(map_ptr_real_base), CG(map_ptr_size) * sizeof(void*), 1); | ||
CG(map_ptr_real_base) = perealloc(CG(map_ptr_real_base), (zend_map_ptr_static_size + CG(map_ptr_size)) * sizeof(void*), 1); | ||
CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(CG(map_ptr_real_base)); | ||
} | ||
ptr = (void**)CG(map_ptr_real_base) + CG(map_ptr_last); | ||
ptr = (void**)CG(map_ptr_real_base) + zend_map_ptr_static_size + CG(map_ptr_last); | ||
*ptr = NULL; | ||
CG(map_ptr_last)++; | ||
return ZEND_MAP_PTR_PTR2OFFSET(ptr); | ||
} | ||
|
||
ZEND_API void *zend_map_ptr_new_static(void) | ||
{ | ||
void **ptr; | ||
|
||
if (zend_map_ptr_static_last >= zend_map_ptr_static_size) { | ||
zend_map_ptr_static_size += 4096; | ||
/* Grow map_ptr table */ | ||
void *new_base = pemalloc((zend_map_ptr_static_size + CG(map_ptr_size)) * sizeof(void*), 1); | ||
if (CG(map_ptr_real_base)) { | ||
memcpy((void **)new_base + 4096, CG(map_ptr_real_base), (CG(map_ptr_last) + zend_map_ptr_static_size - 4096) * sizeof(void *)); | ||
pefree(CG(map_ptr_real_base), 1); | ||
} | ||
CG(map_ptr_real_base) = new_base; | ||
CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(new_base); | ||
} | ||
ptr = (void**)CG(map_ptr_real_base) + (zend_map_ptr_static_last & 4095); | ||
iluuu1994 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*ptr = NULL; | ||
zend_map_ptr_static_last++; | ||
return ZEND_MAP_PTR_PTR2OFFSET(ptr); | ||
} | ||
|
||
ZEND_API void zend_map_ptr_extend(size_t last) | ||
{ | ||
if (last > CG(map_ptr_last)) { | ||
|
@@ -2013,10 +2050,10 @@ ZEND_API void zend_map_ptr_extend(size_t last) | |
if (last >= CG(map_ptr_size)) { | ||
/* Grow map_ptr table */ | ||
CG(map_ptr_size) = ZEND_MM_ALIGNED_SIZE_EX(last, 4096); | ||
CG(map_ptr_real_base) = perealloc(CG(map_ptr_real_base), CG(map_ptr_size) * sizeof(void*), 1); | ||
CG(map_ptr_real_base) = perealloc(CG(map_ptr_real_base), (zend_map_ptr_static_size + CG(map_ptr_size)) * sizeof(void*), 1); | ||
CG(map_ptr_base) = ZEND_MAP_PTR_BIASED_BASE(CG(map_ptr_real_base)); | ||
} | ||
ptr = (void**)CG(map_ptr_real_base) + CG(map_ptr_last); | ||
ptr = (void**)CG(map_ptr_real_base) + zend_map_ptr_static_size + CG(map_ptr_last); | ||
memset(ptr, 0, (last - CG(map_ptr_last)) * sizeof(void*)); | ||
CG(map_ptr_last) = last; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2958,7 +2958,11 @@ ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend | |
if (EG(active)) { // at run-time: this ought to only happen if registered with dl() or somehow temporarily at runtime | ||
ZEND_MAP_PTR_INIT(internal_function->run_time_cache, zend_arena_calloc(&CG(arena), 1, zend_internal_run_time_cache_reserved_size())); | ||
} else { | ||
ZEND_MAP_PTR_NEW(internal_function->run_time_cache); | ||
#if ZTS | ||
ZEND_MAP_PTR_NEW_STATIC(internal_function->run_time_cache); | ||
#else | ||
ZEND_MAP_PTR_INIT(internal_function->run_time_cache, NULL); | ||
#endif | ||
Comment on lines
-2961
to
+2965
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. Can you explain why ZTS makes difference? 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. We need per-thread pointers for ZTS (run-time cache is local to each request). But for NTS we can, as a slight optimization, just skip the indirect lookup via CG(map_ptr_base). 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. Probably worth adding a comment here in code. |
||
} | ||
if (ptr->flags) { | ||
if (!(ptr->flags & ZEND_ACC_PPP_MASK)) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5393,6 +5393,11 @@ static zend_result ffi_fixup_temporaries(void) { | |
++zend_ffi_cast_fn.T; | ||
++zend_ffi_type_fn.T; | ||
} | ||
#if !ZTS | ||
ZEND_MAP_PTR(zend_ffi_new_fn.run_time_cache) = ZEND_MAP_PTR(((zend_internal_function *)zend_hash_str_find_ptr(&zend_ffi_ce->function_table, "new", sizeof("new")-1))->run_time_cache); | ||
ZEND_MAP_PTR(zend_ffi_cast_fn.run_time_cache) = ZEND_MAP_PTR(((zend_internal_function *)zend_hash_str_find_ptr(&zend_ffi_ce->function_table, "cast", sizeof("cast")-1))->run_time_cache); | ||
ZEND_MAP_PTR(zend_ffi_type_fn.run_time_cache) = ZEND_MAP_PTR(((zend_internal_function *)zend_hash_str_find_ptr(&zend_ffi_ce->function_table, "type", sizeof("type")-1))->run_time_cache); | ||
#endif | ||
Comment on lines
+5396
to
+5400
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. Why do we need this? Please use 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. I'm using MAP_PTR_INIT for everything where I assign a value, but in this case I want to copy whatever binary data is, thus I found using ZEND_MAP_PTR directly more fitting and symmetric with the right side. 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. Also, only need it in NTS, as in ZTS this uses a proper map_ptr which is set when the function is created and thus existing here. |
||
if (prev_zend_post_startup_cb) { | ||
return prev_zend_post_startup_cb(); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.