Skip to content

Commit bec7474

Browse files
committed
Auto merge of #145348 - nnethercote:parse_token_tree-speedup-for-uom, r=petrochenkov
Sometimes skip over tokens in `parse_token_tree`. r? `@petrochenkov`
2 parents 22a86f8 + 145b847 commit bec7474

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
@@ -1388,15 +1388,26 @@ impl<'a> Parser<'a> {
13881388
// matching `CloseDelim` we are *after* the delimited sequence,
13891389
// i.e. at depth `d - 1`.
13901390
let target_depth = self.token_cursor.stack.len() - 1;
1391-
loop {
1392-
// Advance one token at a time, so `TokenCursor::next()`
1393-
// can capture these tokens if necessary.
1391+
1392+
if let Capturing::No = self.capture_state.capturing {
1393+
// We are not capturing tokens, so skip to the end of the
1394+
// delimited sequence. This is a perf win when dealing with
1395+
// declarative macros that pass large `tt` fragments through
1396+
// multiple rules, as seen in the uom-0.37.0 crate.
1397+
self.token_cursor.curr.bump_to_end();
13941398
self.bump();
1395-
if self.token_cursor.stack.len() == target_depth {
1396-
debug_assert!(self.token.kind.close_delim().is_some());
1397-
break;
1399+
debug_assert_eq!(self.token_cursor.stack.len(), target_depth);
1400+
} else {
1401+
loop {
1402+
// Advance one token at a time, so `TokenCursor::next()`
1403+
// can capture these tokens if necessary.
1404+
self.bump();
1405+
if self.token_cursor.stack.len() == target_depth {
1406+
break;
1407+
}
13981408
}
13991409
}
1410+
debug_assert!(self.token.kind.close_delim().is_some());
14001411

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

0 commit comments

Comments
 (0)