-
-
Notifications
You must be signed in to change notification settings - Fork 722
perf(lexer): add #[cold] to unicode path
#12768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf(lexer): add #[cold] to unicode path
#12768
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. |
CodSpeed Instrumentation Performance ReportMerging #12768 will not alter performanceComparing Summary
|
|
Codspeed does not consider branch predictors. Benching locally. No performance improvement, slight regression? |
|
@copilot remove usage of |
... You're correct - the benchmarks show no meaningful performance improvement. I've reverted all the branch prediction changes in commit 9166ec9, removing the |
I've removed all |
|
This seems to have a big impact (5% - 9%): |
de52c2c to
a690045
Compare
|
Final round: |
#[cold] to unicode path
Merge activity
|
This PR refactors the lexer to use `#[cold]` functions instead of runtime branch prediction hints for better performance optimization. ## Background Previously attempted to optimize lexer performance using the `likely_stable` crate with `likely()` and `unlikely()` hints. However, benchmarking showed no meaningful performance improvement and some slight regressions. ## Changes Instead of runtime branch prediction hints, this approach uses `#[cold]` function attributes to move rarely executed code paths out of the hot path: - **String handling**: Extract escape sequence processing into `save_escaped_string()` - **Numeric parsing**: Move decimal point and BigInt suffix handling into cold functions - **Unicode processing**: Separate Unicode character handling from ASCII fast path in `next_char()`, `next_2_chars()`, and `peek_char()` - **Error cases**: Move irregular whitespace and invalid character handling into cold functions ## Benefits The `#[cold]` attribute tells the compiler that these functions are rarely executed, allowing it to: - Keep hot paths compact and well-predicted - Move cold code out of instruction cache pressure - Optimize register allocation for common cases - Provide better performance than manual runtime hints This approach achieves similar optimization goals while being more maintainable and letting the compiler make informed decisions about code layout. <!-- START COPILOT CODING AGENT TIPS --> --- 💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to start the survey.
a690045 to
373b5b7
Compare
This PR refactors the lexer to use `#[cold]` functions instead of runtime branch prediction hints for better performance optimization. ## Background Previously attempted to optimize lexer performance using the `likely_stable` crate with `likely()` and `unlikely()` hints. However, benchmarking showed no meaningful performance improvement and some slight regressions. ## Changes Instead of runtime branch prediction hints, this approach uses `#[cold]` function attributes to move rarely executed code paths out of the hot path: - **String handling**: Extract escape sequence processing into `save_escaped_string()` - **Numeric parsing**: Move decimal point and BigInt suffix handling into cold functions - **Unicode processing**: Separate Unicode character handling from ASCII fast path in `next_char()`, `next_2_chars()`, and `peek_char()` - **Error cases**: Move irregular whitespace and invalid character handling into cold functions ## Benefits The `#[cold]` attribute tells the compiler that these functions are rarely executed, allowing it to: - Keep hot paths compact and well-predicted - Move cold code out of instruction cache pressure - Optimize register allocation for common cases - Provide better performance than manual runtime hints This approach achieves similar optimization goals while being more maintainable and letting the compiler make informed decisions about code layout. <!-- START COPILOT CODING AGENT TIPS --> --- 💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to start the survey.
#12768 split `next_char`, `next_2_chars`, and `peek_char` into separate functions for the hot and cold paths. That was a good change, but had one side-effect - because the unicode branch is now in a separate function which isn't inlined, the compiler loses knowledge of the context - that `Source` isn't at EOF, and that (in 2 of the 3 methods) the next character is known not to be ASCII. Add unchecked assertions to inform compiler of the known facts, so it can remove 2 branches when calling `chars.next().unwrap()`. This code is on a cold path, so will likely not make a noticeable difference in files which don't contain many Unicode chars (like our benchmark fixtures), but why not?
This PR refactors the lexer to use
#[cold]functions instead of runtime branch prediction hints for better performance optimization.Background
Previously attempted to optimize lexer performance using the
likely_stablecrate withlikely()andunlikely()hints. However, benchmarking showed no meaningful performance improvement and some slight regressions.Changes
Instead of runtime branch prediction hints, this approach uses
#[cold]function attributes to move rarely executed code paths out of the hot path:save_escaped_string()next_char(),next_2_chars(), andpeek_char()Benefits
The
#[cold]attribute tells the compiler that these functions are rarely executed, allowing it to:This approach achieves similar optimization goals while being more maintainable and letting the compiler make informed decisions about code layout.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.