Skip to content

Commit 33842bf

Browse files
committed
Speed up remaining_tokens() with a tight scan loop
Tokenizing a whole statement routed every token through the pull iterator (next_token -> produce -> scan_lexeme -> read_next_token -> enqueue_token), adding ~4 method calls plus token-queue bookkeeping per token over a plain scan-and-emit loop. Give remaining_tokens() a tight fast path that emits the common single-token lexemes inline and delegates only the rare multi-token ones (@, WITH ROLLUP, end markers) to the buffered producers. The pull API is unchanged and the output is byte-identical; ~24% faster (no JIT) / ~16% (JIT) end-to-end over the MySQL server corpus.
1 parent e347cb2 commit 33842bf

1 file changed

Lines changed: 70 additions & 2 deletions

File tree

‎packages/mysql-parser/src/class-wp-mysql-lexer.php‎

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,15 +404,83 @@ public function get_token(): ?WP_MySQL_Token {
404404
/**
405405
* Read all remaining tokens as an array, draining the stream.
406406
*
407+
* From a fresh lexer this runs a tight scan-and-emit loop that bypasses the
408+
* pull iterator's per-token queue bookkeeping — the dominant cost when
409+
* tokenizing a whole statement. The common single-token lexemes are emitted
410+
* inline; only the multi-token ones (end markers, "WITH ROLLUP", "@") route
411+
* through the buffered producers. A lexer already advanced by next_token()
412+
* falls back to the iterator to stay correct.
413+
*
407414
* @return WP_MySQL_Token[] The remaining tokens — terminated by END_OF_INPUT
408415
* and the end marker, or a partial list with no
409416
* terminators on invalid input.
410417
*/
411418
public function remaining_tokens(): array {
419+
if ( $this->stream_ended || $this->queue_index > 0 || null !== $this->current_token ) {
420+
$tokens = array();
421+
while ( $this->next_token() ) {
422+
$tokens[] = $this->current_token;
423+
}
424+
return $tokens;
425+
}
426+
412427
$tokens = array();
413-
while ( $this->next_token() ) {
414-
$tokens[] = $this->current_token;
428+
$sql = $this->sql;
429+
$nbe = $this->no_backslash_escapes;
430+
while ( true ) {
431+
if ( null !== $this->lookahead ) {
432+
list( $type, $start, $length ) = $this->lookahead;
433+
$this->lookahead = null;
434+
} else {
435+
// Inlined scan_lexeme(): skip whitespace and comments, then scan.
436+
$this->bytes_already_read += strspn( $sql, self::WHITESPACE_MASK, $this->bytes_already_read );
437+
do {
438+
$this->token_starts_at = $this->bytes_already_read;
439+
$this->token_type = $this->read_next_token();
440+
} while (
441+
self::WHITESPACE === $this->token_type
442+
|| self::COMMENT === $this->token_type
443+
|| self::MYSQL_COMMENT_START === $this->token_type
444+
|| self::MYSQL_COMMENT_END === $this->token_type
445+
);
446+
$type = $this->token_type;
447+
if ( null === $type ) {
448+
// Invalid input: end the stream after the last valid token.
449+
$this->stream_ended = true;
450+
break;
451+
}
452+
$start = $this->token_starts_at;
453+
$length = $this->bytes_already_read - $start;
454+
}
455+
456+
// Common single-token lexeme — the overwhelming majority.
457+
if (
458+
self::EOF !== $type
459+
&& self::WITH_SYMBOL !== $type
460+
&& self::AT_SIGN_SYMBOL !== $type
461+
) {
462+
$tokens[] = new WP_MySQL_Token( $type, $start, $length, $sql, $nbe );
463+
continue;
464+
}
465+
466+
// Multi-token lexemes: reuse the buffered producers, then drain.
467+
$this->token_queue = array();
468+
if ( self::EOF === $type ) {
469+
$this->emit_end_markers();
470+
} elseif ( self::WITH_SYMBOL === $type ) {
471+
$this->produce_with_or_rollup( $start, $length );
472+
} else {
473+
$this->produce_at_variable( $start );
474+
}
475+
foreach ( $this->token_queue as $token ) {
476+
$tokens[] = $token;
477+
}
478+
if ( $this->stream_ended ) {
479+
break;
480+
}
415481
}
482+
483+
$this->token_queue = array();
416484
return $tokens;
417485
}
418486

0 commit comments

Comments
 (0)