Skip to content

Commit 12a78da

Browse files
committed
Inline the keyword lookup on the hot identifier path
The identifier/keyword branch handles the single largest share of tokens (~17% identifiers plus all keywords, ~35-45% of the corpus). It called two methods per token: get_current_token_bytes() to extract the token string and determine_identifier_or_keyword_type() to classify it. Inline that fast path into read_next_token(): extract the bytes and do the strtoupper + TOKENS lookup directly, returning the identifier without any call when it is not a keyword (the common case). The post-lookup keyword logic (version gating, function-call lookahead, high-NOT precedence, synonyms) moves to a new resolve_keyword_type() that is reached only for actual keywords; determine_identifier_or_keyword_type() now delegates to it for its other caller. Lex-only throughput on the MySQL server corpus: +5-6% under tracing JIT, +3.4% without (best-of-seven, ABAB-confirmed). The keyword path's measured ceiling was ~10% (JIT), most of which is the irreducible substr/strtoupper/ hash-lookup work that remains.
1 parent ee86643 commit 12a78da

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

‎packages/mysql-on-sqlite/src/mysql/class-wp-mysql-lexer.php‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,12 +2438,19 @@ private function read_next_token(): ?int {
24382438
) {
24392439
$started_at = $this->bytes_already_read;
24402440
$type = $this->read_identifier();
2441-
if ( self::IDENTIFIER === $type ) {
2441+
if (
2442+
self::IDENTIFIER === $type
24422443
// When preceded by a dot, it is always an identifier.
2443-
if ( $started_at > 0 && '.' === $this->sql[ $started_at - 1 ] ) {
2444-
$type = self::IDENTIFIER;
2445-
} else {
2446-
$type = $this->determine_identifier_or_keyword_type( $this->get_current_token_bytes() );
2444+
&& ! ( $started_at > 0 && '.' === $this->sql[ $started_at - 1 ] )
2445+
) {
2446+
// Inline the keyword lookup on the hot identifier path: most
2447+
// identifiers are not keywords, so this avoids two method calls
2448+
// (token-bytes extraction + keyword determination) per token.
2449+
$keyword = self::TOKENS[ strtoupper(
2450+
substr( $this->sql, $started_at, $this->bytes_already_read - $started_at )
2451+
) ] ?? self::IDENTIFIER;
2452+
if ( self::IDENTIFIER !== $keyword ) {
2453+
$type = $this->resolve_keyword_type( $keyword );
24472454
}
24482455
}
24492456
} elseif ( null !== $byte && isset( $single_byte_ops[ $byte ] ) ) {
@@ -2996,13 +3003,20 @@ private function read_comment_content(): void {
29963003
}
29973004

29983005
private function determine_identifier_or_keyword_type( string $value ): int {
2999-
$value = strtoupper( $value );
3000-
3001-
// Lookup the string in the token table.
3002-
$type = self::TOKENS[ $value ] ?? self::IDENTIFIER;
3006+
$type = self::TOKENS[ strtoupper( $value ) ] ?? self::IDENTIFIER;
30033007
if ( self::IDENTIFIER === $type ) {
30043008
return self::IDENTIFIER;
30053009
}
3010+
return $this->resolve_keyword_type( $type );
3011+
}
3012+
3013+
/**
3014+
* Resolve a keyword token id matched in self::TOKENS, applying version gating,
3015+
* function-call lookahead, the SQL_MODE_HIGH_NOT_PRECEDENCE rule, and synonyms.
3016+
*
3017+
* @param int $type A token id already matched in self::TOKENS (never IDENTIFIER).
3018+
*/
3019+
private function resolve_keyword_type( int $type ): int {
30063020

30073021
// Apply MySQL version specifics (positive number: >= <version>, negative number: < <version>).
30083022
if ( isset( self::VERSIONS[ $type ] ) ) {

0 commit comments

Comments
 (0)