-
-
Notifications
You must be signed in to change notification settings - Fork 357
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(parser): lex identifiers as bytes not chars #2352
Merged
Boshen
merged 1 commit into
main
from
02-08-perf_parser_lex_identifiers_as_bytes_not_chars
Feb 9, 2024
Merged
perf(parser): lex identifiers as bytes not chars #2352
Boshen
merged 1 commit into
main
from
02-08-perf_parser_lex_identifiers_as_bytes_not_chars
Feb 9, 2024
Conversation
This file contains 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
This was referenced Feb 8, 2024
Current dependencies on/for this PR: This stack of pull requests is managed by Graphite. |
CodSpeed Performance ReportMerging #2352 will improve performances by 56.76%Comparing Summary
Benchmarks breakdown
|
6360f04
to
5b9f1a7
Compare
This was referenced Feb 8, 2024
5b9f1a7
to
070d3c4
Compare
593a602
to
beafc6c
Compare
Base automatically changed from
02-08-refactor_parser_macro_for_ASCII_identifier_byte_handlers
to
main
February 9, 2024 03:55
Boshen
pushed a commit
that referenced
this pull request
Feb 9, 2024
Add a macro for ASCII identifier byte handlers. This is a preparatory step towards #2352.
070d3c4
to
9a8d7b7
Compare
Boshen
pushed a commit
that referenced
this pull request
Feb 9, 2024
Uses the `byte_search!` macro introduced in #2352 to consume whitespace after a line break.
This was referenced May 13, 2024
IWANABETHATGUY
pushed a commit
to IWANABETHATGUY/oxc
that referenced
this pull request
May 29, 2024
…ct#2351) Add a macro for ASCII identifier byte handlers. This is a preparatory step towards oxc-project#2352.
IWANABETHATGUY
pushed a commit
to IWANABETHATGUY/oxc
that referenced
this pull request
May 29, 2024
This PR re-implements lexing identifiers with a fast path for the most common case - identifiers which are pure ASCII characters, using the new `Source` / `SourcePosition` APIs. Lexing identifiers is a hot path, and accounts for the majority of the time the Lexer spends. The performance bump from this change is (if I do say so myself!) quite decent. I've spent a lot of time tuning the implementation, which gained a further 10-15% on the Lexer benchmarks compared to my first, simpler attempt. Some of the design decisions, if they look odd, are likely motivated by gains in performance. ### Techniques This implementation uses a few different strategies for performance: * Search byte-by-byte, not char-by-char. * Process batches of 32 bytes at a time to reduce bounds checks. * Mark uncommon paths `#[cold]`. ### Structure The implementation is built in 3 layers: 1. ASCII characters only. 2. ASCII and Unicode characters. 3. `\` escape sequences (and all the above). `identifier_name_handler` starts at the top layer, and is optimized for consuming ASCII as fast as possible. Each "layer" is considered more uncommon than the previous, and dropping down a layer is a de-opt. I'm assuming that 95%+ of JavaScript code does not include either Unicode characters or escapes in identifiers, so the speed of the fast path is prioritised. That said, once a Unicode character is encountered, the next layer does expect to find further Unicode characters, rather than de-opting over and over again. If an identifier *starts* with a Unicode character, it enters the code straight on the 2nd layer, so is not penalised by going through a `#[cold]` boundary. Lexing Unicode is never going to be as fast as ASCII, but still I felt it was important not to penalise it unnecessarily, so as not to be Anglo-centric. ### ASCII search macro The main ASCII search is implemented as a macro. I found that, for reasons I don't understand, it's significantly faster to have all the code in a single function, even compared to multiple functions marked `#[inline]` or `#[inline(always)]`. The fastest implementation also requires some code to be repeated twice, which is nicer to do with a macro. This macro, and the `ByteMatchTable` types that go with it, are designed to be re-usable. Next step will be to apply them for whitespace and strings, which should be fairly simple. Searching in batches of 32 bytes is also designed to be forward-compatible with SIMD. ### Bye bye `AutoCow` `AutoCow` is removed. Instead, a string-builder is only created if it's needed, when a `\` escape is first encountered. The string builder is also more efficient than `AutoCow` was, as it copies bytes in chunks, rather than 1-by-1. This won't make much difference for identifiers, as escapes are so rare anyway, but this same technique can be used for strings, where they're more common.
IWANABETHATGUY
pushed a commit
to IWANABETHATGUY/oxc
that referenced
this pull request
May 29, 2024
Uses the `byte_search!` macro introduced in oxc-project#2352 to consume whitespace after a line break.
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.
This PR re-implements lexing identifiers with a fast path for the most common case - identifiers which are pure ASCII characters, using the new
Source
/SourcePosition
APIs.Lexing identifiers is a hot path, and accounts for the majority of the time the Lexer spends. The performance bump from this change is (if I do say so myself!) quite decent.
I've spent a lot of time tuning the implementation, which gained a further 10-15% on the Lexer benchmarks compared to my first, simpler attempt. Some of the design decisions, if they look odd, are likely motivated by gains in performance.
Techniques
This implementation uses a few different strategies for performance:
#[cold]
.Structure
The implementation is built in 3 layers:
\
escape sequences (and all the above).identifier_name_handler
starts at the top layer, and is optimized for consuming ASCII as fast as possible. Each "layer" is considered more uncommon than the previous, and dropping down a layer is a de-opt.I'm assuming that 95%+ of JavaScript code does not include either Unicode characters or escapes in identifiers, so the speed of the fast path is prioritised.
That said, once a Unicode character is encountered, the next layer does expect to find further Unicode characters, rather than de-opting over and over again. If an identifier starts with a Unicode character, it enters the code straight on the 2nd layer, so is not penalised by going through a
#[cold]
boundary. Lexing Unicode is never going to be as fast as ASCII, but still I felt it was important not to penalise it unnecessarily, so as not to be Anglo-centric.ASCII search macro
The main ASCII search is implemented as a macro. I found that, for reasons I don't understand, it's significantly faster to have all the code in a single function, even compared to multiple functions marked
#[inline]
or#[inline(always)]
. The fastest implementation also requires some code to be repeated twice, which is nicer to do with a macro.This macro, and the
ByteMatchTable
types that go with it, are designed to be re-usable. Next step will be to apply them for whitespace and strings, which should be fairly simple.Searching in batches of 32 bytes is also designed to be forward-compatible with SIMD.
Bye bye
AutoCow
AutoCow
is removed. Instead, a string-builder is only created if it's needed, when a\
escape is first encountered. The string builder is also more efficient thanAutoCow
was, as it copies bytes in chunks, rather than 1-by-1.This won't make much difference for identifiers, as escapes are so rare anyway, but this same technique can be used for strings, where they're more common.