Related posts
- Plain, BOLT, PGO or both on an AMD EPYC (Zen 1)
A second run on the Zen 1 machine, with four builds of PHP 8.6.0 and the first run's two caveats fixed. PGO alone was fastest at +6.1 to +8.4 % and won all 20 comparisons. BOLT from plain samples gave +4.3 to +5.0 % on its own but made PGO slower, and the CLI still lost 4.8 % on bench.php, so the CLI gets built without the profile.
- BOLT, PGO or both for PHP on a Xeon
I built PHP 8.5.13 four ways on the Xeon test host: plain, with BOLT, with clang PGO, and with both. BOLT and PGO each give about +7 % on uncached XenForo pages, but PGO on top of the BOLT build my setup script already ships adds only -1 to +2.4 %, so it stays out where BOLT runs.
- Compiling Angie too, and the hardening my compiled PHP was missing
Angie compiled from source served cached pages 4-6 % faster; zlib-ng cut its gzip cost by a fifth. PGO, AutoFDO and a static glibc were measured; and the from-source php-fpm got back the stack protector and RELRO that upstream clang leaves off.
My setup script compiles PHP with clang and LTO, then runs BOLT over the finished binary. BOLT reorders the machine code by how it ran under live traffic, so the hot paths sit together. On Intel that step is worth about 7-9 % to an uncached XenForo page.
BOLT needs a profile with branch history, and it gets that from the CPU’s last branch records (LBR). Intel has had them for years. AMD added full branch records with Zen 4; Zen 3 has only a more limited branch-sampling feature, and Zen 1 and 2 have none. Where the script finds no branch samples, it records
bolt=nolbrand skips the step. The PHP it installs there has no profile-based layout optimisation at all.That left an obvious question: profile-guided optimisation (PGO) gets its profile from an instrumented build, not from CPU counters, so it runs on any CPU. Does it give back what BOLT can’t?
On an AMD EPYC 7401P (Zen 1) it does: +6.2 to +9.1 % requests per second, p99 down 6-9 %, and it won every tier in every round. There’s one catch, and it matters for cron jobs.
Not the first PGO result on this blog#
PGO has a mixed record here. Turning everything on with GCC made PHP 8.5 5-10 % slower. That was GCC’s PGO, measured against a distribution package. The three-builds post had clang PGO arms that did gain, but ThinLTO plus BOLT beat them. This run uses clang’s IR-level PGO on top of the script’s real production build, with the same flags and extensions. On the Xeon E-2236 test host the same method was worth about the same as BOLT (+5 to +7 %), but BOLT already takes most of that there, so PGO on top added only -1.1 to +2.4 % and didn’t become a default. The interesting machine is the one where BOLT can’t run.
How it was measured#
The EPYC is a shared development server that other people use. I didn’t install anything on it system-wide. Everything ran inside one temporary directory with its own private copy of the stack:
wrkwith 48 connectionsTwo arms: A is the plain production-style build, I is the same build plus
-fprofile-usefrom a profile trained on XenForo traffic on that machine. Five rounds, arm order rotated each round, median with min-max. I couldn’t pin the CPU clock on a machine I don’t own, so the per-round numbers move more than on the Xeon. To check the neighbours weren’t skewing it, I logged their CPUs during every tier: median 0.05 % busy, maximum 1.8 %. No request returned anything but 2xx.When it finished I deleted the directory and ran the same audit as before the test. The machine was unchanged.
The numbers#
Uncached XenForo pages, the ones that run PHP:
The ranges don’t overlap on any tier, though on guest home they miss by less than 1 req/s. Round by round, PGO won all 20 comparisons. The smallest was +0.2 % (guest home, round 1) and the largest +15.7 %, which is what an unpinned clock does to single rounds. The medians are the number to read.
The profile itself shows where a XenForo page spends its time. Of 17,027 functions in the binary, 4,025 ran at all during training. The hottest were
strtrwith an array, PHP’s allocator (_emalloc,_efree), hash lookups,stripos, lower-casing, JSON parsing and HTML escaping. That’s mostly template rendering and string work, plus a few JIT and VM helpers, not the interpreter loop. With the tracing JIT on, PHP’s own code runs as machine code the JIT generated. What C compiler flags can still reach are these runtime helpers, and that’s exactly what PGO rearranged.The catch: PHP without the JIT got slower#
The training ran with the JIT on, like production. That left the interpreter’s VM handlers cold in the profile, and the optimiser treated them as code that hardly runs. PHP’s own CPU benchmarks, run with
php -n(nophp.ini, so no OPcache and no JIT), on one CPU, three runs each:Zend/bench.phpZend/micro_bench.phpphp-fpm doesn’t care. The PHP CLI does: it runs with OPcache off, so XenForo’s cron jobs (
xf:run-jobs), imports and rebuilds would get slower with this profile. That’s a regression for the CLI, and my rule for a new default is at least 2 % faster somewhere and slower nowhere. So this isn’t shipped as it stands.What I didn’t measure#
What’s next#
A follow-up run is already underway on the same machine and the same way. It fixes both problems: training uses its own pages, kept apart from the measured ones, and the CLI is trained too, so the interpreter’s handlers aren’t cold. It also adds sample-based BOLT as a third arm, to see whether BOLT without LBR adds anything to PGO. That gets its own post.
What it means#
On CPUs without LBR (such as AMD Zen 1 and 2), BOLT can’t run, so PGO is the only layout optimisation available, and it’s worth roughly +6-9 % to XenForo. That clears the 2 % bar for a default by a wide margin. Once a profile no longer slows the CLI down, PGO becomes a candidate default for those CPUs. On Intel and Zen 4 or newer, BOLT already does most of this job, and the script stays as it is.
The numbers, with the Intel PGO runs beside them, are in
tests/stress-site/RESULTS.mdin ubuntu-setup-script.