Repository navigation
Conversation
Byte handlers were `unsafe fn`s. Each relied on its caller having checked that the byte it was called for is ASCII and is not at the end of the source, and the handlers for ASCII bytes told the compiler so with `assert_unchecked!`. Carry those facts in a type instead. `Source` checks them where it reads the byte, and returns an `AsciiPosition`: the position of an ASCII byte, so not the end of the source. The position after it is in bounds, and on a UTF-8 character boundary. Byte handlers take an `AsciiPosition` instead of a `SourcePosition`, and are safe functions, as are `identifier_name_handler`, `read_string_literal_double_quote`, `read_string_literal_single_quote` and `read_jsx_string_literal`. The byte handler table holds one handler for each ASCII byte, and is an associated constant of `Lexer`, so `LexerConfig` no longer has a `ByteHandlers` type or a `byte_handlers` method. A reference to an associated constant is to static memory, so getting a handler from the table is still a single load. Bytes which are not ASCII no longer have a byte handler. The token loop sends them, along with the last byte of the source, to one cold function. The token loop now checks that the first byte of each token is ASCII before it calls a handler, which is one branch per token. Also removes `Source::next_byte` and `BOOL_MASK`, which were unused, and only compiled because they were marked `#[expect(dead_code)]`. AI-assisted. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
Contributor
Merging this PR will not alter performance
Comparing Footnotes
|
Member
Author
|
Closing: this refactor is out of the lexer performance stack. #29617–#29623 are back on the commits they had before it, with #29617 based on On its own it cost 0.6% more instructions than AI-assisted. |
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.
First of a stack of 8 PRs. The 7 above it (#29617 to #29623) cut the work the lexer does for each token and each comment. This one removes
unsafefrom the code which they build on, so that their handlers can be safe functions. By the top of the stack (#29623), a full parse of 63,739 real-world files takes 9.8% fewer instructions and 4.7% fewer CPU cycles than onmain.Byte handlers were
unsafe fns. Each relied on its caller having checked that the byte it was called for is ASCII and is not at the end of the source, and the handlers for ASCII bytes told the compiler so withassert_unchecked!.Those facts are now carried in a type.
Sourcechecks them where it reads the byte, and returns anAsciiPosition: the position of an ASCII byte, so not the end of the source. The position after it is in bounds, and on a UTF-8 character boundary.AsciiPositioninstead of aSourcePosition, and are safe functions, as areidentifier_name_handler,read_string_literal_double_quote,read_string_literal_single_quoteandread_jsx_string_literal.Lexer.LexerConfigno longer has aByteHandlerstype or abyte_handlersmethod. A reference to an associated constant is to static memory, so getting a handler from the table is still a single load.Source::next_byteandBOOL_MASKare removed. Both were unused, and marked#[expect(dead_code)].Occurrences of
unsafeincrates/oxc_parser/src/lexer:mainbyte_handlers.rsmod.rsidentifier.rsstring.rsjsx.rssource.rsThe 7 PRs above this one add 5 between them.
Cost. The token loop now checks that the first byte of each token is ASCII before it calls a handler, which is one branch per token.
Instructions retired per parse on macOS arm64, Rust 1.100.0, measured with
/usr/bin/time -lover a fixed number of parses. Each figure is the lowest of 6 runs, and two sets of 3 runs differ by at most 0.13%. Negative is fewer.The same measurement over 63,739 real-world files (69.6M tokens), sampled from published npm packages and from application and library repositories. Each run also reads the files, so between runs the figure for one kind of file varies by up to 0.2%, and the total by 0.03%:
.js.js.d.ts.ts.ts.tsx/.jsx.d.ts.jsThe
lexerbenchmarks run the lexer on its own, where that branch is a larger share of the work. Over the 8 fixtures above, the lexer takes +1.05% instructions againstmainat this PR, −0.26% with #29617 on top, and −6.93% with #29618.CPU cycles. A full parse of the same 63,739 files, measured twice, takes +0.5% and +1.0% cycles against
mainat this PR. Two builds ofmaindiffered by 0.3% and 0.7% in two such measurements, so this is not clear of the noise.Validation:
oxc_parserlint, rustdoc and unit tests pass, andcargo coverage parsersnapshots are unchanged, at this commit. Tokens, comments and diagnostics are identical tomainon the 63,739 files above and on 100,279 files from the conformance suites (Test262, Babel, TypeScript, and themiscfixtures).AI-assisted.
🤖 Generated with Claude Code