Skip to content

Skip runs of horizontal whitespace in one scan in Lexer::positionAfte… - #1963

Merged
spawnia merged 3 commits into
webonyx:masterfrom
OpaqueRock:optimize-whitespace-scanning
Aug 14, 2026
Merged

Skip runs of horizontal whitespace in one scan in Lexer::positionAfte…#1963
spawnia merged 3 commits into
webonyx:masterfrom
OpaqueRock:optimize-whitespace-scanning

Conversation

@OpaqueRock

Copy link
Copy Markdown
Contributor

Skip runs of horizontal whitespace in one scan in Lexer::positionAfterWhitespace

positionAfterWhitespace() currently advances one character at a time even
for long runs of tabs, spaces, and commas — none of which affect line/column
tracking. This proposed change uses strspn() to skip such runs in a single call, falling
through to the existing per-character handling for line breaks and the BOM.

Verified byte-identical token streams against the current implementation on
pretty-printed, minified, deeply-indented, and edge-case (BOM/CRLF/CR/blank
lines) inputs.

Added a benchDeeplyIndentedQuery case to LexerBench to measure the effect
on an indentation-heavy query:

before after
benchIntrospectionQuery 0.712ms 0.427ms
benchDeeplyIndentedQuery 4.345ms 2.768ms

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes the lexer’s whitespace skipping by consuming long runs of horizontal whitespace (tabs, spaces, commas) in a single native scan, while keeping the existing per-character handling for BOM and line terminators to preserve correct line/column tracking.

Changes:

  • Added Lexer::HORIZONTAL_WHITESPACE_BYTES and used strspn() in positionAfterWhitespace() to bulk-skip horizontal whitespace.
  • Kept existing logic for BOM, LF, CR, and CRLF handling to maintain correct line / lineStart bookkeeping.
  • Added a new benchDeeplyIndentedQuery benchmark case to quantify the optimization on indentation-heavy inputs.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Language/Lexer.php Bulk-skips runs of tab/space/comma via strspn() to reduce per-character overhead in whitespace scanning.
benchmarks/LexerBench.php Adds an indentation-heavy benchmark to measure the whitespace-skipping optimization impact.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@spawnia

spawnia commented Aug 14, 2026

Copy link
Copy Markdown
Collaborator

@copilot resolve the merge conflicts in this pull request

@spawnia spawnia left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work, thank you

@spawnia
spawnia merged commit a29ba52 into webonyx:master Aug 14, 2026
20 checks passed
@spawnia

spawnia commented Aug 14, 2026

Copy link
Copy Markdown
Collaborator

Thanks @OpaqueRock! Released as v15.37.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants