Skip to content

Commit 548067e

Browse files
committed
Remove StringReader::terminator.
It's silly for a hot function like `bump()` to have such an expensive bounds check. This patch replaces terminator with `end_src_index`. Note that the `self.terminator` check in `is_eof()` wasn't necessary because of the way `StringReader` is initialized.
1 parent 7a090fb commit 548067e

File tree

1 file changed

+11
-20
lines changed
  • src/libsyntax/parse/lexer

1 file changed

+11
-20
lines changed

src/libsyntax/parse/lexer/mod.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ pub struct StringReader<'a> {
4949
/// The current character (which has been read from self.pos)
5050
pub ch: Option<char>,
5151
pub filemap: Lrc<syntax_pos::FileMap>,
52-
/// If Some, stop reading the source at this position (inclusive).
53-
pub terminator: Option<BytePos>,
52+
/// Stop reading src at this index.
53+
pub end_src_index: usize,
5454
/// Whether to record new-lines and multibyte chars in filemap.
5555
/// This is only necessary the first time a filemap is lexed.
5656
/// If part of a filemap is being re-lexed, this should be set to false.
@@ -113,14 +113,7 @@ impl<'a> StringReader<'a> {
113113
self.unwrap_or_abort(res)
114114
}
115115
fn is_eof(&self) -> bool {
116-
if self.ch.is_none() {
117-
return true;
118-
}
119-
120-
match self.terminator {
121-
Some(t) => self.next_pos > t,
122-
None => false,
123-
}
116+
self.ch.is_none()
124117
}
125118
/// Return the next token. EFFECT: advances the string_reader.
126119
pub fn try_next_token(&mut self) -> Result<TokenAndSpan, ()> {
@@ -185,7 +178,7 @@ impl<'a> StringReader<'a> {
185178
col: CharPos(0),
186179
ch: Some('\n'),
187180
filemap,
188-
terminator: None,
181+
end_src_index: src.len(),
189182
save_new_lines_and_multibyte: true,
190183
// dummy values; not read
191184
peek_tok: token::Eof,
@@ -222,7 +215,7 @@ impl<'a> StringReader<'a> {
222215
// Seek the lexer to the right byte range.
223216
sr.save_new_lines_and_multibyte = false;
224217
sr.next_pos = span.lo();
225-
sr.terminator = Some(span.hi());
218+
sr.end_src_index = sr.src_index(span.hi());
226219

227220
sr.bump();
228221

@@ -441,8 +434,7 @@ impl<'a> StringReader<'a> {
441434
/// discovered, add it to the FileMap's list of line start offsets.
442435
pub fn bump(&mut self) {
443436
let next_src_index = self.src_index(self.next_pos);
444-
let end_src_index = self.terminator.map_or(self.src.len(), |t| self.src_index(t));
445-
if next_src_index < end_src_index {
437+
if next_src_index < self.end_src_index {
446438
let next_ch = char_at(&self.src, next_src_index);
447439
let next_ch_len = next_ch.len_utf8();
448440

@@ -472,7 +464,7 @@ impl<'a> StringReader<'a> {
472464

473465
pub fn nextch(&self) -> Option<char> {
474466
let next_src_index = self.src_index(self.next_pos);
475-
if next_src_index < self.src.len() {
467+
if next_src_index < self.end_src_index {
476468
Some(char_at(&self.src, next_src_index))
477469
} else {
478470
None
@@ -485,13 +477,12 @@ impl<'a> StringReader<'a> {
485477

486478
pub fn nextnextch(&self) -> Option<char> {
487479
let next_src_index = self.src_index(self.next_pos);
488-
let s = &self.src[..];
489-
if next_src_index >= s.len() {
480+
if next_src_index >= self.end_src_index {
490481
return None;
491482
}
492-
let next_next_src_index = next_src_index + char_at(s, next_src_index).len_utf8();
493-
if next_next_src_index < s.len() {
494-
Some(char_at(s, next_next_src_index))
483+
let next_next_src_index = next_src_index + char_at(&self.src, next_src_index).len_utf8();
484+
if next_next_src_index < self.end_src_index {
485+
Some(char_at(&self.src, next_next_src_index))
495486
} else {
496487
None
497488
}

0 commit comments

Comments
 (0)