Skip to content

Commit 41fc4b9

Browse files
committed
optimize cursor bumping in the lexer
1 parent f896f50 commit 41fc4b9

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

compiler/rustc_lexer/src/cursor.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,17 @@ impl<'a> Cursor<'a> {
9191
// It was tried making optimized version of this for eg. line comments, but
9292
// LLVM can inline all of this and compile it down to fast iteration over bytes.
9393
while predicate(self.first()) && !self.is_eof() {
94-
self.bump();
94+
#[cfg(debug_assertions)]
95+
{
96+
self.bump();
97+
}
98+
99+
#[cfg(not(debug_assertions))]
100+
{
101+
// SAFETY: we checked it's not EOF therefore there must be
102+
// at least 1 char present.
103+
unsafe { self.chars.advance_by(1).unwrap_unchecked() };
104+
}
95105
}
96106
}
97107
}

compiler/rustc_lexer/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
//! lexeme types.
1919
//!
2020
//! [`rustc_parse::lexer`]: ../rustc_parse/lexer/index.html
21+
#![feature(iter_advance_by)]
2122
#![deny(rustc::untranslatable_diagnostic)]
2223
#![deny(rustc::diagnostic_outside_of_impl)]
2324
// We want to be able to build this crate with a stable compiler, so no

0 commit comments

Comments
 (0)