Skip to content

Do less work per reduction in ParserAbstract::doParse() - #1191

Open
ondrejmirtes wants to merge 1 commit into
nikic:masterfrom
ondrejmirtes:perf/doparse-fewer-stack-writes
Open

ondrejmirtes wants to merge 1 commit into
nikic:masterfrom
ondrejmirtes:perf/doparse-fewer-stack-writes

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Contributor

ParserAbstract::doParse() does the same stack bookkeeping for every reduction. It saves the end position of the rule's last symbol, pops the rule's symbols, and pushes the nonterminal by writing its state, its value, and its end position, plus its start position for empty rules. For a rule without an action, it first copies the value of the rule's first symbol into $this->semValue.

Most reductions are of rules with a single symbol, and most of those have no action: they only pass a value up, as in expr: variable. Of the 1.64M reductions when parsing laravel/framework's src, 41 % are of one-symbol rules without an action and another 25 % of one-symbol rules with an action. On WordPress the shares are 47 % and 25 %. In a one-symbol rule, the nonterminal takes the stack slot of its only symbol. That slot already holds the right start and end positions and, when the rule has no action, the right value. This PR skips those writes:

  • A one-symbol rule writes only the new state, and the value when it has an action.
  • A rule without an action copies the value of its first symbol only when it has more than one symbol.
  • Other rules work as before. The end position of the last symbol is now read after the goto lookup instead of before it, at the same stack index.

This leaves $this->semValue stale after a one-symbol rule without an action. Nothing reads it there:

  • Every reduce callback in Php7 and Php8 assigns $self->semValue before it reads it.
  • The accept step returns $this->semValue right after reducing rule 1 (start), which has an action in both grammars.

The action and goto check tables are lists, and their entries are symbols or nonterminals, so they are never negative. That makes ($check[$idx] ?? -1) === $x give the same result as $idx >= 0 && $idx < $tableSize && $check[$idx] === $x, with one array fetch instead of two comparisons and a fetch. This applies to both action table lookups and the goto lookup.

How it was measured

Parse every file of a codebase and resolve its names. I ran it on two applications:

  • laravel/framework v12.0.0, src/: 1,410 files.
  • WordPress 6.8.1: 1,650 files.
Application master this PR Change (95 % CI)
laravel/framework v12.0.0 src 1052.7 ± 34.2 ms 985.3 ± 26.2 ms −6.4 % (−7.9 % to −4.9 %)
WordPress 6.8.1 3616.5 ± 107.1 ms 3367.0 ± 120.7 ms −6.9 % (−8.5 % to −5.3 %)

Reductions of a rule with one symbol are the most common ones, and most
of them have no action and only pass the value of that symbol through.
The nonterminal takes the place of the symbol on the stacks, with the
same start position, end position, and value, so these reductions now
only update the state stack. Rules of one symbol with an action still
store the new value, but no longer rewrite the end position.

The action and goto check tables are lists, so a lookup with ?? finds no
entry for an index outside of them, which replaces the two bounds checks
of each lookup.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1da28-50d0-74d0-b757-78ea2de4dd33
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.

1 participant