Skip to content

Commit 7edf3c8

Browse files
committed
Add initial capacities, use u32s for indents/spaces.
1 parent ecac895 commit 7edf3c8

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

compiler/parser/src/lexer.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use unic_ucd_ident::{is_xid_continue, is_xid_start};
1818

1919
#[derive(Clone, Copy, PartialEq, Debug, Default)]
2020
struct IndentationLevel {
21-
tabs: usize,
22-
spaces: usize,
21+
tabs: u32,
22+
spaces: u32,
2323
}
2424

2525
impl IndentationLevel {
@@ -225,7 +225,8 @@ where
225225
at_begin_of_line: true,
226226
nesting: 0,
227227
indentations: Indentations::default(),
228-
pending: Vec::new(),
228+
// Usually we have less than 5 tokens pending.
229+
pending: Vec::with_capacity(5),
229230
location: start,
230231
window: CharWindow::new(input),
231232
};
@@ -257,13 +258,13 @@ where
257258
};
258259

259260
let start_pos = self.get_pos();
260-
let mut name = String::new();
261+
let mut name = String::with_capacity(8);
261262
while self.is_identifier_continuation() {
262263
name.push(self.next_char().unwrap());
263264
}
264265
let end_pos = self.get_pos();
265266

266-
if let Some(tok) = KEYWORDS.get(name.as_str()) {
267+
if let Some(tok) = KEYWORDS.get(&name) {
267268
Ok((start_pos, tok.clone(), end_pos))
268269
} else {
269270
Ok((start_pos, Tok::Name { name }, end_pos))
@@ -464,7 +465,7 @@ where
464465
self.next_char();
465466
}
466467
let quote_char = self.next_char().unwrap();
467-
let mut string_content = String::new();
468+
let mut string_content = String::with_capacity(5);
468469

469470
// If the next two characters are also the quote character, then we have a triple-quoted
470471
// string; consume those two characters and ensure that we require a triple-quote to close
@@ -567,8 +568,8 @@ where
567568
/// Given we are at the start of a line, count the number of spaces and/or tabs until the first character.
568569
fn eat_indentation(&mut self) -> Result<IndentationLevel, LexicalError> {
569570
// Determine indentation:
570-
let mut spaces: usize = 0;
571-
let mut tabs: usize = 0;
571+
let mut spaces: u32 = 0;
572+
let mut tabs: u32 = 0;
572573
loop {
573574
match self.window[0] {
574575
Some(' ') => {
@@ -1127,7 +1128,7 @@ where
11271128

11281129
fn eat_single_char(&mut self, ty: Tok) {
11291130
let tok_start = self.get_pos();
1130-
self.next_char().unwrap();
1131+
self.next_char();
11311132
let tok_end = self.get_pos();
11321133
self.emit((tok_start, ty, tok_end));
11331134
}

0 commit comments

Comments
 (0)