Related posts
- Is a compiled PHP faster? Four builds, one Xeon, and a JIT that makes the question moot
Sury packages, Ubuntu's archive, and PHP built from source with -O3 -march=native and only the modules XenForo needs. On a PHP-bound forum the difference was inside the noise, and the reason is the tracing JIT.
- clang, PHP 8.4 and the missing zlib: the last round of compiled PHP
The final round of the compiled-PHP question: CentminMod's function alignment on its own, a clang build and PHP 8.4, with every module compiled from source. clang is the only build with a repeatable edge (+3 % on XenForo), 8.4 is slower than 8.5, and my earlier source builds were missing zlib.
- Who compresses a logged-in XenForo page? 12 % of PHP, one fastcgi_param, and a clock that lied
Profiling the member pages of a XenForo forum showed zlib eating 12 % of PHP-FPM CPU: XenForo gzips its own pages at level 9 inside the worker. Moving that to Angie gave +5 % on the forum index and +9 % on thread pages. Along the way, a CPU turbo budget faked a 27 % speed-up, and three other ideas did not survive measurement.
The last post compared Sury, Ubuntu’s archive, and a source build with
-O3 -march=native, and found nothing: the tracing JIT generates the hot code at run time, so the C compiler barely matters. The obvious follow-up question is fair: what if you turn on everything? LTO, profile-guided optimisation, function alignment, the interposition and PLT switches, and every module compiled from source against the new tree.So I did that. The short version: the more I enabled, the slower it got. LTO alone was neutral. PGO, alone or with LTO, cost 5–10 % on the application tiers and 18–25 % on a CPU-only probe.
What “everything on” means#
-O3 -m64 -march=native -mtune=nativeMARCH_TARGETNATIVE=y)-falign-functions=32GCC_FALIGN_FUNCTION=y-fno-semantic-interposition-flto -fno-fat-lto-objects -fuse-linker-pluginPHP_LTO=yshape, with bfd instead of gold-fprofile-generate→ train →-fprofile-usePHP_PGO=y, but trained on real XenForo and WordPress traffic instead of phpbench--disable-gcc-global-regsThe module set is XenForo’s, compiled into the binary: mysqli, gd (jpeg, freetype, webp), curl, mbstring, xml/dom/simplexml, zip, gmp, exif, bcmath, sockets, OPcache with the JIT. On top of that, redis 6.3.0 and imagick 3.8.1 were built from PECL sources with the new tree’s
phpize, so the extensions are not borrowed from the distribution either (except that the shared library dependencies — libgd, libcurl, libxml2 — stay system packages, which is what “source compiled PHP” means in practice).One module would not build: igbinary 3.2.16 still includes
ext/standard/php_smart_string.h, which PHP 8.5 removed. Redis 6.3.0 detects that and builds without it. XenForo’s core Redis provider uses PHP’s serializer, so the application does not notice.How PGO was trained#
PGO has two stages and the training has to match what you measure, so I built an instrumented
php-fpm, put it behind the same Angie vhosts, and drove the real applications through it:That produced 779
.gcdafiles. The rebuild with-fprofile-usereported zero missing profiles, so every object really was optimised with data. Then the arm was switched back in and the benchmark ran exactly like the others: clock pinned at 3.6 GHz, 3 rounds, arms rotated, 100 connections, 10 s per tier.The numbers#
Sury is the package every host runs today (8.5.13). “LTO” adds LTO and the alignment/interposition flags to the previous post’s
-O3 -march=nativebuild (which measured flat on all four tiers). “LTO+PGO” adds the two-stage profile. “PGO” is the same without LTO. Each arm is its own 3-round run against Sury on the same host and clock; medians, spread in parentheses.The source-native column is from the previous post and is flat; the absolute Sury numbers move a little between runs because the host restarts PHP between arms, so compare each row with its own Sury column, not across rows.
Against Sury: LTO is a wash (−0.7 % to +0.0 %). LTO + PGO is −9.7 % on the XenForo thread, −9.1 % on the home page, −3.0 % on WordPress home, flat on WordPress post. PGO alone is −6.5 %, −5.0 %, −5.7 %, −5.1 %. On the XenForo tiers the spreads do not overlap; on WordPress the PGO spreads are so tight that the median drop is still the signal.
The cleanest evidence is the CPU-only probe, run 15 times per arm with no web server in the loop:
The minimum of 15 runs is shown; medians agree within 1 %. The “everything on” binary is slower even with the JIT off, where the interpreter is supposed to be exactly what PGO optimises. So this is not a JIT artefact: the generated code itself is worse for this workload.
Why would PGO make it slower?#
Three things I can point at, and one I cannot rule out:
-fprofile-useGCC enables more inlining, loop peeling and function reordering. PHP’s interpreter is a very large switch with a lot of cold code; profile-driven inlining can bloat the hot path and cost instruction cache.-falign-functions=32adds padding on top of that.The lesson from the previous post still holds and is now stronger: with
opcache.jit=tracing, the code that runs your PHP is generated at run time. Compiler flags reach the interpreter, the runtime helpers and the JIT compiler — never the code that actually renders the page. Optimising that 20 % harder cannot buy 20 %; here it bought negative numbers.What failed before it ran#
Three build-system traps, in case you try this at home:
-fno-pltwith LTO does not link. The link failed with hundreds ofundefined reference to '.L1'-style errors from bfd: PLT-less calls plus LTO partitioning produced local labels with no definition. Dropping-fno-pltand-flto-partition=maxfixed it.conftest.c. The secondconfigurereuses the same/root/work/php-src-max/conftest.cpath, whose profile from the first stage no longer matches, and GCC treats it as an error (-Werror=coverage-mismatch). Deleting the*conftest*profile files and adding-Wno-error=coverage-mismatchfixes it..urlfiles written by the exercise script already contain a full URL, and I was prefixing the host again, sowrkrejected every URL and the script swallowed the error. 779.gcdafiles existed anyway, because starting and stopping FPM writes them. If a PGO result looks impossible, check that the training actually made requests.What I would do#
The harness for all of this is in
tests/bench-php-matrix/in ubuntu-setup-script:build-php-max.sh(plain/LTO/PGO stages, every module from source),train-php-pgo.sh,switch-php-arm.sh, andbench-php-sources.sh. The host went back to its pristine 696-package manifest afterwards.