-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Optimize rustc_lexer
#91393
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
Merged
Merged
Optimize rustc_lexer
#91393
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,10 +2,11 @@ use std::str::Chars; | |||||
|
||||||
/// Peekable iterator over a char sequence. | ||||||
/// | ||||||
/// Next characters can be peeked via `nth_char` method, | ||||||
/// Next characters can be peeked via `first` method, | ||||||
/// and position can be shifted forward via `bump` method. | ||||||
pub(crate) struct Cursor<'a> { | ||||||
initial_len: usize, | ||||||
/// Iterator over chars. Slightly faster than a &str. | ||||||
chars: Chars<'a>, | ||||||
#[cfg(debug_assertions)] | ||||||
prev: char, | ||||||
|
@@ -37,22 +38,21 @@ impl<'a> Cursor<'a> { | |||||
} | ||||||
} | ||||||
|
||||||
/// Returns nth character relative to the current cursor position. | ||||||
/// Peeks the next symbol from the input stream without consuming it. | ||||||
/// If requested position doesn't exist, `EOF_CHAR` is returned. | ||||||
/// However, getting `EOF_CHAR` doesn't always mean actual end of file, | ||||||
/// it should be checked with `is_eof` method. | ||||||
fn nth_char(&self, n: usize) -> char { | ||||||
self.chars().nth(n).unwrap_or(EOF_CHAR) | ||||||
} | ||||||
|
||||||
/// Peeks the next symbol from the input stream without consuming it. | ||||||
pub(crate) fn first(&self) -> char { | ||||||
self.nth_char(0) | ||||||
// `.next()` optimizes better than `.nth(0)` | ||||||
self.chars.clone().next().unwrap_or(EOF_CHAR) | ||||||
} | ||||||
|
||||||
/// Peeks the second symbol from the input stream without consuming it. | ||||||
pub(crate) fn second(&self) -> char { | ||||||
self.nth_char(1) | ||||||
// `.next()` optimizes better than `.nth(1)` | ||||||
let mut iter = self.chars.clone(); | ||||||
iter.next(); | ||||||
iter.next().unwrap_or(EOF_CHAR) | ||||||
Julian-Wollersberger marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
|
||||||
/// Checks if there is nothing more to consume. | ||||||
|
@@ -65,9 +65,9 @@ impl<'a> Cursor<'a> { | |||||
self.initial_len - self.chars.as_str().len() | ||||||
} | ||||||
|
||||||
/// Returns a `Chars` iterator over the remaining characters. | ||||||
fn chars(&self) -> Chars<'a> { | ||||||
self.chars.clone() | ||||||
/// Resets the number of bytes consumed to 0. | ||||||
pub(crate) fn reset_len_consumed(&mut self) { | ||||||
self.initial_len = self.chars.as_str().len(); | ||||||
} | ||||||
|
||||||
/// Moves to the next character. | ||||||
|
@@ -81,4 +81,13 @@ impl<'a> Cursor<'a> { | |||||
|
||||||
Some(c) | ||||||
} | ||||||
|
||||||
/// Eats symbols while predicate returns true or until the end of file is reached. | ||||||
pub(crate) fn eat_while(&mut self, mut predicate: impl FnMut(char) -> bool) { | ||||||
// It was tried making optimized version of this for eg. line comments, but | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would have worked to. |
||||||
// LLVM can inline all of this and compile it down to fast iteration over bytes. | ||||||
while predicate(self.first()) && !self.is_eof() { | ||||||
self.bump(); | ||||||
} | ||||||
} | ||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.