Skip to content

Commit bf43a04

Browse files
committed
Compare selectStatement by rule id instead of by name
Minor cleanup in parse_recursive(): cache the selectStatement rule id once and compare integers on every call instead of re-comparing the 'selectStatement' string against every rule's name. Also drops the $rules instance cache from the parser, which the hot path no longer touches now that branch sequences are embedded in the selector.
1 parent a786fe2 commit bf43a04

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

packages/mysql-on-sqlite/src/parser/class-wp-parser.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ class WP_Parser {
1616

1717
// Grammar data cached as instance fields so the hot path avoids an extra
1818
// property hop via $this->grammar on every recursive call.
19-
private $rules;
2019
private $rule_names;
2120
private $fragment_ids;
2221
private $branches_for_token;
2322
private $nullable_branches;
2423
private $highest_terminal_id;
24+
private $select_statement_rule_id;
2525

2626
public function __construct( WP_Parser_Grammar $grammar, array $tokens ) {
2727
$this->grammar = $grammar;
@@ -48,12 +48,16 @@ public function __construct( WP_Parser_Grammar $grammar, array $tokens ) {
4848
$tokens[] = new WP_Parser_Token( WP_Parser_Grammar::EMPTY_RULE_ID, 0, 0, '' );
4949
$this->tokens = $tokens;
5050
$this->position = 0;
51-
$this->rules = $grammar->rules;
5251
$this->rule_names = $grammar->rule_names;
5352
$this->fragment_ids = $grammar->fragment_ids;
5453
$this->branches_for_token = $grammar->branches_for_token;
5554
$this->nullable_branches = $grammar->nullable_branches;
5655
$this->highest_terminal_id = $grammar->highest_terminal_id;
56+
57+
// The INTO negative-lookahead only fires for selectStatement. Cache
58+
// the rule id so the per-call check is an int compare instead of a
59+
// string compare.
60+
$this->select_statement_rule_id = $grammar->get_or_cache_rule_id( 'selectStatement' );
5761
}
5862

5963
public function parse() {
@@ -87,9 +91,8 @@ private function parse_recursive( $rule_id ) {
8791
}
8892

8993
$highest_terminal_id = $this->highest_terminal_id;
90-
$rule_name = $this->rule_names[ $rule_id ];
9194
$is_fragment = isset( $this->fragment_ids[ $rule_id ] );
92-
$is_select_statement = 'selectStatement' === $rule_name;
95+
$is_select_statement = $rule_id === $this->select_statement_rule_id;
9396
$branch_matches = false;
9497
$children = array();
9598
foreach ( $candidate_branches as $branch ) {
@@ -167,6 +170,6 @@ private function parse_recursive( $rule_id ) {
167170
return $children;
168171
}
169172

170-
return new WP_Parser_Node( $rule_id, $rule_name, $children );
173+
return new WP_Parser_Node( $rule_id, $this->rule_names[ $rule_id ], $children );
171174
}
172175
}

0 commit comments

Comments
 (0)