Repository navigation
Do less work per reduction in ParserAbstract::doParse() - #1191
Open
ondrejmirtes wants to merge 1 commit into
Open
ondrejmirtes wants to merge 1 commit into
ondrejmirtes wants to merge 1 commit into
Conversation
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
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()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'ssrc, 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:This leaves
$this->semValuestale after a one-symbol rule without an action. Nothing reads it there:Php7andPhp8assigns$self->semValuebefore it reads it.$this->semValueright 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) === $xgive 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:
src/: 1,410 files.