Skip to content
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 TokenCursor #96210

Merged
merged 20 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ad566b7
Tweak `Cursor::next_with_spacing`.
nnethercote Apr 19, 2022
aefbbee
Inline and remove `TokenTree::{open_tt,close_tt}`.
nnethercote Apr 13, 2022
89ec75b
Inline and remove `Parser::next_tok()`.
nnethercote Apr 14, 2022
b1e6dee
Merge `TokenCursor::{next,next_desugared}`.
nnethercote Apr 19, 2022
0231754
Rearrange `TokenCursor::inlined_next()`.
nnethercote Apr 19, 2022
29c78cc
Add {open,close}_delim arguments to `TokenCursorFrame::new()`.
nnethercote Apr 19, 2022
d235ac7
Handle `Delimited` opening immediately.
nnethercote Apr 19, 2022
f1c32c1
Move desugaring code into its own function.
nnethercote Apr 19, 2022
804103b
Add a size assertion for `Parser`.
nnethercote Apr 19, 2022
86723d3
Use `true` for `open_delim`/`close_delim` in one spot.
nnethercote Apr 20, 2022
3cd5e34
Remove `TokenCursorFrame::open_delim`.
nnethercote Apr 20, 2022
b09522a
Remove the loop from `Parser::bump()`.
nnethercote Apr 20, 2022
5b653c1
Inline `Cursor::next_with_spacing`.
nnethercote Apr 20, 2022
9e6879f
Only record `fallback_span` when necessary.
nnethercote Apr 20, 2022
f9235db
Inline `Parser::parse_nonterminal`.
nnethercote Apr 20, 2022
d2b9bbb
Inline `Parser::nonterminal_may_begin_with`.
nnethercote Apr 20, 2022
880318c
Remove `Eof` sanity check in `Parser::inlined_bump_with`.
nnethercote Apr 20, 2022
7a89255
Avoid some tuple destructuring.
nnethercote Apr 20, 2022
cc4e344
Produce `CloseDelim` and pop the stack at the same time.
nnethercote Apr 21, 2022
643e9f7
Introduced `Cursor::next_with_spacing_ref`.
nnethercote Apr 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions compiler/rustc_ast/src/tokenstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,6 @@ impl TokenTree {
TokenTree::Token(Token::new(kind, span))
}

/// Returns the opening delimiter as a token tree.
pub fn open_tt(span: DelimSpan, delim: DelimToken) -> TokenTree {
TokenTree::token(token::OpenDelim(delim), span.open)
}

/// Returns the closing delimiter as a token tree.
pub fn close_tt(span: DelimSpan, delim: DelimToken) -> TokenTree {
TokenTree::token(token::CloseDelim(delim), span.close)
}

pub fn uninterpolate(self) -> TokenTree {
match self {
TokenTree::Token(token) => TokenTree::Token(token.uninterpolate().into_owned()),
Expand Down Expand Up @@ -585,13 +575,20 @@ impl Cursor {
Cursor { stream, index: 0 }
}

#[inline]
pub fn next_with_spacing(&mut self) -> Option<TreeAndSpacing> {
if self.index < self.stream.len() {
self.stream.0.get(self.index).map(|tree| {
self.index += 1;
Some(self.stream.0[self.index - 1].clone())
} else {
None
}
tree.clone()
})
}

#[inline]
pub fn next_with_spacing_ref(&mut self) -> Option<&TreeAndSpacing> {
self.stream.0.get(self.index).map(|tree| {
self.index += 1;
tree
})
}

pub fn index(&self) -> usize {
Expand Down
17 changes: 6 additions & 11 deletions compiler/rustc_parse/src/parser/attr_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,16 @@ rustc_data_structures::static_assert_size!(LazyTokenStreamImpl, 144);

impl CreateTokenStream for LazyTokenStreamImpl {
fn create_token_stream(&self) -> AttrAnnotatedTokenStream {
// The token produced by the final call to `{,inlined_}next` or
// `{,inlined_}next_desugared` was not actually consumed by the
// callback. The combination of chaining the initial token and using
// `take` produces the desired result - we produce an empty
// `TokenStream` if no calls were made, and omit the final token
// otherwise.
// The token produced by the final call to `{,inlined_}next` was not
// actually consumed by the callback. The combination of chaining the
// initial token and using `take` produces the desired result - we
// produce an empty `TokenStream` if no calls were made, and omit the
// final token otherwise.
let mut cursor_snapshot = self.cursor_snapshot.clone();
let tokens =
std::iter::once((FlatToken::Token(self.start_token.0.clone()), self.start_token.1))
.chain((0..self.num_calls).map(|_| {
let token = if cursor_snapshot.desugar_doc_comments {
cursor_snapshot.next_desugared()
} else {
cursor_snapshot.next()
};
let token = cursor_snapshot.next(cursor_snapshot.desugar_doc_comments);
(FlatToken::Token(token.0), token.1)
}))
.take(self.num_calls);
Expand Down
Loading