Skip to content

perf(lexer): O(N^2) -> O(N) tokenizer — self-compile 320s -> 28s#16

Merged
zemo-g merged 1 commit into
masterfrom
fix/lexer-quadratic
Jun 16, 2026
Merged

perf(lexer): O(N^2) -> O(N) tokenizer — self-compile 320s -> 28s#16
zemo-g merged 1 commit into
masterfrom
fix/lexer-quadratic

Conversation

@zemo-g

@zemo-g zemo-g commented Jun 16, 2026

Copy link
Copy Markdown
Owner

The lexer used length cs == 0 as its empty-check; _rail_length walks the whole list, so the main loop tc (recursing per-token over remaining chars) was O(chars x tokens) ~2.7e10 on the 551K-char compile.rail = ~297s, 92% of every self-compile. Switched 10 empty-checks to == [] (O(1)). Byte-identical (same tokens).

Measured: self-compile 320s -> 28s (~11x). 177/177 tests. 2-pass fixed point byte-identical (reseeded). The lexer was always O(N^2); compile.rail growing to 551K chars made it dominant.

🤖 Generated with Claude Code

The lexer helpers used `length cs == 0` as their empty-check. _rail_length
walks the whole list, so that test is O(remaining) PER recursion step. The
main loop `tc` recurses once per token over the remaining char list, making
it O(chars x tokens) ~ 2.7e10 on the 551K-char compile.rail -- ~297s, 92% of
the entire self-compile. `rev` (over ~100K tokens), `strip_nl_pp_loop`,
`lx_str`/`lx_col` (long string/word spans) compounded it.

Switched 10 empty-checks (tc, rev_acc, strip_nl_pp_loop, lx_str, lx_col,
lx_skip, lx_pk, htint_acc, has + one `length (tail cs) > 0`) to `== []` /
`!= []` (O(1)). Byte-identical: same tokens out -> same parse -> same codegen.

Measured: tokenize 297s -> sub-second; self-compile 320s -> 28s (~11x).
177/177 tests; 2-pass self-host fixed point byte-identical (rail_native reseeded).
Not audit-resolve-specific -- the lexer was always O(N^2); compile.rail growing
to 551K chars made it dominant.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@zemo-g zemo-g merged commit 59ee1a8 into master Jun 16, 2026
3 checks passed
@zemo-g zemo-g deleted the fix/lexer-quadratic branch June 16, 2026 04:19
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.

1 participant