Skip to content

TSRM: make CG, EG, SCNG and AG compile-time offsets#22287

Open
henderkes wants to merge 1 commit into
php:masterfrom
henderkes:perf/compile_time_global_offsets
Open

TSRM: make CG, EG, SCNG and AG compile-time offsets#22287
henderkes wants to merge 1 commit into
php:masterfrom
henderkes:perf/compile_time_global_offsets

Conversation

@henderkes

@henderkes henderkes commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

saves an offset load every time they're accessed

fifth split from #22231

a bit of a different approach (keep everything on the existing heap allocations, just reordered) so we don't need to work around the apache module tls surplus problem and don't need to fall back to global-exec

it's not the entire gain of the other PR, but still something. measurements without the local-exec or -mtls-size change (which will benefit on top, together they restore around half the ZTS penalty over NTS):

buildPHPStan instrPHPStan wallphpbench (no JIT)phpbench (JIT)
gcc NTS840.5827 B68.6s7091681179326
gcc ZTS881.9715 B68.0s6693791130824
gcc ZTS patch863.8799 B (−2.051%)66.7s (−1.912%)680468 (+1.66%)1135553 (+0.42%)
clang NTS846.6887 B65.7s7056341141506
clang ZTS898.4707 B68.4s6700861089468
clang ZTS patch870.6615 B (−3.095%)67.2s (−1.754%)686443 (+2.44%)1095468 (+0.55%)

saves an offset load every time they're accessed
Comment thread main/main.c
TSRM_ALIGNED_SIZE(sizeof(zend_compiler_globals)) +
TSRM_ALIGNED_SIZE(sizeof(zend_executor_globals)) +
TSRM_ALIGNED_SIZE(sizeof(zend_php_scanner_globals)) +
TSRM_ALIGNED_SIZE(zend_mm_globals_size())); // AG size, exposed through function call

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really unhappy about this, same issue as below:

Comment thread TSRM/TSRM.c
Comment on lines +39 to +47
#if defined(__cplusplus) || defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202311L)
# define TSRM_STATIC_ASSERT(c, m) static_assert((c), m)
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */
# define TSRM_STATIC_ASSERT(c, m) _Static_assert((c), m)
#else
# define TSRM_STATIC_ASSERT(c, m)
#endif
TSRM_STATIC_ASSERT(TSRM_ALIGNED_SIZE(sizeof(tsrm_tls_entry)) == TSRM_FAST_RESERVED_BASE,
"TSRM_FAST_RESERVED_BASE must equal the tsrm_tls_entry header size");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is a two-fold problem:

  • duplicated ZEND_STATIC_ASSERT copy - tsrm can't use zend_portability, because zend_portability depends on tsrm.h
  • tsrm_ls_entry size is only known in this file, not in tsrm.h consumers.
  • AG size can be consumed at runtime, so it gets the last slot, but it's structurally the same problem

In C++ this would be a simple consteval function in the header, but I have no idea how to make it work in C. So I kept the definition private, but force it to the specific size it has.

Comment thread Zend/zend_portability.h
#define ZEND_CGG_DIAGNOSTIC_IGNORED_END ZEND_DIAGNOSTIC_IGNORED_END

#if defined(__cplusplus)
#if defined(__cplusplus) || defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202311L)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically unrelated, but I didn't want to copy an incomplete definition into tsrm.c

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see also #21228.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be in favour, getting php-src and extensions to compile in extremely old environments is a pain anyway (I do rhel-7 builds for frankenphp). Should I replace this then?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I replace this then?

Not yet, probably. See: https://news-web.php.net/php.internals/130714

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants