refactor(lexer): revert Kind methods to normal matches#20655
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via 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. |
Merging this PR will not alter performance
Comparing Footnotes
|
There was a problem hiding this comment.
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 explicitmatches!over numeric literal variants. - Replaced
Kind::is_any_keyword()’s discriminant range check with calls to the existing keyword-category helpers.
fdc71b0 to
79cee57
Compare
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.
79cee57 to
55d99db
Compare

Partially revert #14410. That PR altered
Kind::is_numberandKind::is_any_keyword, replacingmatches!onKindvariants 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.
New Claude pretty damning about old Claude!
So go back to explicit matches, as it's easier to understand.