Commit 098ee83005f for php

commit 098ee83005f920929a65cb784022afffc8974568
Author: Marc <m@pyc.ac>
Date:   Fri Nov 20 17:46:22 2026 +0700

    perf: use native TLS storage for pcre and standard module globals (#25618)

diff --git a/UPGRADING.INTERNALS b/UPGRADING.INTERNALS
index 5d1864f3d6a..b75e3909f41 100644
--- a/UPGRADING.INTERNALS
+++ b/UPGRADING.INTERNALS
@@ -53,9 +53,9 @@ PHP 8.7 INTERNALS UPGRADE NOTES
     the `disable_classes` INI setting in PHP 8.5.
   . Removed HAVE_NORETURN and HAVE_NORETURN_ALIAS. ZEND_NORETURN is guaranteed
     to work.
-  . CG(), EG(), PG(), SG(), CWDG(), SIGG(), ZCG(), JIT_G() and INI_SCNG() use
-    native TLS in ZTS builds. The compiler_globals_offset, executor_globals_offset,
-    core_globals_offset, sapi_globals_offset, cwd_globals_offset,
+  . CG(), EG(), PG(), SG(), CWDG(), SIGG(), ZCG(), JIT_G(), INI_SCNG(), PCRE_G()
+    and BG() use native TLS in ZTS builds. The compiler_globals_offset,
+    executor_globals_offset, core_globals_offset, sapi_globals_offset, cwd_globals_offset,
     zend_signal_globals_offset, accel_globals_offset, jit_globals_offset and
     ini_scanner_globals_offset symbols have been removed.
   . php_reserve_tsrm_memory() has been removed.
diff --git a/ext/pcre/php_pcre.c b/ext/pcre/php_pcre.c
index c75d7049d0a..5c58e7385fd 100644
--- a/ext/pcre/php_pcre.c
+++ b/ext/pcre/php_pcre.c
@@ -61,6 +61,10 @@ struct _pcre_cache_entry {
 };

 PHPAPI ZEND_DECLARE_MODULE_GLOBALS(pcre)
+#ifdef ZTS
+ZEND_TLS_API TSRM_TLS TSRM_TLS_MODEL_ATTR zend_pcre_globals pcre_globals;
+static void *pcre_globals_tls_addr(void) { return &pcre_globals; }
+#endif

 #ifdef HAVE_PCRE_JIT_SUPPORT
 #define PCRE_JIT_STACK_MIN_SIZE (32 * 1024)
@@ -410,6 +414,12 @@ static PHP_MINFO_FUNCTION(pcre)
 /* {{{ PHP_MINIT_FUNCTION(pcre) */
 static PHP_MINIT_FUNCTION(pcre)
 {
+#ifdef ZTS
+	ts_allocate_tls_id(&pcre_globals_id, pcre_globals_tls_addr, sizeof(zend_pcre_globals), (ts_allocate_ctor) PHP_GINIT(pcre), (ts_allocate_dtor) PHP_GSHUTDOWN(pcre));
+#else
+	ZEND_MODULE_GLOBALS_CTOR_N(pcre)(&pcre_globals);
+#endif
+
 #ifdef HAVE_PCRE_JIT_SUPPORT
 	if (UNEXPECTED(!pcre2_init_ok)) {
 		/* Retry. */
@@ -433,6 +443,11 @@ static PHP_MINIT_FUNCTION(pcre)
 /* {{{ PHP_MSHUTDOWN_FUNCTION(pcre) */
 static PHP_MSHUTDOWN_FUNCTION(pcre)
 {
+#ifdef ZTS
+	ts_free_id(pcre_globals_id);
+#else
+	ZEND_MODULE_GLOBALS_DTOR_N(pcre)(&pcre_globals);
+#endif
 	UNREGISTER_INI_ENTRIES();

 	free(php_pcre_version);
@@ -3147,10 +3162,7 @@ zend_module_entry pcre_module_entry = {
 	PHP_RSHUTDOWN(pcre),
 	PHP_MINFO(pcre),
 	PHP_PCRE_VERSION,
-	PHP_MODULE_GLOBALS(pcre),
-	PHP_GINIT(pcre),
-	PHP_GSHUTDOWN(pcre),
-	NULL,
+	NO_MODULE_GLOBALS,
 	STANDARD_MODULE_PROPERTIES_EX
 };

diff --git a/ext/pcre/php_pcre.h b/ext/pcre/php_pcre.h
index 8b104f20497..7312b36307f 100644
--- a/ext/pcre/php_pcre.h
+++ b/ext/pcre/php_pcre.h
@@ -85,7 +85,12 @@ ZEND_BEGIN_MODULE_GLOBALS(pcre)
 ZEND_END_MODULE_GLOBALS(pcre)

 PHPAPI ZEND_EXTERN_MODULE_GLOBALS(pcre)
-#define PCRE_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(pcre, v)
+#ifdef ZEND_TLS_DIRECT
+# define PCRE_G(v) (pcre_globals.v)
+extern ZEND_TLS_API TSRM_TLS TSRM_TLS_MODEL_ATTR zend_pcre_globals pcre_globals;
+#else
+# define PCRE_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(pcre, v)
+#endif

 #define phpext_pcre_ptr pcre_module_ptr

diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c
index 9ae99b6f1ee..8443908a6a8 100644
--- a/ext/standard/basic_functions.c
+++ b/ext/standard/basic_functions.c
@@ -104,6 +104,8 @@ typedef struct yy_buffer_state *YY_BUFFER_STATE;

 #ifdef ZTS
 PHPAPI int basic_globals_id;
+ZEND_TLS_API TSRM_TLS TSRM_TLS_MODEL_ATTR php_basic_globals basic_globals;
+static void *basic_globals_tls_addr(void) { return &basic_globals; }
 #else
 PHPAPI php_basic_globals basic_globals;
 #endif
@@ -277,7 +279,7 @@ PHPAPI double php_get_inf(void) /* {{{ */
 PHP_MINIT_FUNCTION(basic) /* {{{ */
 {
 #ifdef ZTS
-	ts_allocate_id(&basic_globals_id, sizeof(php_basic_globals), (ts_allocate_ctor) basic_globals_ctor, (ts_allocate_dtor) basic_globals_dtor);
+	ts_allocate_tls_id(&basic_globals_id, basic_globals_tls_addr, sizeof(php_basic_globals), (ts_allocate_ctor) basic_globals_ctor, (ts_allocate_dtor) basic_globals_dtor);
 # ifdef PHP_WIN32
 	ts_allocate_id(&php_win32_core_globals_id, sizeof(php_win32_core_globals), (ts_allocate_ctor)php_win32_core_globals_ctor, (ts_allocate_dtor)php_win32_core_globals_dtor );
 # endif
diff --git a/ext/standard/basic_functions.h b/ext/standard/basic_functions.h
index 10dfd584d70..1fe1bedd4b6 100644
--- a/ext/standard/basic_functions.h
+++ b/ext/standard/basic_functions.h
@@ -108,8 +108,13 @@ typedef struct _php_basic_globals {
 } php_basic_globals;

 #ifdef ZTS
-#define BG(v) ZEND_TSRMG(basic_globals_id, php_basic_globals *, v)
 PHPAPI extern int basic_globals_id;
+# ifdef ZEND_TLS_DIRECT
+#  define BG(v) (basic_globals.v)
+extern ZEND_TLS_API TSRM_TLS TSRM_TLS_MODEL_ATTR php_basic_globals basic_globals;
+# else
+#  define BG(v) ZEND_TSRMG(basic_globals_id, php_basic_globals *, v)
+# endif
 #else
 #define BG(v) (basic_globals.v)
 PHPAPI extern php_basic_globals basic_globals;