Skip to content

Commit 873ede9

Browse files
AJenboclaude
andcommitted
Intern TypeCombinator results in turbo
This is ported over from PHPantom and how it handles types internally. The benefits aren't as big as I had hoped since part of it was already being performed in the majority of cases, but it still provided a very real 3% improvement to turbo's performance. Keep one canonical instance per distinct type value, keyed by the same 128-bit structural hash the memo already computes. Operations that reach the same value by different routes then hand back the same object, so the identity checks already on the hot paths — pt_types_identical_or_equal() on scope merges, `$a === $b` in TypeCombinator::doUnion() — decide comparisons that used to recurse into two graphs. Entries are borrowed exactly like the memo's: a weak map whose dtor releases the slot, so a canonical instance is retained only as long as some live scope holds it anyway. The open-addressing table is now a SlotTable shared by the memo and the intern table. Worth -3.1% user CPU on serial self-analysis (81.8 -> 79.2 s, three interleaved rotated rounds). Instrumented over src/Type + src/Analyser: 793k TypeCombinator calls, 184k memo misses, and 110,874 of those misses (60%) recompute a value that already exists — it reduces the lifetime type instance count by 60%, though since they're short-lived there's no real memory benefit to this; only 73k distinct values are ever canonical. Every substitution was verified equals()-true and describe()-identical. Peak RSS is unchanged — the deduplicated results are short-lived, and peak RSS is set by reflection and parser state instead. Interning is only sound because doUnion() now returns the same type for equal array operands as for identical ones; before that fix, making the operands identical changed inference at tests/PHPStan/Rules/Variables/data/bug-8113.php:47 and analysis output was no longer identical with the extension on and off. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 92fd7d0 commit 873ede9

3 files changed

Lines changed: 326 additions & 150 deletions

File tree

‎src/Type/TypeCombinatorCache.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
* This PHP implementation performs no caching at all — it delegates straight to
1111
* TypeCombinator. The native implementation memoizes each operation on a
1212
* structural key of its arguments; roughly 91% of the calls in a
13-
* self-analysis run repeat an argument tuple that was already computed.
13+
* self-analysis run repeat an argument tuple that was already computed. It also
14+
* interns the results, keeping one canonical instance per distinct type value,
15+
* so operations reaching the same value by different routes return the same
16+
* object and identity checks can stand in for structural comparison.
1417
*
15-
* The cache is scoped to a single container: memoization hands back shared Type
18+
* The cache is scoped to a single container: it hands back shared Type
1619
* instances, and Type objects carry a lazily resolved ClassReflection tied to the
1720
* container that created them.
1821
*

0 commit comments

Comments
 (0)