perf(parser): replace regex tokenizer with state-machine + keyword trie#12
Merged
Conversation
Introduces TokenizerFSM, a hand-rolled state machine that tokenizes via: - a character-keyed trie for English/Vietnamese (multi-word) keywords with per-entry boundary rules (\b for ASCII, custom for VI, none for else/return/ try/as/from/const/async — matching the original regex spec exactly); - bounded backtracking only when distinguishing multi-word identifiers from multi-word keywords (peek next word, rewind on no match); - direct char-code dispatch for whitespace/comments/numbers/operators, eliminating the per-token linear scan over ~80 regex specs and the String#slice + concat in the regex tokenizer's hot path. Wired through Parser via a `tokenizerKind: 'fsm' | 'regex'` option (default 'fsm'). The legacy Tokenizer is kept side-by-side for parity testing and rollback. Tests: 402/402 pass (regex baseline was 347; +55 new tests for fixtures, parity, snapshots, and the FSM tokenizer itself). Bench (vitest bench, side-by-side): tiny 1.8k hz → 364k hz (~206x) medium 48 hz → 26k hz (~546x) keywordHeavy 33 hz → 20k hz (~609x) stringHeavy 143 hz → 69k hz (~484x) large (14k) 1.1 hz → 3.3k hz (~2969x — eliminates super-linear regex behavior) Adds a benchmark harness (vitest bench, fixtures + tokenizer + parser benches), captured baseline JSON, and token-stream snapshots so future tokenizer changes are guarded against silent drift. https://claude.ai/code/session_01DwkDg2bBKFmfGmfP3Xtg7o
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduces TokenizerFSM, a hand-rolled state machine that tokenizes via:
per-entry boundary rules (\b for ASCII, custom for VI, none for else/return/
try/as/from/const/async — matching the original regex spec exactly);
multi-word keywords (peek next word, rewind on no match);
eliminating the per-token linear scan over ~80 regex specs and the
String#slice + concat in the regex tokenizer's hot path.
Wired through Parser via a
tokenizerKind: 'fsm' | 'regex'option (default'fsm'). The legacy Tokenizer is kept side-by-side for parity testing and
rollback.
Tests: 402/402 pass (regex baseline was 347; +55 new tests for fixtures,
parity, snapshots, and the FSM tokenizer itself).
Bench (vitest bench, side-by-side):
tiny 1.8k hz → 364k hz (~206x)
medium 48 hz → 26k hz (~546x)
keywordHeavy 33 hz → 20k hz (~609x)
stringHeavy 143 hz → 69k hz (~484x)
large (14k) 1.1 hz → 3.3k hz (~2969x — eliminates super-linear regex behavior)
Adds a benchmark harness (vitest bench, fixtures + tokenizer + parser benches),
captured baseline JSON, and token-stream snapshots so future tokenizer changes
are guarded against silent drift.
https://claude.ai/code/session_01DwkDg2bBKFmfGmfP3Xtg7o