Skip to content

refactor(parser): make lexer byte handlers safe functions - #29643

Closed
Boshen wants to merge 1 commit into
mainfrom
refactor/parser-lexer-safe-handlers
Closed

Boshen wants to merge 1 commit into
mainfrom
refactor/parser-lexer-safe-handlers

Conversation

@Boshen

@Boshen Boshen commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

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 unsafe from 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 on main.

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 with assert_unchecked!.

Those facts are now carried in a type. 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. 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.
  • Source::next_byte and BOOL_MASK are removed. Both were unused, and marked #[expect(dead_code)].

Occurrences of unsafe in crates/oxc_parser/src/lexer:

File main This PR
byte_handlers.rs 13 0
mod.rs 17 2
identifier.rs 9 7
string.rs 5 3
jsx.rs 3 1
source.rs 59 64
All files 196 167

The 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 -l over 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.

Fixture vs main
App.tsx +0.94%
kitchen-sink.tsx +0.45%
RadixUIAdoptionSection.jsx +0.45%
pdf.mjs +0.76%
checker.ts +0.56%
react.development.js +0.67%
binder.ts +0.68%
antd.js +0.34%

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%:

Files vs main
npm packages, .js +0.66%
npm packages, minified .js +0.65%
npm packages, .d.ts +0.56%
npm packages, .ts +0.75%
repositories, .ts +0.47%
repositories, .tsx / .jsx +0.77%
repositories, .d.ts +0.54%
repositories, .js +0.72%
All +0.65%

The lexer benchmarks 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 against main at 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 main at this PR. Two builds of main differed by 0.3% and 0.7% in two such measurements, so this is not clear of the noise.

Validation: oxc_parser lint, rustdoc and unit tests pass, and cargo coverage parser snapshots are unchanged, at this commit. Tokens, comments and diagnostics are identical to main on the 63,739 files above and on 100,279 files from the conformance suites (Test262, Babel, TypeScript, and the misc fixtures).

AI-assisted.

🤖 Generated with Claude Code

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>
@github-actions github-actions Bot added the A-parser Area - JS/TS parsing and syntax errors, oxc_parser label Nov 16, 2026
@Boshen
Boshen added this pull request to stack #29644 November 16, 2026 04:12
@codspeed

codspeed Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 91 untouched benchmarks
⏩ 19 skipped benchmarks1


Comparing refactor/parser-lexer-safe-handlers (8afde1b) with main (fe214bc)

Open in CodSpeed

Footnotes

  1. 19 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@Boshen

Boshen commented Nov 16, 2026

Copy link
Copy Markdown
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 main again.

On its own it cost 0.6% more instructions than main on real-world files. The branch is kept in case the refactor is picked up separately.

AI-assisted.

@Boshen Boshen closed this Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-parser Area - JS/TS parsing and syntax errors, oxc_parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant