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.
- PGO where BOLT cannot run: PHP 8.6 on an AMD EPYC (Zen 1)
My setup script optimises PHP's code layout with BOLT, but BOLT needs branch records that older AMD CPUs such as Zen 1 don't have, so on those machines PHP gets no layout optimisation at all. Profile-guided optimisation trained on XenForo traffic filled the gap: +6 to +9 % requests per second on an EPYC 7401P, with one catch for the PHP CLI.
- Three php-fpm builds side by side: Sury, clang + BOLT, and everything from source
My earlier compiled-PHP tests found nothing. Two flaws in my build harness were hiding the gain: GCC was built without its global registers, and BOLT was never tried. With both fixed, a BOLT-optimised php-fpm is 5-10 % faster on logged-in XenForo pages. Building every library from upstream as well added nothing measurable.
Nine days ago, turning everything on made PHP slower: GCC with LTO and profile-guided optimisation lost 5-10 % against the distribution package. Since then my setup script has moved to a clang build of PHP with ThinLTO,
-march=nativeand mimalloc, and it runs BOLT on that binary once the site has served real traffic. That build is the one the three-builds post found 5-10 % faster than the package.PGO deserved a second try on the new build. The three-builds post did have clang PGO arms, but as alternatives to ThinLTO rather than on top of it, and ThinLTO plus BOLT beat PGO plus BOLT there. PGO was never tried on the build the script actually ships, and clang’s PGO is a different implementation from GCC’s. So I compiled the same PHP four ways and measured all four in one run.
Short version: BOLT and PGO each give about +7 % over the plain build, and together +7 to +10 %. Against the BOLT build my script already ships, PGO adds -1.1 to +2.4 %. That isn’t enough to make it a default, so it stays out on CPUs where BOLT runs.
The four builds#
All four are PHP 8.5.13 from the same sources, with the same configure line, extensions and flags as the build the script installs.
-march=native, mimalloc: the script’s build before BOLT runsperfbranch samples (LBR) taken under load-fprofile-useBOLT and PGO both work on code layout: which branches are hot, which functions sit next to each other, what moves out of the way. They get there differently. PGO counts every edge in an instrumented build and feeds the counts back into the compiler. BOLT samples the finished binary with the CPU’s own branch records and rewrites the machine code. I expected a big overlap between them, and that’s what the numbers show.
How I measured#
The test host is a Xeon E-2236 (6 cores, 12 threads) with the clock pinned at 3.6 GHz, so turbo fade can’t favour whichever arm runs first. It serves XenForo 2.3.14 on synthetic, seeded data (20,000 users, 10,000 threads, 100,000 posts), with 12 PHP-FPM workers and the tracing JIT on. Each tier ran for 10 s at 100 connections. There were 5 rounds, with the order of the four builds rotated each round, and the switch between builds was a unit drop-in plus a PHP-FPM restart. The tables show the median of the five rounds, with the slowest and fastest round in brackets.
The numbers#
Requests per second. The percentages compare each build with the plain one.
Median p99 latency moved the same way. On the member thread, for example, it went 702 ms plain, 649 ms with BOLT, 644 ms with PGO and 635 ms with both.
The cached guest tiers aren’t in the table. They never reach PHP: nginx serves them from its cache at around 145,000 and 106,000 requests per second. The four builds landed between -2.5 % and +0.9 % there, with overlapping ranges, and that’s nginx noise.
The question that matters for the script is the last column against the second:
The two earlier, partial runs agree with this. The first compared PGO alone with the plain and BOLT builds: two clean rounds, +4.9 to +6.7 % for PGO and +6.5 to +7.6 % for BOLT. I threw away its other three rounds because they overlapped unrelated test runs on the same host. The second run put PGO + BOLT against the shipped BOLT build for 5 rotated rounds and found +0.9 to +2.0 %.
Why PGO isn’t added#
The rule for a default in my script: at least +2 % on some tier, no tier slower, measured on both Intel and AMD. PGO + BOLT misses on two counts. Member home is 1.1 % slower, and the best tier’s range overlaps the shipped build’s. The earlier run topped out at exactly +2.0 %. BOLT has already taken most of what PGO offers, because both of them are fixing the same layout problem.
The cost side is also real. Every PHP refresh would need an instrumented build, a training run under load with a single worker (see below), and a rebuild. That’s a second full compile and minutes of synthetic traffic on a production server, for a gain inside the rule’s margin.
There’s an exception. On CPUs where BOLT can’t run, PGO is the only layout optimisation. Older AMD CPUs have no LBR, so the script records
bolt=nolbrand ships the plain build there. That case has its own measurement and its own caveats: PGO where BOLT cannot run.What tripped me up#
LLVM’s PGO and php-fpm don’t get along out of the box. Four traps, in the order I hit them:
-fprofile-generatelinks clang’s profile runtime library. The LLVM packages from apt.llvm.org ship it in its ownlibclang-rt-<version>-devpackage, and without it the instrumented build fails at link time.%mmerging corrupted the profile with 12 workers. LLVM’s%mpattern makes processes merge into a shared file in place. With twelve busy workers exiting together, the merged profile came out corrupt. Training withpm.max_children=1produced a clean profile. One worker serves less traffic, but the profile records edge frequencies, not throughput, so that doesn’t matter.-fprofile-userebuild must therefore compile in the same directory, or every static function’s profile silently goes missing. A fixed work directory fixed it.What changed since the GCC attempt#
The GCC attempt and this one disagree: there PGO cost 5-10 %, here it gains about 7 % over the same build without it. Several things differ, and I didn’t isolate which one decided it:
-fprofile-generate-falign-functions=32,-fno-semantic-interpositionThe harness bug alone, found in the three-builds post, handicapped every GCC arm in that round. The old post’s main lesson still holds: train with the JIT in the state you ship and measure the same way.
What I didn’t measure#
The script stays as it is: BOLT where the CPU has branch records, the plain ThinLTO build where it doesn’t. PGO for the CPUs without LBR is the open question.