Skip to content

Commit

Permalink
Split TokenCursor::{next,next_desugared} into inlined and non-inlin…
Browse files Browse the repository at this point in the history
…ed halves.
  • Loading branch information
nnethercote committed Mar 22, 2022
1 parent 4e700a0 commit f8f1d3f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
11 changes: 6 additions & 5 deletions compiler/rustc_parse/src/parser/attr_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ 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 `next` or `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` 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.
let mut cursor_snapshot = self.cursor_snapshot.clone();
let tokens =
std::iter::once((FlatToken::Token(self.start_token.0.clone()), self.start_token.1))
Expand Down
23 changes: 18 additions & 5 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ struct TokenCursor {
frame: TokenCursorFrame,
stack: Vec<TokenCursorFrame>,
desugar_doc_comments: bool,
// Counts the number of calls to `next` or `next_desugared`,
// depending on whether `desugar_doc_comments` is set.
// Counts the number of calls to `{,inlined_}next` or
// `{,inlined_}next_desugared`, depending on whether
// `desugar_doc_comments` is set.
num_next_calls: usize,
// During parsing, we may sometimes need to 'unglue' a
// glued token into two component tokens
Expand Down Expand Up @@ -256,6 +257,12 @@ impl TokenCursorFrame {

impl TokenCursor {
fn next(&mut self) -> (Token, Spacing) {
self.inlined_next()
}

/// This always-inlined version should only be used on hot code paths.
#[inline(always)]
fn inlined_next(&mut self) -> (Token, Spacing) {
loop {
let (tree, spacing) = if !self.frame.open_delim {
self.frame.open_delim = true;
Expand Down Expand Up @@ -285,7 +292,13 @@ impl TokenCursor {
}

fn next_desugared(&mut self) -> (Token, Spacing) {
let (data, attr_style, sp) = match self.next() {
self.inlined_next_desugared()
}

/// This always-inlined version should only be used on hot code paths.
#[inline(always)]
fn inlined_next_desugared(&mut self) -> (Token, Spacing) {
let (data, attr_style, sp) = match self.inlined_next() {
(Token { kind: token::DocComment(_, attr_style, data), span }, _) => {
(data, attr_style, span)
}
Expand Down Expand Up @@ -467,9 +480,9 @@ impl<'a> Parser<'a> {
fn next_tok(&mut self, fallback_span: Span) -> (Token, Spacing) {
loop {
let (mut next, spacing) = if self.desugar_doc_comments {
self.token_cursor.next_desugared()
self.token_cursor.inlined_next_desugared()
} else {
self.token_cursor.next()
self.token_cursor.inlined_next()
};
self.token_cursor.num_next_calls += 1;
// We've retrieved an token from the underlying
Expand Down

0 comments on commit f8f1d3f

Please sign in to comment.