Skip to content

Commit 4b63998

Browse files
committed
perf: Pre-allocate some buffers in parsing
1 parent 7406f46 commit 4b63998

4 files changed

Lines changed: 11 additions & 5 deletions

File tree

crates/parser/src/input.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl Input {
9797
let b_idx = n % (bits::BITS as usize);
9898
(idx, b_idx)
9999
}
100-
fn len(&self) -> usize {
100+
pub fn len(&self) -> usize {
101101
self.kind.len()
102102
}
103103
}

crates/parser/src/lexed_str.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ struct Converter<'a> {
150150
impl<'a> Converter<'a> {
151151
fn new(edition: Edition, text: &'a str) -> Self {
152152
Self {
153-
res: LexedStr { text, kind: Vec::new(), start: Vec::new(), error: Vec::new() },
153+
res: LexedStr {
154+
text,
155+
kind: Vec::with_capacity(text.len() / 3),
156+
start: Vec::with_capacity(text.len() / 3),
157+
error: Vec::new(),
158+
},
154159
offset: 0,
155160
edition,
156161
}

crates/parser/src/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const PARSER_STEP_LIMIT: usize = if cfg!(debug_assertions) { 150_000 } else { 15
3232

3333
impl<'t> Parser<'t> {
3434
pub(super) fn new(inp: &'t Input) -> Parser<'t> {
35-
Parser { inp, pos: 0, events: Vec::new(), steps: Cell::new(0) }
35+
Parser { inp, pos: 0, events: Vec::with_capacity(2 * inp.len()), steps: Cell::new(0) }
3636
}
3737

3838
pub(crate) fn finish(self) -> Vec<Event> {

crates/span/src/ast_id.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,8 +603,9 @@ impl AstIdMap {
603603
// After all, the block will then contain the *outer* item, so we allocate
604604
// an ID for it anyway.
605605
let mut blocks = Vec::new();
606-
let mut curr_layer = vec![(node.clone(), None)];
607-
let mut next_layer = vec![];
606+
let mut curr_layer = Vec::with_capacity(32);
607+
curr_layer.push((node.clone(), None));
608+
let mut next_layer = Vec::with_capacity(32);
608609
while !curr_layer.is_empty() {
609610
curr_layer.drain(..).for_each(|(node, parent_idx)| {
610611
let mut preorder = node.preorder();

0 commit comments

Comments
 (0)