Skip to content

perf: Use local variables for parser tables in ParserAbstract::doParse() - #1182

Merged
nikic merged 1 commit into
nikic:masterfrom
Cayan:perf/parser-tables-in-locals
Oct 27, 2026
Merged

nikic merged 1 commit into
nikic:masterfrom
Cayan:perf/parser-tables-in-locals

Conversation

@Cayan

@Cayan Cayan commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

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 57 FETCH_OBJ_R in doParse().

This PR copies the tables into local variables before the loop, the same way $stackPos became a local in 3bbf8d8. The tables are written only in the constructor and $this->tokens only in parse(), 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:

PHP laravel/framework src (1,493 files) symfony/symfony src (9,968 files)
7.4.33 1214 → 1139 ms (-6.2%) 14073 → 13214 ms (-6.1%)
8.3.6 941 → 849 ms (-9.8%) 10393 → 9433 ms (-9.2%)
8.5.12 871 → 802 ms (-7.9%) 9659 → 8888 ms (-8.0%)

In a tool, the gain follows its share of time in doParse(). Psalm without cache on laravel src (20% of its CPU in doParse()) 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.php passes on the php-src 7.4.33 and 8.5.8 test trees.

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
nikic merged commit 1ee86c8 into nikic:master Oct 27, 2026
12 checks passed
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.

2 participants