Skip to content

Commit df594ba

Browse files
Merge pull request #19690 from ChayimFriedman2/preallocate-input
minor: Preallocate `parser::Input`
2 parents 71a3888 + 8bff414 commit df594ba

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

crates/parser/src/input.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ type bits = u64;
1212
/// `Tokens` doesn't include whitespace and comments. Main input to the parser.
1313
///
1414
/// Struct of arrays internally, but this shouldn't really matter.
15-
#[derive(Default)]
1615
pub struct Input {
1716
kind: Vec<SyntaxKind>,
1817
joint: Vec<bits>,
@@ -21,6 +20,14 @@ pub struct Input {
2120

2221
/// `pub` impl used by callers to create `Tokens`.
2322
impl Input {
23+
#[inline]
24+
pub fn with_capacity(capacity: usize) -> Self {
25+
Self {
26+
kind: Vec::with_capacity(capacity),
27+
joint: Vec::with_capacity(capacity / size_of::<bits>()),
28+
contextual_kind: Vec::with_capacity(capacity),
29+
}
30+
}
2431
#[inline]
2532
pub fn push(&mut self, kind: SyntaxKind) {
2633
self.push_impl(kind, SyntaxKind::EOF)

crates/parser/src/shortcuts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub enum StrStep<'a> {
2727
impl LexedStr<'_> {
2828
pub fn to_input(&self, edition: Edition) -> crate::Input {
2929
let _p = tracing::info_span!("LexedStr::to_input").entered();
30-
let mut res = crate::Input::default();
30+
let mut res = crate::Input::with_capacity(self.len());
3131
let mut was_joint = false;
3232
for i in 0..self.len() {
3333
let kind = self.kind(i);

crates/syntax-bridge/src/to_parser_input.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn to_parser_input<Ctx: Copy + fmt::Debug + PartialEq + Eq + Hash>(
1212
buffer: tt::TokenTreesView<'_, SpanData<Ctx>>,
1313
span_to_edition: &mut dyn FnMut(Ctx) -> Edition,
1414
) -> parser::Input {
15-
let mut res = parser::Input::default();
15+
let mut res = parser::Input::with_capacity(buffer.len());
1616

1717
let mut current = buffer.cursor();
1818
let mut syntax_context_to_edition_cache = FxHashMap::default();

0 commit comments

Comments
 (0)