Skip to content

Commit 7dbb0bd

Browse files
ondrejmirtesclaude
andcommitted
Build smaller turbo binaries
The shipped .so had grown to ~12 MB per platform and PHP version, 369 MB across the 38 binaries in phpstan/phpstan. Measured on Linux arm64 (GCC 11.4, make pgo), stripped: - strip the symbol tables in CI (make strip, before signing and before the tests, so they run the shipped binary): 12.0 MB raw -> 8.9 MB - no unwind tables: the extension never throws and never walks its own stack, yet -fno-exceptions alone still leaves .eh_frame (0.8 MB) - per-function sections with --gc-sections / -dead_strip - LTO under GCC: 3.9% faster on top of PGO and 15% smaller; clang's full LTO was 1.2% faster but 9% larger, so macOS builds stay without it - main.cpp supplies the terminate handler in the static-libstdc++ build, keeping libstdc++'s demangler out of the link With the pointer-free signatures and cold registration, the Linux .so goes from 12.0 MB to 8.3 MB raw and 6.3 MB stripped, 4.1% faster than today's CI build; macOS from 10.5 MB to 7.1 MB stripped at unchanged speed. -Os was measured and rejected: a third smaller than -O2, but 10.4% slower. PGO keeps its place: 4.0% faster on Linux, 3.6% on macOS. The sanitize target restores the unwind tables its stack traces need. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Qi7GRGMn9ZuY2jSc97wqBF
1 parent a1f52c6 commit 7dbb0bd

4 files changed

Lines changed: 102 additions & 12 deletions

File tree

‎.github/workflows/phar.yml‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,16 @@ jobs:
491491
run: |
492492
make pgo WARN_FLAGS="$(make -s print-warn-flags)" -j"$(getconf _NPROCESSORS_ONLN 2>/dev/null || nproc)"
493493
494+
# Before signing (stripping invalidates a signature) and before the
495+
# tests below, so they exercise the exact binary that ships. The
496+
# symbol tables are a quarter of the .so; see `strip` in the Makefile.
497+
- name: "Strip symbol tables"
498+
working-directory: "turbo-ext"
499+
run: |
500+
ls -l phpstan_turbo.so
501+
make strip
502+
ls -l phpstan_turbo.so
503+
494504
# On arm64 AMFI refuses to map unsigned code, so the binary must carry
495505
# at least an ad-hoc signature. The linker applies one itself, but
496506
# re-sign deliberately instead of relying on that. Quarantine (and thus
@@ -638,6 +648,15 @@ jobs:
638648
make pgo WARN_FLAGS="$(make -s print-warn-flags)" -j"$(getconf _NPROCESSORS_ONLN 2>/dev/null || nproc)"
639649
EOF
640650
651+
# before the tests below, so they exercise the exact binary that ships
652+
- name: "Strip symbol tables"
653+
run: |
654+
docker exec -i -w /work/turbo-ext alpine-build sh -e <<'EOF'
655+
ls -l phpstan_turbo.so
656+
make strip
657+
ls -l phpstan_turbo.so
658+
EOF
659+
641660
- name: "Verify the built extension reports the expected version"
642661
run: |
643662
docker exec -i alpine-build sh -e <<'EOF'

‎turbo-ext/CLAUDE.md‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,18 @@ each probed against the compiler in use, since the targets disagree about
253253
nearly all of them. **Adopt a codegen flag only with its own measurement**
254254
(interleaved A/B pairs on user CPU) and record the number beside it, the way
255255
the existing entries do; the full-workload runs have a ~±2% noise floor, so
256-
screen on a smaller target where the floor is ~0.35%. Measured and rejected
257-
so far: thin LTO (+1.76% slower), `-O3` (wash), PGO (no longer measurable),
258-
`-fstrict-flex-arrays=3` (traps on the engine's struct-hack), and
259-
`-D_GLIBCXX_ASSERTIONS` is absent only because it was never measured on a
260-
Linux host — the libc++ equivalent was.
256+
screen on a smaller target where the floor is ~0.35%. Measured on
257+
2026-09-23 with the extension really loaded (see the protocol below): PGO
258+
4.0% faster on Linux and 3.6% on macOS (kept); GCC LTO 3.9% faster on top of
259+
PGO and 15% smaller (adopted for GCC); clang full LTO 1.2% faster but 9%
260+
larger (not adopted); `-Os` 10.4% slower (rejected); dropping unwind tables,
261+
section GC, stripping, cold registration and the pointer-free generated
262+
signatures — size only, no measurable effect on time. Earlier entries —
263+
thin LTO (+1.76% slower), `-O3` (wash) — were measured loading the extension
264+
with `-d extension=`, which PHPStan's OPcache restart can silently drop;
265+
re-measure before relying on them. `-fstrict-flex-arrays=3` traps on the
266+
engine's struct-hack, and `-D_GLIBCXX_ASSERTIONS` is absent only because it
267+
was never measured on a Linux host — the libc++ equivalent was.
261268

262269
Static analysis and sanitizers, from the repository root (both gate in CI,
263270
`.github/workflows/lint.yml`):

‎turbo-ext/Makefile‎

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
PHP_CONFIG ?= php-config
1414

1515
CXX ?= c++
16+
UNAME_S := $(shell uname -s)
17+
18+
# The extension never throws and never walks its own stack (the engine
19+
# unwinds with longjmp), so it needs no unwind tables: -fno-exceptions alone
20+
# still leaves .eh_frame for every function, 0.8 MB of the Linux .so. The
21+
# section flags let the linker drop whatever nothing references.
22+
SIZE_FLAGS := -fno-asynchronous-unwind-tables -fno-unwind-tables -ffunction-sections -fdata-sections
1623
# The strict set the CI compile legs build with, defined once here and read
1724
# by the workflows (`make -s print-warn-flags`) instead of being spelled out
1825
# in each of them. The three -Wno- exemptions cover third-party macro
@@ -47,14 +54,21 @@ STRICT_WARN_FLAGS := -Wall -Wextra -Werror \
4754
# warn where the versions CI pins do not, and -Werror would turn that into a
4855
# build failure for them.
4956
WARN_FLAGS ?= -Wall
57+
# -O2, measured against -Os in the CI image (GCC 11.4, arm64, interleaved
58+
# A/B, user CPU with the extension loaded): -Os with LTO takes the stripped
59+
# .so from 7.5 MB to 5.0 MB but makes the analysis 10.4% slower (6.7% with
60+
# PGO), so the size is not worth it.
5061
# ZEND_ENABLE_STATIC_TSRMLS_CACHE: on ZTS builds EG()/CG() go through the
5162
# per-thread cache main.cpp defines instead of a ts_resource lookup per
5263
# access; a no-op on NTS builds.
5364
CXXFLAGS := $(WARN_FLAGS) -O2 -std=c++17 -fPIC \
5465
-DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 \
5566
`$(PHP_CONFIG) --includes`
5667

57-
# Profile-guided optimisation. PGO_FLAGS is empty for a plain build; `make
68+
# Profile-guided optimisation, measured 2026-09-23 with the extension
69+
# loaded (interleaved A/B, user CPU, n=11): 4.0% faster on Linux (GCC 11.4,
70+
# t=-6.3), 3.6% on macOS (clang, t=-14.1), for a stripped .so within 1-2%
71+
# of the same size. PGO_FLAGS is empty for a plain build; `make
5872
# pgo` drives the two instrumented/optimised builds below with the flags of
5973
# the compiler in use (clang writes .profraw files merged by llvm-profdata,
6074
# GCC writes .gcda files next to the objects and reads them back from
@@ -71,6 +85,16 @@ PGO_GEN_FLAGS := -fprofile-generate -fprofile-update=atomic
7185
PGO_USE_FLAGS := -fprofile-use -fprofile-correction -Wno-missing-profile
7286
endif
7387

88+
# Link-time optimisation, with GCC (every Linux leg). Measured in the CI
89+
# image (GCC 11.4, arm64, `make pgo` on both sides, interleaved A/B, user
90+
# CPU, n=11): 3.9% faster (t=-9.4) and the stripped .so 15% smaller
91+
# (7.4 -> 6.3 MB). Clang goes the other way on size: full LTO on macOS was
92+
# 1.2% faster (t=-3.6) but made the stripped .so 9% larger (7.1 -> 7.7 MB),
93+
# so clang builds stay without it. On the compile and the link line both.
94+
ifneq ($(CXX_IS_CLANG),1)
95+
LTO_FLAGS := -flto=auto
96+
endif
97+
7498
# The extension version is the short SHA of the last commit touching
7599
# turbo-ext/src/ (the same computation the CI version job enforces against
76100
# TurboExtensionEnabler::EXPECTED_EXTENSION_VERSION),
@@ -98,11 +122,13 @@ CXXFLAGS += -DPHPSTANTURBO_VERSION='"$(PHPSTANTURBO_VERSION)"'
98122
# GNU ld allows them in shared objects by default, Darwin needs the flag.
99123
# On Linux, fold libstdc++/libgcc into the .so statically so a distributed
100124
# binary does not depend on the build host's GLIBCXX_*/GCC_* symbol versions.
101-
UNAME_S := $(shell uname -s)
102125
ifeq ($(UNAME_S),Darwin)
103-
LINK_FLAGS := -undefined dynamic_lookup
126+
LINK_FLAGS := -undefined dynamic_lookup -Wl,-dead_strip
104127
else
105-
LINK_FLAGS := -static-libstdc++ -static-libgcc
128+
LINK_FLAGS := -static-libstdc++ -static-libgcc -Wl,--gc-sections
129+
# main.cpp then supplies the terminate handler, keeping libstdc++'s (and its
130+
# demangler) out of the link
131+
CXXFLAGS += -DPHPSTANTURBO_STATIC_LIBSTDCXX
106132
# the loader resolves and write-protects the GOT before handing control over,
107133
# and the stack is never executable
108134
LINK_FLAGS += -Wl,-z,relro,-z,now -Wl,-z,noexecstack
@@ -127,6 +153,7 @@ endif
127153
# (config.m4's phpize path does not carry these, the same way it does not
128154
# carry the strict warnings: the Makefile is the primary build.)
129155
CXXFLAGS += -fvisibility=hidden -fvisibility-inlines-hidden -fno-exceptions -fno-rtti
156+
CXXFLAGS += $(SIZE_FLAGS) $(LTO_FLAGS)
130157

131158
# Hardening. Every flag is probed against the compiler actually in use rather
132159
# than assumed, because the targets disagree: the CI floor is GCC 11.4, which
@@ -183,12 +210,26 @@ SOURCES := $(wildcard src/*.cpp) $(wildcard src/parser/*.cpp)
183210
OBJECTS := $(SOURCES:.cpp=.o)
184211

185212
phpstan_turbo.so: $(OBJECTS)
186-
$(CXX) `$(PHP_CONFIG) --ldflags` -shared $(LINK_FLAGS) $(PGO_FLAGS) -o $@ $(OBJECTS)
213+
$(CXX) `$(PHP_CONFIG) --ldflags` -shared $(LTO_FLAGS) $(LINK_FLAGS) $(PGO_FLAGS) -o $@ $(OBJECTS)
187214
@# a shared object links with undefined symbols allowed (the engine's are
188215
@# resolved at load time), so a helper declared but never defined only
189216
@# surfaces as a jump to NULL at run time — fail the build instead
190217
@if nm -u $@ | grep -E 'pt_[a-z_]+|phpstanturbo' > /dev/null; then echo "undefined extension symbols in $@:"; nm -u $@ | grep -E 'pt_[a-z_]+|phpstanturbo'; rm -f $@; exit 1; fi
191218

219+
# The distributed binaries ship without symbol tables: a quarter of the .so
220+
# (2.4 MB on Linux, 3.2 MB on macOS) that the loader never reads, since the
221+
# only symbol PHP looks up is get_module, which stays in the dynamic table.
222+
# Local builds keep them — sample and perf name frames from them. Not a
223+
# dependency of phpstan_turbo.so on purpose: this runs after `make pgo`, and
224+
# must never rebuild what that produced with other flags.
225+
strip:
226+
@test -f phpstan_turbo.so || { echo "phpstan_turbo.so is missing — build it first"; exit 1; }
227+
ifeq ($(UNAME_S),Darwin)
228+
strip -x phpstan_turbo.so
229+
else
230+
strip --strip-unneeded phpstan_turbo.so
231+
endif
232+
192233
# every object depends on every header: the op tables and the trait helper
193234
# declarations live in TypeOps.h / TypeTraits.h, and a stale object built
194235
# against an older layout jumps into the wrong entry at run time
@@ -302,7 +343,9 @@ lint: compile_commands.json
302343
# this build — a bounds violation would abort with no diagnostic, the exact
303344
# opposite of what a sanitizer run is for. Probed, because the spelling is
304345
# not available on every compiler this builds with.
305-
SANITIZE_FLAGS ?= -fsanitize=undefined $(call cxx-supports,-fno-sanitize-trap=all) -fno-omit-frame-pointer
346+
# The unwind tables SIZE_FLAGS drops come back here for the same reason:
347+
# print_stacktrace unwinds through them.
348+
SANITIZE_FLAGS ?= -fsanitize=undefined $(call cxx-supports,-fno-sanitize-trap=all) -fno-omit-frame-pointer -funwind-tables -fasynchronous-unwind-tables
306349
SANITIZE_TESTS ?= smoke arena-smoke signature-parity parser-corpus
307350

308351
sanitize:
@@ -316,4 +359,4 @@ sanitize:
316359
done
317360
$(MAKE) clean
318361

319-
.PHONY: clean lint pgo pgo-clean print-clang-tidy-version print-warn-flags sanitize FORCE
362+
.PHONY: clean lint pgo pgo-clean print-clang-tidy-version print-warn-flags sanitize strip FORCE

‎turbo-ext/src/main.cpp‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,27 @@
1818
#include <unistd.h>
1919
#endif
2020

21+
/* The Makefile's Linux build folds libstdc++ in statically, and with it the
22+
* default terminate handler, which demangles the type of the exception that
23+
* escaped — 45 KB of demangler for a path this code reaches only when a
24+
* standard container fails an internal check (it never throws or catches
25+
* itself). This definition keeps the archive's handler, and so the
26+
* demangler, out of the link. Only for the static link: against a shared
27+
* libstdc++ (the phpize build) the handler is the process's, not ours. The
28+
* signature must match <exception>'s declaration exactly — GCC rejects a
29+
* redeclaration adding [[noreturn]] or hidden visibility. */
30+
#ifdef PHPSTANTURBO_STATIC_LIBSTDCXX
31+
#include <cstdio>
32+
#include <cstdlib>
33+
namespace __gnu_cxx {
34+
void __verbose_terminate_handler()
35+
{
36+
fputs("phpstan_turbo: terminate called (a C++ standard library check failed)\n", stderr);
37+
abort();
38+
}
39+
} // namespace __gnu_cxx
40+
#endif
41+
2142
/* The short SHA of the last commit touching the watched set: baked from git
2243
* by the Makefile (quoted string passed directly), or from the VERSION.txt
2344
* the subsplit workflow commits into phpstan/turbo-ext; "dev" with neither,

0 commit comments

Comments
 (0)