Skip to content

refactor(lexer): revert Kind methods to normal matches#20655

Merged
graphite-app[bot] merged 1 commit into
mainfrom
om/03-23-refactor_lexer_revert_kind_methods_to_normal_matches
Mar 23, 2026
Merged

refactor(lexer): revert Kind methods to normal matches#20655
graphite-app[bot] merged 1 commit into
mainfrom
om/03-23-refactor_lexer_revert_kind_methods_to_normal_matches

Conversation

@overlookmotel

@overlookmotel overlookmotel commented Mar 23, 2026

Copy link
Copy Markdown
Member

Partially revert #14410. That PR altered Kind::is_number and Kind::is_any_keyword, replacing matches! on Kind variants and method calls with explicit matching on the enum discriminant range e.g. matches!(self as u8, x if x >= Decimal as u8 && x <= HexBigInt as u8).

The rationale for the change was improved performance. I was suspicious of the AI's claims that this was a perf improvement, as I assumed that compiler would boil down the code to the same anyway.

I asked Claude to generate the ASM before / after and he concluded it makes no difference either way.

The compiler generates identical code - a simple sub + cmp + setb range check in both cases.

// is_number
add dil, 107
cmp dil, 11
setb al

LLVM recognises that:

  1. Contiguous matches! variants can be reduced to a range check.
  2. The union of the 4 keyword sub-methods covers a contiguous range, so it merges them into a single range check.

The commit's claim about "20% fewer instructions" and "better pipeline utilization" is nonsense. The manual as u8 casts just make the code harder to read for zero benefit.

New Claude pretty damning about old Claude!

So go back to explicit matches, as it's easier to understand.

Copy link
Copy Markdown
Member Author

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent changes, fast-track this PR to the front of the merge queue

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions github-actions Bot added A-parser Area - Parser C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior labels Mar 23, 2026
@codspeed-hq

codspeed-hq Bot commented Mar 23, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 53 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing om/03-23-refactor_lexer_revert_kind_methods_to_normal_matches (79cee57) with main (89a5374)2

Open in CodSpeed

Footnotes

  1. 3 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.

  2. No successful run was found on main (75f4a3e) during the generation of this report, so 89a5374 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@overlookmotel overlookmotel marked this pull request as ready for review March 23, 2026 12:50
Copilot AI review requested due to automatic review settings March 23, 2026 12:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR partially reverts #14410 by restoring readable matches!/method-based implementations for keyword/number classification in the lexer Kind, instead of manual enum-discriminant range checks.

Changes:

  • Replaced Kind::is_number()’s discriminant range check with an explicit matches! over numeric literal variants.
  • Replaced Kind::is_any_keyword()’s discriminant range check with calls to the existing keyword-category helpers.

Comment thread crates/oxc_parser/src/lexer/kind.rs Outdated
Comment thread crates/oxc_parser/src/lexer/kind.rs
@overlookmotel overlookmotel force-pushed the om/03-23-refactor_lexer_revert_kind_methods_to_normal_matches branch 2 times, most recently from fdc71b0 to 79cee57 Compare March 23, 2026 13:02
@overlookmotel overlookmotel self-assigned this Mar 23, 2026
@overlookmotel overlookmotel added the 0-merge Merge with Graphite Merge Queue label Mar 23, 2026

overlookmotel commented Mar 23, 2026

Copy link
Copy Markdown
Member Author

Merge activity

Partially revert #14410. That PR altered `Kind::is_number` and `Kind::is_any_keyword`, replacing `matches!` on `Kind` variants and method calls with explicit matching on the enum discriminant range e.g. `matches!(self as u8, x if x >= Decimal as u8 && x <= HexBigInt as u8)`.

The rationale for the change was improved performance. I was suspicious of the AI's claims that this was a perf improvement, as I assumed that compiler would boil down the code to the same anyway.

I asked Claude to generate the ASM before / after and he concluded it makes no difference either way.

> The compiler generates identical code - a simple sub + cmp + setb range check in both cases.
>
> ```asm
> // is_number
> add dil, 107
> cmp dil, 11
> setb al
> ```
>
> LLVM recognises that:
>
> 1. Contiguous matches! variants can be reduced to a range check.
> 2. The union of the 4 keyword sub-methods covers a contiguous range, so it merges them into a single range check.
>
> The commit's claim about "20% fewer instructions" and "better pipeline utilization" is nonsense. The manual `as u8` casts just make the code harder to read for zero benefit.

New Claude pretty damning about old Claude!

So go back to explicit matches, as it's easier to understand.
@graphite-app graphite-app Bot force-pushed the om/03-23-refactor_lexer_revert_kind_methods_to_normal_matches branch from 79cee57 to 55d99db Compare March 23, 2026 13:09
@graphite-app graphite-app Bot merged commit 55d99db into main Mar 23, 2026
27 checks passed
@graphite-app graphite-app Bot removed the 0-merge Merge with Graphite Merge Queue label Mar 23, 2026
@graphite-app graphite-app Bot deleted the om/03-23-refactor_lexer_revert_kind_methods_to_normal_matches branch March 23, 2026 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-parser Area - Parser C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants