Merge pull request #21353 from Veykril/push-wquzszzrzwpu

perf: Pre-allocate some buffers in parsing
This commit is contained in:
Lukas Wirth
2025-12-28 10:27:33 +00:00
committed by GitHub
4 changed files with 11 additions and 5 deletions
@@ -97,7 +97,7 @@ fn bit_index(&self, n: usize) -> (usize, usize) {
let b_idx = n % (bits::BITS as usize);
(idx, b_idx)
}
fn len(&self) -> usize {
pub fn len(&self) -> usize {
self.kind.len()
}
}
@@ -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,
}
@@ -32,7 +32,7 @@ pub(crate) struct Parser<'t> {
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> {
@@ -603,8 +603,9 @@ pub fn from_source(node: &SyntaxNode) -> 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();