Related posts
- Why my from-source PHP was slower, and the one-line answer: ICU
A from-source php-fpm executed 9 % fewer instructions per page than a clang build using Sury's extensions, yet ran 1-2 % slower. The cause was ICU: the intl extension made the binary 102 MB and multiplied data-TLB misses by 4.5. Without it, the merged build is the fastest: +11 % on logged-in XenForo pages.
- 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.
- Compiling Valkey, and the BOLT flag that gave my binaries an executable stack
Valkey from source was 35 % slower until I stopped passing CFLAGS on make's command line. Then its unit refused the BOLTed binary, which exposed an executable stack in my compiled PHP and Angie too. Plus nginx vs Angie, compiled the same way.
Two earlier posts on this blog, Is a compiled PHP faster? and Everything on: LTO + PGO made PHP slower, concluded that building PHP yourself buys nothing once the tracing JIT is on. This round says that was wrong, or at least incomplete. Two flaws in my build harness were hiding the gain, and the tool that finds it is one I had never tried: LLVM BOLT.
Short version: a php-fpm built with clang, ThinLTO and BOLT serves logged-in XenForo pages 5-10 % faster than Ondřej Surý’s package. Compiling every library from upstream on top of that (newest OpenSSL, ICU, curl, PCRE2, all static) added nothing I could measure. My setup script now offers all three builds behind one switch, so I can measure them again whenever versions change.
What was wrong before#
perfand the CPU’s branch records) and lays the machine code out so the hot paths sit together and the cold paths move out of the way. PHP’s interpreter is a big binary with a small hot core, which is exactly the kind of program that benefits.PHP 8.5 built with clang 19 or newer also uses a newer interpreter design (the “tail-call” VM), which GCC can’t build. That’s part of why the clang arms lead.
The compiler round#
All on the same Xeon E-2236 test host (6 cores, 12 threads), PHP 8.5.13, the same
php.ini(OPcache 512 MB, tracing JIT 64 MB), 12 PHP-FPM workers, Angie in front, a seeded XenForo forum with 20,000 users and 103,000 posts. Load came fromwrkon the same machine, five rounds with the order rotated each round, and the CPU clock pinned at 3.6 GHz so turbo fade couldn’t favour whoever went first.-O3 -march=nativePGO turned out not to be needed: ThinLTO plus BOLT beat PGO plus BOLT, and it needs no training build. Cached guest pages didn’t move at all, because Angie serves those without touching PHP.
Building it so the package still works#
A custom PHP binary is only useful if updates don’t break it. My first version keeps everything the package provides: it compiles the package’s own PHP version with exactly the package’s compiled-in core, linked against Ubuntu’s libraries like the package, so Sury’s shared extensions,
php.ini, pools and systemd unit keep working unchanged. Only the binary that systemd starts changes, through a unit drop-in. Before switching over, the new binary has to report the same modules and the same INI defaults as the package (php-fpm -i), andphp-fpm -thas to accept the live pool configuration.An APT hook watches for security updates: the moment the package’s PHP version changes, the server goes back to the package binary and rebuilds in the background. BOLT runs after the site has served real traffic for a while; it samples the workers for 60 seconds and rewrites the binary.
The “everything from upstream” version#
Then I went further: the newest PHP from php.net, redis and igbinary from PECL, and every library php-fpm links (OpenSSL 4.1, ICU 79, curl 8.23, PCRE2 10.49, libxml2, zlib, libsodium, GMP, libjpeg-turbo, libpng, libwebp, libzip) from each project’s own releases, compiled with the same flags, linked statically, with ThinLTO across PCRE2, zlib and libxml2. The compiler, LLVM 23, came from apt.llvm.org rather than Ubuntu’s archive. Only glibc, libstdc++ and libsystemd still come from Ubuntu. The versions sit in a small file beside the setup script, each one
latestor a pin, and a daily timer rebuilds when any project ships a new release.Getting there took three fixes:
The result: level with the simpler build. On the forum it measured +7.9 % / +9.3 % against the simpler build’s +10.4 % / +10.1 %, a difference inside the run-to-run spread. With the tracing JIT on, page time goes into PHP’s VM and the JIT’s generated code, not into OpenSSL or PCRE2. Newer libraries are nice to have, but they don’t make pages faster.
Only the modules the applications need#
The upstream build compiles in only what my three applications use: WordPress, XenForo and Chevereto. I took the list from XenForo’s installer checks, Chevereto’s own integrity check and WordPress Site Health, and from the code itself. The CLI is out (XenForo’s cron still runs on the package’s
php). So are posix, pcntl, sockets, tokenizer, phar, xsl, calendar, ftp and gettext. ImageMagick is out too: Chevereto uses imagick only when it’s loaded and accepts GD otherwise, and XenForo and WordPress already default to GD, so all three now run on GD.The same policy now applies to the package build: Sury enables every installed extension for every SAPI, so the setup script disables the extra fourteen for php-fpm only. The CLI keeps all of them. On a server that also runs some other PHP application it leaves everything enabled and says why.
Three builds, three applications#
For the final comparison I wiped the test host back to its original packages, installed from scratch with all three applications, built and BOLT-optimised each mode through the setup script itself, and ran the same benchmark on the smaller standard data set:
All three builds passed the same functional, security and cache tests with all three applications, including Chevereto uploads on GD. One thing changed from the forum round: there, the BOLT profile came from XenForo traffic only, and XenForo gained 10 %. Here, one profile has to serve three applications, and XenForo’s share drops to about 6 %. If a server runs mainly one application, profile it on that application.
What I’m running#
The default stays the package: no toolchain on the server, and Sury’s security updates as soon as they’re published. For a busy XenForo forum, the clang build with BOLT is the one I’d pick. It’s the simplest of the compiled builds, keeps the package’s extensions, and has the best member-page numbers. The everything-from-source build stays available, so I can run this comparison again the next time OpenSSL, ICU or LLVM ships a major version.