Skip to content

Commit 82ccd47

Browse files
authored
Use ZEND_TLS for spl (#7043)
For extension globals that do not require cross-module visibility, we can use ZEND_TLS as a more efficient TLS mechanism.
1 parent 97a8496 commit 82ccd47

File tree

2 files changed

+36
-56
lines changed

2 files changed

+36
-56
lines changed

ext/spl/php_spl.c

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,11 @@
4343
ZEND_GET_MODULE(spl)
4444
#endif
4545

46-
ZEND_DECLARE_MODULE_GLOBALS(spl)
46+
ZEND_TLS zend_string *spl_autoload_extensions;
47+
ZEND_TLS HashTable *spl_autoload_functions;
4748

4849
#define SPL_DEFAULT_FILE_EXTENSIONS ".inc,.php"
4950

50-
/* {{{ PHP_GINIT_FUNCTION */
51-
static PHP_GINIT_FUNCTION(spl)
52-
{
53-
spl_globals->autoload_extensions = NULL;
54-
spl_globals->autoload_functions = NULL;
55-
}
56-
/* }}} */
57-
5851
static zend_class_entry * spl_find_ce_by_name(zend_string *name, bool autoload)
5952
{
6053
zend_class_entry *ce;
@@ -307,7 +300,7 @@ PHP_FUNCTION(spl_autoload)
307300
}
308301

309302
if (!file_exts) {
310-
file_exts = SPL_G(autoload_extensions);
303+
file_exts = spl_autoload_extensions;
311304
}
312305

313306
if (file_exts == NULL) { /* autoload_extensions is not initialized, set to defaults */
@@ -345,17 +338,17 @@ PHP_FUNCTION(spl_autoload_extensions)
345338
}
346339

347340
if (file_exts) {
348-
if (SPL_G(autoload_extensions)) {
349-
zend_string_release_ex(SPL_G(autoload_extensions), 0);
341+
if (spl_autoload_extensions) {
342+
zend_string_release_ex(spl_autoload_extensions, 0);
350343
}
351-
SPL_G(autoload_extensions) = zend_string_copy(file_exts);
344+
spl_autoload_extensions = zend_string_copy(file_exts);
352345
}
353346

354-
if (SPL_G(autoload_extensions) == NULL) {
347+
if (spl_autoload_extensions == NULL) {
355348
RETURN_STRINGL(SPL_DEFAULT_FILE_EXTENSIONS, sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1);
356349
} else {
357-
zend_string_addref(SPL_G(autoload_extensions));
358-
RETURN_STR(SPL_G(autoload_extensions));
350+
zend_string_addref(spl_autoload_extensions);
351+
RETURN_STR(spl_autoload_extensions);
359352
}
360353
} /* }}} */
361354

@@ -413,17 +406,17 @@ static bool autoload_func_info_equals(
413406
}
414407

415408
static zend_class_entry *spl_perform_autoload(zend_string *class_name, zend_string *lc_name) {
416-
if (!SPL_G(autoload_functions)) {
409+
if (!spl_autoload_functions) {
417410
return NULL;
418411
}
419412

420413
/* We don't use ZEND_HASH_FOREACH here,
421414
* because autoloaders may be added/removed during autoloading. */
422415
HashPosition pos;
423-
zend_hash_internal_pointer_reset_ex(SPL_G(autoload_functions), &pos);
416+
zend_hash_internal_pointer_reset_ex(spl_autoload_functions, &pos);
424417
while (1) {
425418
autoload_func_info *alfi =
426-
zend_hash_get_current_data_ptr_ex(SPL_G(autoload_functions), &pos);
419+
zend_hash_get_current_data_ptr_ex(spl_autoload_functions, &pos);
427420
if (!alfi) {
428421
break;
429422
}
@@ -451,7 +444,7 @@ static zend_class_entry *spl_perform_autoload(zend_string *class_name, zend_stri
451444
}
452445
}
453446

454-
zend_hash_move_forward_ex(SPL_G(autoload_functions), &pos);
447+
zend_hash_move_forward_ex(spl_autoload_functions, &pos);
455448
}
456449
return NULL;
457450
}
@@ -480,12 +473,12 @@ PHP_FUNCTION(spl_autoload_call)
480473
} while (0)
481474

482475
static Bucket *spl_find_registered_function(autoload_func_info *find_alfi) {
483-
if (!SPL_G(autoload_functions)) {
476+
if (!spl_autoload_functions) {
484477
return NULL;
485478
}
486479

487480
autoload_func_info *alfi;
488-
ZEND_HASH_FOREACH_PTR(SPL_G(autoload_functions), alfi) {
481+
ZEND_HASH_FOREACH_PTR(spl_autoload_functions, alfi) {
489482
if (autoload_func_info_equals(alfi, find_alfi)) {
490483
return _p;
491484
}
@@ -514,11 +507,11 @@ PHP_FUNCTION(spl_autoload_register)
514507
"spl_autoload_register() will always throw");
515508
}
516509

517-
if (!SPL_G(autoload_functions)) {
518-
ALLOC_HASHTABLE(SPL_G(autoload_functions));
519-
zend_hash_init(SPL_G(autoload_functions), 1, NULL, autoload_func_info_zval_dtor, 0);
510+
if (!spl_autoload_functions) {
511+
ALLOC_HASHTABLE(spl_autoload_functions);
512+
zend_hash_init(spl_autoload_functions, 1, NULL, autoload_func_info_zval_dtor, 0);
520513
/* Initialize as non-packed hash table for prepend functionality. */
521-
zend_hash_real_init_mixed(SPL_G(autoload_functions));
514+
zend_hash_real_init_mixed(spl_autoload_functions);
522515
}
523516

524517
/* If first arg is not null */
@@ -558,10 +551,10 @@ PHP_FUNCTION(spl_autoload_register)
558551
RETURN_TRUE;
559552
}
560553

561-
zend_hash_next_index_insert_ptr(SPL_G(autoload_functions), alfi);
562-
if (prepend && SPL_G(autoload_functions)->nNumOfElements > 1) {
554+
zend_hash_next_index_insert_ptr(spl_autoload_functions, alfi);
555+
if (prepend && spl_autoload_functions->nNumOfElements > 1) {
563556
/* Move the newly created element to the head of the hashtable */
564-
HT_MOVE_TAIL_TO_HEAD(SPL_G(autoload_functions));
557+
HT_MOVE_TAIL_TO_HEAD(spl_autoload_functions);
565558
}
566559

567560
RETURN_TRUE;
@@ -580,15 +573,15 @@ PHP_FUNCTION(spl_autoload_unregister)
580573
if (fcc.function_handler && zend_string_equals_literal(
581574
fcc.function_handler->common.function_name, "spl_autoload_call")) {
582575
/* Don't destroy the hash table, as we might be iterating over it right now. */
583-
zend_hash_clean(SPL_G(autoload_functions));
576+
zend_hash_clean(spl_autoload_functions);
584577
RETURN_TRUE;
585578
}
586579

587580
autoload_func_info *alfi = autoload_func_info_from_fci(&fci, &fcc);
588581
Bucket *p = spl_find_registered_function(alfi);
589582
autoload_func_info_destroy(alfi);
590583
if (p) {
591-
zend_hash_del_bucket(SPL_G(autoload_functions), p);
584+
zend_hash_del_bucket(spl_autoload_functions, p);
592585
RETURN_TRUE;
593586
}
594587

@@ -605,8 +598,8 @@ PHP_FUNCTION(spl_autoload_functions)
605598
}
606599

607600
array_init(return_value);
608-
if (SPL_G(autoload_functions)) {
609-
ZEND_HASH_FOREACH_PTR(SPL_G(autoload_functions), alfi) {
601+
if (spl_autoload_functions) {
602+
ZEND_HASH_FOREACH_PTR(spl_autoload_functions, alfi) {
610603
if (alfi->closure) {
611604
GC_ADDREF(alfi->closure);
612605
add_next_index_object(return_value, alfi->closure);
@@ -723,21 +716,21 @@ PHP_MINIT_FUNCTION(spl)
723716

724717
PHP_RINIT_FUNCTION(spl) /* {{{ */
725718
{
726-
SPL_G(autoload_extensions) = NULL;
727-
SPL_G(autoload_functions) = NULL;
719+
spl_autoload_extensions = NULL;
720+
spl_autoload_functions = NULL;
728721
return SUCCESS;
729722
} /* }}} */
730723

731724
PHP_RSHUTDOWN_FUNCTION(spl) /* {{{ */
732725
{
733-
if (SPL_G(autoload_extensions)) {
734-
zend_string_release_ex(SPL_G(autoload_extensions), 0);
735-
SPL_G(autoload_extensions) = NULL;
726+
if (spl_autoload_extensions) {
727+
zend_string_release_ex(spl_autoload_extensions, 0);
728+
spl_autoload_extensions = NULL;
736729
}
737-
if (SPL_G(autoload_functions)) {
738-
zend_hash_destroy(SPL_G(autoload_functions));
739-
FREE_HASHTABLE(SPL_G(autoload_functions));
740-
SPL_G(autoload_functions) = NULL;
730+
if (spl_autoload_functions) {
731+
zend_hash_destroy(spl_autoload_functions);
732+
FREE_HASHTABLE(spl_autoload_functions);
733+
spl_autoload_functions = NULL;
741734
}
742735
return SUCCESS;
743736
} /* }}} */
@@ -753,10 +746,6 @@ zend_module_entry spl_module_entry = {
753746
PHP_RSHUTDOWN(spl),
754747
PHP_MINFO(spl),
755748
PHP_SPL_VERSION,
756-
PHP_MODULE_GLOBALS(spl),
757-
PHP_GINIT(spl),
758-
NULL,
759-
NULL,
760-
STANDARD_MODULE_PROPERTIES_EX
749+
STANDARD_MODULE_PROPERTIES
761750
};
762751
/* }}} */

ext/spl/php_spl.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@ PHP_RINIT_FUNCTION(spl);
5050
PHP_RSHUTDOWN_FUNCTION(spl);
5151
PHP_MINFO_FUNCTION(spl);
5252

53-
54-
ZEND_BEGIN_MODULE_GLOBALS(spl)
55-
zend_string *autoload_extensions;
56-
HashTable *autoload_functions;
57-
ZEND_END_MODULE_GLOBALS(spl)
58-
59-
ZEND_EXTERN_MODULE_GLOBALS(spl)
60-
#define SPL_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(spl, v)
61-
6253
PHPAPI zend_string *php_spl_object_hash(zend_object *obj);
6354

6455
#endif /* PHP_SPL_H */

0 commit comments

Comments
 (0)