Skip to content

Commit 9b042c9

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 f726c2e commit 9b042c9

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ class WP_Parser_Grammar {
6565
*/
6666
public $start_rule_id;
6767

68+
/**
69+
* Cached id of the selectStatement rule, populated lazily on first parse.
70+
*
71+
* @var int|null
72+
*/
73+
public $select_statement_rule_id;
74+
6875
public function __construct( array $rules ) {
6976
$this->inflate( $rules );
7077
}

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

Lines changed: 11 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;
@@ -34,12 +34,19 @@ public function __construct( WP_Parser_Grammar $grammar, array $tokens ) {
3434
$tokens[] = new WP_Parser_Token( WP_Parser_Grammar::EMPTY_RULE_ID, 0, 0, '' );
3535
$this->tokens = $tokens;
3636
$this->position = 0;
37-
$this->rules = $grammar->rules;
3837
$this->rule_names = $grammar->rule_names;
3938
$this->fragment_ids = $grammar->fragment_ids ?? array();
4039
$this->branches_for_token = $grammar->branches_for_token;
4140
$this->nullable_branches = $grammar->nullable_branches;
4241
$this->highest_terminal_id = $grammar->highest_terminal_id;
42+
43+
// The INTO negative-lookahead only fires for selectStatement. Cache
44+
// the rule id so the per-call check is an int compare instead of a
45+
// string compare.
46+
if ( null === $grammar->select_statement_rule_id ) {
47+
$grammar->select_statement_rule_id = $grammar->get_rule_id( 'selectStatement' );
48+
}
49+
$this->select_statement_rule_id = $grammar->select_statement_rule_id;
4350
}
4451

4552
public function parse() {
@@ -80,9 +87,8 @@ private function parse_recursive( $rule_id ) {
8087
}
8188

8289
$highest_terminal_id = $this->highest_terminal_id;
83-
$rule_name = $this->rule_names[ $rule_id ];
8490
$is_fragment = isset( $this->fragment_ids[ $rule_id ] );
85-
$is_select_statement = 'selectStatement' === $rule_name;
91+
$is_select_statement = $rule_id === $this->select_statement_rule_id;
8692
$branch_matches = false;
8793
$children = array();
8894
foreach ( $candidate_branches as $branch ) {
@@ -160,6 +166,6 @@ private function parse_recursive( $rule_id ) {
160166
return $children;
161167
}
162168

163-
return new WP_Parser_Node( $rule_id, $rule_name, $children );
169+
return new WP_Parser_Node( $rule_id, $this->rule_names[ $rule_id ], $children );
164170
}
165171
}

0 commit comments

Comments
 (0)