Skip to content

fix(lexer): stop rechecking UTF-8 on every character - #965

Merged
LunaStev merged 1 commit into
wavefnd:masterfrom
hiyouwhatsapp:fix/935-linear-utf8-cursor
Oct 19, 2026
Merged

LunaStev merged 1 commit into
wavefnd:masterfrom
hiyouwhatsapp:fix/935-linear-utf8-cursor

Conversation

@hiyouwhatsapp

@hiyouwhatsapp hiyouwhatsapp commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #935

advance and peek were checking all the remaining source with from_utf8 every time they read a character. So a long comment was getting checked over and over, even though the source is already a valid &str

This just reads the next character from the string and moves forward by its byte length

Added a longer test with ASCII and multibyte comments, a nested comment, a Unicode identifier and a string. It checks the tokens and their positions with LF, CRLF and CR line endings. That test passes on the old code too, I wanted to make sure the cursor still gives the same result

I also timed the lexer before and after. A 240 KB comment went from about 4.4 seconds to 0.036 in the debug build. At 30 KB it went from 0.083 to 0.0046, and doubling the input now takes roughly twice as long. These are medians of five runs on the same machine, just timing the lexer

Testing: All 12 lexer tests pass at both O0 and O2 with Rust 1.89.0 on Windows. Built the actual utils, error and lexer sources directly with rustc. cargo fmt --all --check and git diff --check pass too but I haven't run the full workspace checks or Wave corpus, this setup doesn't have the LLVM SDK

@hiyouwhatsapp hiyouwhatsapp changed the title fix(lexer): avoid quadratic UTF-8 suffix validation fix(lexer): stop rechecking UTF-8 on every character Oct 19, 2026
@hiyouwhatsapp
hiyouwhatsapp marked this pull request as ready for review October 19, 2026 12:27

@LunaStev LunaStev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks

@LunaStev
LunaStev merged commit 56d7fb2 into wavefnd:master Oct 19, 2026
35 checks passed
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.

Avoid revalidating the entire remaining UTF-8 source for every lexer character

2 participants