Speed up parsing by avoiding keyword and token allocations - #318
Closed
git-hulk wants to merge 1 commit into
Closed
Conversation
Profiling the large PostHog queries showed the parser was bound by
allocations rather than by grammar work. About 60% of allocated bytes
came from peekToken: the clause-starter lookahead peeked up to 14 times
per select item and each peek lexed the next token from scratch. Every
unquoted identifier also paid a strings.ToUpper for the keyword lookup,
every token was a separate heap object, and the precedence lookup that
runs after each primary expression chained ~25 sequential match calls.
- Cache the last peekToken transition in the lexer, keyed on the full
lexer state (offset and previous token), so repeated peeks and the
following consumeToken reuse it. Backtracking to a saved state hits
the cache again and unary-minus disambiguation stays exact.
- Add lookupFold/containsFold, which upper-case into a stack buffer
and index the map with string(buf) so no copy is allocated. Use it
for keyword detection, reserved-keyword checks, interval units and
keyword-argument function names. Token.ToString returns the interned
canonical keyword instead of a fresh upper-cased copy.
- Carve tokens from a slab sized by the remaining input, so tiny
statements do not pay for a large chunk.
- Switch getNextPrecedence on the token kind once, let matchKeyword
skip the variadic matchTokenKind, peek once in the clause-starter
check, and add an ASCII fast path to skipSpace.
Results on an Apple M-series laptop (see docs/benchmarks.md):
posthog_huge_0 5.4 ms / 59k allocs -> 1.9 ms / 14.5k allocs
posthog_huge_1 4.7 ms / 50k allocs -> 1.6 ms / 12.6k allocs
window_function 57 us / 595 allocs -> 15 us / 93 allocs
No public API changes. Golden AST and format fixtures are unchanged.
Assistant By Claude Fable 5.1
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
git-hulk
marked this pull request as draft
September 2, 2026 13:38
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.
Profiling the large PostHog queries showed the parser was bound by
allocations rather than by grammar work. The clause-starter lookahead
peeked up to 14 times per select item and each peek lexed the next
token from scratch. Every unquoted identifier also paid a
strings.ToUpper for the keyword lookup, every token was a separate heap
object, and the precedence lookup that runs after each primary
expression chained ~25 sequential match calls.
Peek once in the clause-starter check and compare the token against
the keyword list, instead of peeking once per keyword.
Add lookupFold/containsFold, which upper-case into a stack buffer
and index the map with string(buf) so no copy is allocated. Use it
for keyword detection, reserved-keyword checks, interval units and
keyword-argument function names. Token.ToString returns the interned
canonical keyword instead of a fresh upper-cased copy.
Carve tokens from a slab sized by the remaining input, so tiny
statements do not pay for a large chunk.
Switch getNextPrecedence on the token kind once, let matchKeyword
skip the variadic matchTokenKind, and add an ASCII fast path to
skipSpace.
Results on an Apple M-series laptop (see docs/benchmarks.md):
posthog_huge_0 5.4 ms / 59k allocs -> 2.5 ms / 14.9k allocs
posthog_huge_1 4.7 ms / 50k allocs -> 2.0 ms / 12.9k allocs
window_function 57 us / 595 allocs -> 20 us / 101 allocs
No public API changes. Golden AST and format fixtures are unchanged.
🤖 Generated with Claude Code