Unnamed repository; edit this file 'description' to name the repository.
perf: Pre-allocate some buffers in parsing
Lukas Wirth 4 months ago
parent 7406f46 · commit 4b63998
-rw-r--r--crates/parser/src/input.rs2
-rw-r--r--crates/parser/src/lexed_str.rs7
-rw-r--r--crates/parser/src/parser.rs2
-rw-r--r--crates/span/src/ast_id.rs5
4 files changed, 11 insertions, 5 deletions
diff --git a/crates/parser/src/input.rs b/crates/parser/src/input.rs
index 57eeb431cd..42e8a400ed 100644
--- a/crates/parser/src/input.rs
+++ b/crates/parser/src/input.rs
@@ -97,7 +97,7 @@ impl Input {
let b_idx = n % (bits::BITS as usize);
(idx, b_idx)
}
- fn len(&self) -> usize {
+ pub fn len(&self) -> usize {
self.kind.len()
}
}
diff --git a/crates/parser/src/lexed_str.rs b/crates/parser/src/lexed_str.rs
index 7c78ba8faf..d7eec6cde8 100644
--- a/crates/parser/src/lexed_str.rs
+++ b/crates/parser/src/lexed_str.rs
@@ -150,7 +150,12 @@ struct Converter<'a> {
impl<'a> Converter<'a> {
fn new(edition: Edition, text: &'a str) -> Self {
Self {
- res: LexedStr { text, kind: Vec::new(), start: Vec::new(), error: Vec::new() },
+ res: LexedStr {
+ text,
+ kind: Vec::with_capacity(text.len() / 3),
+ start: Vec::with_capacity(text.len() / 3),
+ error: Vec::new(),
+ },
offset: 0,
edition,
}
diff --git a/crates/parser/src/parser.rs b/crates/parser/src/parser.rs
index c41bd593c6..4557078de9 100644
--- a/crates/parser/src/parser.rs
+++ b/crates/parser/src/parser.rs
@@ -32,7 +32,7 @@ const PARSER_STEP_LIMIT: usize = if cfg!(debug_assertions) { 150_000 } else { 15
impl<'t> Parser<'t> {
pub(super) fn new(inp: &'t Input) -> Parser<'t> {
- Parser { inp, pos: 0, events: Vec::new(), steps: Cell::new(0) }
+ Parser { inp, pos: 0, events: Vec::with_capacity(2 * inp.len()), steps: Cell::new(0) }
}
pub(crate) fn finish(self) -> Vec<Event> {
diff --git a/crates/span/src/ast_id.rs b/crates/span/src/ast_id.rs
index e54e0bd2fc..3bf14dea75 100644
--- a/crates/span/src/ast_id.rs
+++ b/crates/span/src/ast_id.rs
@@ -603,8 +603,9 @@ impl AstIdMap {
// After all, the block will then contain the *outer* item, so we allocate
// an ID for it anyway.
let mut blocks = Vec::new();
- let mut curr_layer = vec![(node.clone(), None)];
- let mut next_layer = vec![];
+ let mut curr_layer = Vec::with_capacity(32);
+ curr_layer.push((node.clone(), None));
+ let mut next_layer = Vec::with_capacity(32);
while !curr_layer.is_empty() {
curr_layer.drain(..).for_each(|(node, parent_idx)| {
let mut preorder = node.preorder();