Related posts
- 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.
- 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.
- 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.
The last post ended with one compiled php-fpm, built from upstream sources. This time I asked for the rest of the stack: Angie compiled the same way, glibc and libsystemd from source, smaller binaries, zlib-ng and Brotli, and the optimisations a distribution like CachyOS uses. Same Xeon E-2236 test host, fresh install with WordPress, XenForo and Chevereto, rotated benchmark rounds with the CPU clock pinned.
First, a security gap#
Ubuntu’s gcc turns on a set of hardening flags by default: stack protector, stack-clash probes,
_FORTIFY_SOURCE, CET landing pads, full RELRO. Upstream clang from apt.llvm.org turns on none of them. My compiled php-fpm had been built without all five, while the package it replaces has every one. It now passes them explicitly, for PHP and for every library. It costs 0.5-2 % before BOLT, and it stays.Angie from source#
The configuration I deploy uses a small part of Angie: HTTP/1, 2 and 3, TLS, realip, pre-compressed files, threads and Brotli. The package also carries mail, stream, ACME, WebDAV, mp4 streaming, SSI, gRPC and more. The source build has only what the configuration uses, OpenSSL, PCRE2 and zlib-ng compiled in, clang
-O3 -march=nativewith link-time optimisation:That was before BOLT. Built the way
setup.shnow builds it, with BOLT trained on 60 seconds of live traffic, the gains grew: WordPress guest pages +12.4 % and +11.5 %, XenForo +6.6 % and +4.4 %, Chevereto +6.1 % and +2.2 %. The binary is 13.5 MB and links nothing from Ubuntu but libc and libm. Logged-in pages didn’t change: PHP sets their pace.The binary has its own prefix, so it loads its own Brotli module from the same
modules-enabled/directory and reads/etc/angieunchanged. Every start runsangie -twith the build; a configuration that needs a module the build leaves out sends Angie back to the package.Gzip and Brotli#
Here Angie compresses the logged-in pages, so compression costs Angie CPU, not PHP CPU. Per XenForo member page:
zlib-ng cut the gzip cost by a fifth. The Angie build uses it. PHP gzips WordPress and Chevereto pages itself so the guest cache can store them compressed, and there zlib-ng in PHP gave +7.3 % on uncached Chevereto pages. Both builds use it now. A PHP Brotli extension wouldn’t help: none of the three applications compresses with it, and Angie already does.
PGO and AutoFDO, CachyOS style#
CachyOS ships packages built for x86-64-v3, v4 and Zen 4, plus profile-guided builds of selected packages. A binary distribution has to pick a level for everyone. My builds compile on the server they run on, with
-march=native, which covers the same ground and targets the exact CPU. The missing piece was profile feedback before BOLT. I tried both kinds:Instrumented PGO means building an instrumented binary, running the real applications on it, then rebuilding with the profile. It came out mixed: slower on XenForo threads, slightly faster elsewhere. BOLT already reorders the code from a profile of the same traffic, and most of what PGO adds overlaps with it. AutoFDO (sampled profiles, the approach CachyOS uses for its kernel) was slower everywhere. Neither is worth a second build and a training run, so the build stays LTO + BOLT.
A static glibc, and why it isn’t the default#
glibc 2.44 builds from source and links statically. The first problem: PHP’s huge code pages feature copies the program’s code to 2 MB pages by remapping it in place, and its remap function called
mmapandmemcpy. In a static binary those sit inside the code being remapped, so php-fpm crashed at startup. I patched the function to make its own system calls, and the binary started.It still served nothing. Every worker died with an assertion inside glibc. When a worker switches to the
www-datauser, glibc reads/etc/nsswitch.conf, which on Ubuntu listssystemdfor user lookups. glibc then loads Ubuntu’slibnss_systemd, which brings Ubuntu’s own glibc 2.43 into a program built with 2.44, and glibc refuses the mix. Making it work would mean shipping a whole private glibc runtime next to the binary. So glibc stays Ubuntu’s, like for every other package on the server, and the experiment code is gone.libsystemd was easier: PHP-FPM calls exactly one function from it,
sd_notify(), to tell systemd it’s ready. A 50-line static implementation of that protocol replaces the library. The compiled php-fpm now links only libc and libm.Smaller#
The binary that runs is now stripped: 48.4 MB with BOLT, down from 60.4 MB.
xmlreaderis gone (WordPress only uses it when libxml2 is broken). So are about twenty OpenSSL ciphers and protocols nobody here uses, and the linker drops unused code and folds identical functions. Of what remains, 10.4 MB is fileinfo’s built-in file-type database, which Chevereto and XenForo need for uploads.And the nginx repository’s CI#
The configuration repository had no CI since 4 November, when the old workflows went with the legacy files. It now installs Angie and runs
angie -tagainst the tree and all 14 site templates on every push.