Skip to content

Commit 91f53a1

Browse files
committed
Replace the lexer's token queue with a single pending slot
The pull iterator buffered produced tokens in a dynamic $token_queue drained by index. A scan step yields at most two grammar tokens, so a single $pending_token slot suffices: next_token() returns the first and holds the second. The multi-token producers (@, WITH ROLLUP, end markers) now append to a caller-supplied array, shared directly by both next_token() and remaining_tokens() — removing the queue bookkeeping and the duplicated drain in the fast path. A make_token() helper unifies token construction. Output is byte-identical and throughput is unchanged (the multi-token cases were already off the hot path); this is a structural cleanup.
1 parent 33842bf commit 91f53a1

1 file changed

Lines changed: 53 additions & 58 deletions

File tree

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

Lines changed: 53 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -286,22 +286,15 @@ class WP_MySQL_Lexer {
286286
private $no_backslash_escapes = false;
287287

288288
/**
289-
* Grammar tokens produced ahead of the cursor, waiting to be emitted.
289+
* A grammar token produced ahead of the cursor, waiting to be emitted.
290290
*
291-
* A single scan step can yield more than one grammar token (e.g. "@" plus
292-
* its name, or the two end terminals), so produced tokens are buffered here
293-
* and handed out one at a time by next_token().
291+
* A single scan step can yield two grammar tokens (e.g. "@" plus its name,
292+
* or the two end terminals); next_token() returns the first and holds the
293+
* second here for the following call.
294294
*
295-
* @var WP_MySQL_Token[]
296-
*/
297-
private $token_queue = array();
298-
299-
/**
300-
* Index of the next token to emit from "$this->token_queue".
301-
*
302-
* @var int
295+
* @var WP_MySQL_Token|null
303296
*/
304-
private $queue_index = 0;
297+
private $pending_token = null;
305298

306299
/**
307300
* A scanned-ahead grammar token held back to be processed on the next step.
@@ -369,24 +362,29 @@ public function __construct(
369362
* @return bool Whether a token is available at the new cursor position.
370363
*/
371364
public function next_token(): bool {
372-
if ( $this->queue_index < count( $this->token_queue ) ) {
373-
$this->current_token = $this->token_queue[ $this->queue_index++ ];
365+
if ( null !== $this->pending_token ) {
366+
$this->current_token = $this->pending_token;
367+
$this->pending_token = null;
374368
return true;
375369
}
376-
377-
// The buffer is drained; produce the next token (or tokens) into it.
378-
$this->token_queue = array();
379-
$this->queue_index = 0;
380-
if ( ! $this->stream_ended ) {
381-
$this->produce();
370+
if ( $this->stream_ended ) {
371+
$this->current_token = null;
372+
return false;
382373
}
383374

384-
if ( $this->queue_index < count( $this->token_queue ) ) {
385-
$this->current_token = $this->token_queue[ $this->queue_index++ ];
386-
return true;
375+
// A scan step yields one or two grammar tokens: return the first and
376+
// hold the second (if any) back for the next call.
377+
$out = array();
378+
$this->produce( $out );
379+
if ( ! $out ) {
380+
$this->current_token = null;
381+
return false;
382+
}
383+
$this->current_token = $out[0];
384+
if ( isset( $out[1] ) ) {
385+
$this->pending_token = $out[1];
387386
}
388-
$this->current_token = null;
389-
return false;
387+
return true;
390388
}
391389

392390
/**
@@ -416,7 +414,7 @@ public function get_token(): ?WP_MySQL_Token {
416414
* terminators on invalid input.
417415
*/
418416
public function remaining_tokens(): array {
419-
if ( $this->stream_ended || $this->queue_index > 0 || null !== $this->current_token ) {
417+
if ( $this->stream_ended || null !== $this->pending_token || null !== $this->current_token ) {
420418
$tokens = array();
421419
while ( $this->next_token() ) {
422420
$tokens[] = $this->current_token;
@@ -463,24 +461,20 @@ public function remaining_tokens(): array {
463461
continue;
464462
}
465463

466-
// Multi-token lexemes: reuse the buffered producers, then drain.
467-
$this->token_queue = array();
464+
// Multi-token lexemes: the shared producers append straight to the list.
468465
if ( self::EOF === $type ) {
469-
$this->emit_end_markers();
466+
$this->emit_end_markers( $tokens );
467+
break;
470468
} elseif ( self::WITH_SYMBOL === $type ) {
471-
$this->produce_with_or_rollup( $start, $length );
469+
$this->produce_with_or_rollup( $tokens, $start, $length );
472470
} else {
473-
$this->produce_at_variable( $start );
474-
}
475-
foreach ( $this->token_queue as $token ) {
476-
$tokens[] = $token;
471+
$this->produce_at_variable( $tokens, $start );
477472
}
478473
if ( $this->stream_ended ) {
479474
break;
480475
}
481476
}
482477

483-
$this->token_queue = array();
484478
return $tokens;
485479
}
486480

@@ -574,7 +568,7 @@ public static function get_token_name( int $token_id ): ?string {
574568
* own lexer: "@" expands to the sign plus its name, "WITH ROLLUP" contracts
575569
* into one terminal, and the end of input yields the two end terminals.
576570
*/
577-
private function produce(): void {
571+
private function produce( array &$out ): void {
578572
// Take the lexeme held back by a "WITH" lookahead, or scan a new one.
579573
if ( null !== $this->lookahead ) {
580574
list( $type, $start, $length ) = $this->lookahead;
@@ -590,13 +584,13 @@ private function produce(): void {
590584
}
591585

592586
if ( self::EOF === $type ) {
593-
$this->emit_end_markers();
587+
$this->emit_end_markers( $out );
594588
} elseif ( self::WITH_SYMBOL === $type ) {
595-
$this->produce_with_or_rollup( $start, $length );
589+
$this->produce_with_or_rollup( $out, $start, $length );
596590
} elseif ( self::AT_SIGN_SYMBOL === $type ) {
597-
$this->produce_at_variable( $start );
591+
$this->produce_at_variable( $out, $start );
598592
} else {
599-
$this->enqueue_token( $type, $start, $length );
593+
$out[] = $this->make_token( $type, $start, $length );
600594
}
601595
}
602596

@@ -609,26 +603,26 @@ private function produce(): void {
609603
* @param int $with_start Byte offset of the "WITH" token.
610604
* @param int $with_length Byte length of the "WITH" token.
611605
*/
612-
private function produce_with_or_rollup( int $with_start, int $with_length ): void {
606+
private function produce_with_or_rollup( array &$out, int $with_start, int $with_length ): void {
613607
if ( ! $this->scan_lexeme() ) {
614608
// "WITH" was the last valid token; the trailing input is invalid.
615-
$this->enqueue_token( self::WITH_SYMBOL, $with_start, $with_length );
609+
$out[] = $this->make_token( self::WITH_SYMBOL, $with_start, $with_length );
616610
$this->stream_ended = true;
617611
return;
618612
}
619613

620614
if ( self::ROLLUP_SYMBOL === $this->token_type ) {
621-
$this->enqueue_token(
615+
$out[] = $this->make_token(
622616
self::WITH_ROLLUP_SYMBOL,
623617
$with_start,
624618
$this->bytes_already_read - $with_start
625619
);
626620
return;
627621
}
628622

629-
$this->enqueue_token( self::WITH_SYMBOL, $with_start, $with_length );
623+
$out[] = $this->make_token( self::WITH_SYMBOL, $with_start, $with_length );
630624
if ( self::EOF === $this->token_type ) {
631-
$this->emit_end_markers();
625+
$this->emit_end_markers( $out );
632626
return;
633627
}
634628
$this->lookahead = array(
@@ -650,14 +644,14 @@ private function produce_with_or_rollup( int $with_start, int $with_length ): vo
650644
*
651645
* @param int $at_start Byte offset of the "@" token.
652646
*/
653-
private function produce_at_variable( int $at_start ): void {
654-
$this->enqueue_token( self::AT_SIGN_SYMBOL, $at_start, 1 );
647+
private function produce_at_variable( array &$out, int $at_start ): void {
648+
$out[] = $this->make_token( self::AT_SIGN_SYMBOL, $at_start, 1 );
655649

656650
$position = $this->bytes_already_read; // Right after the "@".
657651
$next = $this->sql[ $position ] ?? '';
658652

659653
if ( '@' === $next ) {
660-
$this->enqueue_token( self::AT_SIGN_SYMBOL, $position, 1 );
654+
$out[] = $this->make_token( self::AT_SIGN_SYMBOL, $position, 1 );
661655
$this->bytes_already_read = $position + 1;
662656
return;
663657
}
@@ -668,21 +662,21 @@ private function produce_at_variable( int $at_start ): void {
668662
$position
669663
);
670664
if ( $name_length > 0 ) {
671-
$this->enqueue_token( self::IDENTIFIER, $position, $name_length );
665+
$out[] = $this->make_token( self::IDENTIFIER, $position, $name_length );
672666
$this->bytes_already_read = $position + $name_length;
673667
} elseif ( "'" !== $next && '"' !== $next && '`' !== $next ) {
674-
$this->enqueue_token( self::IDENTIFIER, $position, 0 );
668+
$out[] = $this->make_token( self::IDENTIFIER, $position, 0 );
675669
}
676670
}
677671

678672
/**
679673
* Emit the two terminals that close a valid stream: END_OF_INPUT and Bison's
680674
* end marker ($end), both at the end-of-input position.
681675
*/
682-
private function emit_end_markers(): void {
683-
$end = $this->bytes_already_read;
684-
$this->enqueue_token( self::END_OF_INPUT, $end, 0 );
685-
$this->enqueue_token( self::END_MARKER, $end, 0 );
676+
private function emit_end_markers( array &$out ): void {
677+
$end = $this->bytes_already_read;
678+
$out[] = $this->make_token( self::END_OF_INPUT, $end, 0 );
679+
$out[] = $this->make_token( self::END_MARKER, $end, 0 );
686680
$this->stream_ended = true;
687681
}
688682

@@ -712,14 +706,15 @@ private function scan_lexeme(): bool {
712706
}
713707

714708
/**
715-
* Append a grammar token to the output buffer.
709+
* Build a grammar token over the current input and SQL mode.
716710
*
717711
* @param int $type The grammar token number.
718712
* @param int $start Byte offset where the token begins.
719713
* @param int $length Byte length of the token.
714+
* @return WP_MySQL_Token
720715
*/
721-
private function enqueue_token( int $type, int $start, int $length ): void {
722-
$this->token_queue[] = new WP_MySQL_Token(
716+
private function make_token( int $type, int $start, int $length ): WP_MySQL_Token {
717+
return new WP_MySQL_Token(
723718
$type,
724719
$start,
725720
$length,

0 commit comments

Comments
 (0)