-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Speed up Parser::expected_tokens
#133793
base: master
Are you sure you want to change the base?
Speed up Parser::expected_tokens
#133793
Conversation
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
…, r=<try> Speed up `Parser::expected_tokens` r? `@ghost`
This comment has been minimized.
This comment has been minimized.
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (4e6952e): comparison URL. Overall result: ✅ improvements - no action neededBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. @bors rollup=never Instruction countThis is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.
Max RSS (memory usage)Results (primary -1.2%, secondary 1.6%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResults (primary -1.5%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 767.333s -> 766.554s (-0.10%) |
Because the `Token` type is similar to but different to the `TokenType` type, and the difference is important, so we want to avoid confusion.
The most significant is `check_keyword`: it now only pushes to `expected_token_types` if the keyword check fails, which matches how all the other `check` methods work. The remainder are just tweaks to make these methods more consistent with each other.
This is a naming convention used in a handful of spots in the parser for delimiters. It confused me when I first saw it a long time ago, and I've never liked it. A web search says "Bra-ket notation" exists in linear algebra but the terminology has zero prior use in a programming context, as far as I can tell. This commit changes it to `open`/`close`, which is consistent with the rest of the compiler.
0133601
to
f5482df
Compare
Some changes occurred in src/tools/rustfmt cc @rust-lang/rustfmt |
Best reviewed one commit at a time. Let's re-run perf just to be sure: @bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
…, r=<try> Speed up `Parser::expected_tokens` The constant pushing/clearing of `Parser::expected_tokens` during parsing is slow. This PR speeds it up greatly. r? `@estebank`
⌛ Trying commit f5482df with merge 26060e63f06a4dcd55fc0757eb5b0bdc8136ed3b... |
|
||
pub(super) fn consume_block(&mut self, delim: Delimiter, consume_close: ConsumeClosingDelim) { | ||
pub(super) fn consume_block( | ||
&mut self, | ||
open: ExpTokenPair<'_>, | ||
close: ExpTokenPair<'_>, | ||
consume_close: ConsumeClosingDelim, | ||
) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sad to see the delimiters go away and get denormalized, but I get it.
/// We really want to keep the number of variants to 128 or fewer, sot that | ||
/// `TokenTypeSet` can be implemented with a `u128`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the one hand, we should be able to do so. On the other, I can see this becoming a point of contention with t-lang in the medium future if we push back on a feature for this reason :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The 17 asm symbols would be a good place to cut things down if necessary. I'm a bit annoyed that they are even in there; so many of them for such a rare use case.
// This assertion will detect if this method and the type definition get out of sync. | ||
assert_eq!(token_type as u32, val); | ||
token_type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't the function just be the as
cast with a <=104
check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. You can convert a C-style enum to an integer with as
, but you can't convert in the other direction, e.g. as per this StackOverflow answer. There are proc macros to do it, but that answer pointed out an alternative that is suitable here: transmute
is fine so long as the enum is repr(uN)
for some value of N
. So I will do that with repr(u8)
, which will cut over 100 lines of code, yay.
// bogus "macro-expanded `macro_export` macros from the current crate cannot be | ||
// referred to by absolute paths" error, ugh. See #52234. | ||
#[cfg_attr(rustfmt, rustfmt::skip)] | ||
macro_rules! exp { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to make sure I understand, exp
and Exp*
stands for "expected"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
(Plus) => { exp!(@binop, Plus) }; | ||
(Minus) => { exp!(@binop, Minus) }; | ||
(Star) => { exp!(@binop, Star) }; | ||
(And) => { exp!(@binop, And) }; | ||
(Or) => { exp!(@binop, Or) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm half tempted at making this support things like exp!(+)
and exp!(?)
😈
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was initially going to do that. At one point I had exp!(_)
instead of exp!(Underscore)
and I was using actual keywords like exp!(if)
instead of exp!(If)
. A lot of tokens can be made to work but all the delimiters absolutely cannot, so I decided to use StudlyCaps
names for all of them :)
I'll finish reviewing tomorrow |
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
The parser pushes a `TokenType` to `Parser::expected_token_types` on every call to the various `check`/`eat` methods, and clears it on every call to `bump`. Some of those `TokenType` values are full tokens that require cloning and dropping. This is a *lot* of work for something that is only used in error messages and it accounts for a significant fraction of parsing execution time. This commit overhauls `TokenType` so that `Parser::expected_token_types` can be implemented as a bitset. This requires changing `TokenType` to a C-style parameterless enum, and adding `TokenTypeSet` which uses a `u128` for the bits. (The new `TokenType` has 105 variants.) The new types `ExpTokenPair` and `ExpKeywordPair` are now arguments to the `check`/`eat` methods. This is for maximum speed. The elements in the pairs are always statically known; e.g. a `token::BinOp(token::Star)` is always paired with a `TokenType::Star`. So we now compute `TokenType`s in advance and pass them in to `check`/`eat` rather than the current approach of constructing them on insertion into `expected_token_types`. Values of these pair types can be produced by the new `exp!` macro, which is used at every `check`/`eat` call site. The macro is for convenience, allowing any pair to be generated from a single identifier. The ident/keyword filtering in `expected_one_of_not_found` is no longer necessary. It was there to account for some sloppiness in `TokenKind`/`TokenType` comparisons. The existing `TokenType` is moved to a new file `token_type.rs`, and all its new infrastructure is added to that file. There is more boilerplate code than I would like, but I can't see how to make it shorter.
Finished benchmarking commit (26060e6): comparison URL. Overall result: ✅ improvements - no action neededBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. @bors rollup=never Instruction countThis is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.
Max RSS (memory usage)Results (primary -0.9%, secondary 0.8%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResults (primary 1.4%, secondary 2.4%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 767.635s -> 770.294s (0.35%) |
f5482df
to
a353d56
Compare
I updated, adding a new commit that uses |
The constant pushing/clearing of
Parser::expected_tokens
during parsing is slow. This PR speeds it up greatly.r? @estebank