Skip to content

Typed SplFixedArray<Class> access and buffer foreach, const-list in_array chain, #159 - #166

Merged
blacktrs merged 13 commits into
mainfrom
csfix-traps
Oct 11, 2026
Merged

blacktrs merged 13 commits into
mainfrom
csfix-traps

Conversation

@blacktrs

Copy link
Copy Markdown
Contributor

What

Five compiler changes found while profiling a real application (a PHP-CS-Fixer fork, 276 files / 1.54M tokens). Together they take its @PSR12 run from 187G to 100G retired instructions with the application's own changes, of which the compiler share is the typed SplFixedArray access and the buffer foreach (about 17%).

  1. SplFixedArray is generic (@template T, offsetGet returns T). /** @extends SplFixedArray<Token> */ class Tokens makes $tokens[$i] an obj<Token>: the emitter strips the cell word to the pointer, so the property read and the method call on it are direct instead of a name lookup on a cell. genericReturnType now starts from a class that has no parameters of its own but binds its parent's.
  2. foreach over a plain SplFixedArray walks the native buffer like a Vec (20 ns -> 4.4 ns per element). When the array is bound to a class the loop variable is that class too (Foreach_::$fixedBuf; inference, ownership and emitter agree on one condition). A subclass overriding getIterator, a by-reference loop and a generator body keep the protocol path.
  3. Strict in_array over a literal int or string list compiles to a chain of compares, which LLVM folds to a switch (10 ns -> 1 ns on a 30-id list). An int list also takes a ?int needle (guarded by is_int). Composes with perf(array): native scans for in_array and implode over typed vecs #162: the chain runs first, the native scan covers what it does not.
  4. Fix SIGSEGV when a static ??= cache holds an array whose elements are a @phpstan-type alias #159: a static ??= cache of an array whose elements are a @phpstan-type alias crashed (SIGSEGV). The shape-field read typed an erased field unknown (a raw word) where an element read already says cell.
  5. __preg_test in the stdlib: a does-it-match search that builds no $matches (3049 -> 1434 instructions on a one-line check). Nothing in src/ uses it yet.

Not done here

Tests

New cases: foreach_splfixedarray_native_loop, splfixedarray_bound_class_elements, splfixedarray_foreach_bound_class, in_array_strict_const_list, static_cache_phpstan_alias_elems (promoted from the #159 repro). AOT suite after rebasing onto main: 1784 passed, 0 failed (2 skipped). The in_array case checks semantics (it would also pass without the pass); that the chain is emitted shows in dump-mir as __mc_fuse_inarraylit_*.

Bootstrap

No src/ code uses a feature of this change, so BOOTSTRAP_VERSION is unaffected.

Fixes #159

… instead of driving __McFixedArrayIterator

A loop over Tokens ran at 20 ns per element through the iterator protocol (a boxed cell per step); the buffer loop is 4.4 ns. A subclass that overrides getIterator, and a by-reference foreach, keep the protocol path.
Repro for the by-reference gap: #163.
… of compares instead of a loop over a heap vec

php-cs-fixer's TokensAnalyzer::isBinaryOperator scans 30 token ids per token: 10 ns per call became 1 ns in isolation. An int list takes an int or a cell needle (guarded by is_int); a string list takes a string needle.
SplFixedArray is generic (@template T, offsetGet returns T), so `/** @extends SplFixedArray<Token> */ class Tokens` makes $tokens[$i] an obj<Token>: the emitter strips the cell word to the pointer, and the property read and the method call on it are direct instead of a name lookup on a cell.
php-cs-fixer @psr12 over 276 files: 111.5G -> 103.6G retired instructions.
The foreach over a SplFixedArray bound to a class (`@extends SplFixedArray<Token>`) types its loop variable as that class: InferTypes sets Foreach_::$fixedBuf, the ownership pass binds the element's +1 as the class, and the emitter unboxes the cell word to the pointer.
…the string untouched; strrpos honours its offset
…placements skip expansion, no-match preg_replace returns the subject
@blacktrs
blacktrs merged commit 2c4b8e3 into main Oct 11, 2026
8 checks passed
@blacktrs
blacktrs deleted the csfix-traps branch October 11, 2026 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SIGSEGV when a static ??= cache holds an array whose elements are a @phpstan-type alias

1 participant