Repository navigation
perf: Use local variables for parser tables in ParserAbstract::doParse() - #1182
Merged
Merged
Conversation
doParse() reads the action and goto tables on every iteration of its loop, and each read was a property fetch. Copying the tables into local variables before the loop makes each read a local variable access, as $stackPos already is. The tables are written only in the constructor and $tokens only in parse(), so the copies cannot go stale. Parsing laravel/framework and symfony/symfony src is 6% faster on PHP 7.4 and 8% to 10% faster on PHP 8.3 and 8.5, with identical ASTs (parse() including lexing, median of 5 alternated runs per side on one pinned core, opcache on, JIT off).
nikic
approved these changes
Oct 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ParserAbstract::doParse()reads the action and goto tables on every iteration of its loop, and each of those reads is a property fetch ($this->actionBase[...],$this->actionCheck[...],$this->action[...], ...). In the opcode dump that is 57FETCH_OBJ_RindoParse().This PR copies the tables into local variables before the loop, the same way
$stackPosbecame a local in 3bbf8d8. The tables are written only in the constructor and$this->tokensonly inparse(), so the copies cannot go stale. State the reduce callbacks use (tokenPos,semStack, the token start and end stacks,errorState) stays on the object.Parse time (median of 5 alternated runs per side, one pinned core, opcache on, JIT off). The serialized ASTs are identical on both sides:
src(1,493 files)src(9,968 files)In a tool, the gain follows its share of time in
doParse(). Psalm without cache on laravel src (20% of its CPU indoParse()) takes 1.8% less CPU (76.4 → 75.0 s). PHPStan and Rector spend under 5% there, so their change stays within run-to-run noise.The test suite passes on PHP 7.4 and 8.2, and
test_old/run.phppasses on the php-src 7.4.33 and 8.5.8 test trees.