Skip to content

Commit 145b847

Browse files
committed
Sometimes skip over tokens in parse_token_tree.
This sometimes avoids a lot of `bump` calls.
1 parent 2fd855f commit 145b847

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,12 @@ impl TokenTreeCursor {
907907
pub fn bump(&mut self) {
908908
self.index += 1;
909909
}
910+
911+
// For skipping ahead in rare circumstances.
912+
#[inline]
913+
pub fn bump_to_end(&mut self) {
914+
self.index = self.stream.len();
915+
}
910916
}
911917

912918
/// A `TokenStream` cursor that produces `Token`s. It's a bit odd that

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,15 +1389,26 @@ impl<'a> Parser<'a> {
13891389
// matching `CloseDelim` we are *after* the delimited sequence,
13901390
// i.e. at depth `d - 1`.
13911391
let target_depth = self.token_cursor.stack.len() - 1;
1392-
loop {
1393-
// Advance one token at a time, so `TokenCursor::next()`
1394-
// can capture these tokens if necessary.
1392+
1393+
if let Capturing::No = self.capture_state.capturing {
1394+
// We are not capturing tokens, so skip to the end of the
1395+
// delimited sequence. This is a perf win when dealing with
1396+
// declarative macros that pass large `tt` fragments through
1397+
// multiple rules, as seen in the uom-0.37.0 crate.
1398+
self.token_cursor.curr.bump_to_end();
13951399
self.bump();
1396-
if self.token_cursor.stack.len() == target_depth {
1397-
debug_assert!(self.token.kind.close_delim().is_some());
1398-
break;
1400+
debug_assert_eq!(self.token_cursor.stack.len(), target_depth);
1401+
} else {
1402+
loop {
1403+
// Advance one token at a time, so `TokenCursor::next()`
1404+
// can capture these tokens if necessary.
1405+
self.bump();
1406+
if self.token_cursor.stack.len() == target_depth {
1407+
break;
1408+
}
13991409
}
14001410
}
1411+
debug_assert!(self.token.kind.close_delim().is_some());
14011412

14021413
// Consume close delimiter
14031414
self.bump();

0 commit comments

Comments
 (0)