Skip to content

Commit 1e7d77c

Browse files
committed
perf: reserve Vec capacities where appropriate
1 parent 8184935 commit 1e7d77c

File tree

12 files changed

+32
-25
lines changed

12 files changed

+32
-25
lines changed

crates/cli/src/fuzz.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,9 @@ pub fn fuzz_language_corpus(
220220
}
221221

222222
// Perform a random series of edits and reparse.
223-
let mut undo_stack = Vec::new();
224-
for _ in 0..=rand.unsigned(*EDIT_COUNT) {
223+
let edit_count = rand.unsigned(*EDIT_COUNT);
224+
let mut undo_stack = Vec::with_capacity(edit_count);
225+
for _ in 0..=edit_count {
225226
let edit = get_random_edit(&mut rand, &input);
226227
undo_stack.push(invert_edit(&input, &edit));
227228
perform_edit(&mut tree, &mut input, &edit).unwrap();

crates/cli/src/fuzz/random.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ impl Rand {
2020
}
2121

2222
pub fn words(&mut self, max_count: usize) -> Vec<u8> {
23-
let mut result = Vec::new();
2423
let word_count = self.unsigned(max_count);
24+
let mut result = Vec::with_capacity(2 * word_count);
2525
for i in 0..word_count {
2626
if i > 0 {
2727
if self.unsigned(5) == 0 {

crates/cli/src/highlight.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ impl<'de> Deserialize<'de> for Theme {
8383
let mut styles = Vec::new();
8484
let mut highlight_names = Vec::new();
8585
if let Ok(colors) = BTreeMap::<String, Value>::deserialize(deserializer) {
86-
highlight_names.reserve(colors.len());
8786
styles.reserve(colors.len());
87+
highlight_names.reserve(colors.len());
8888
for (name, style_value) in colors {
8989
let mut style = Style::default();
9090
parse_style(&mut style, style_value);

crates/cli/src/tests/corpus_test.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,9 @@ pub fn test_language_corpus(
243243
}
244244

245245
// Perform a random series of edits and reparse.
246-
let mut undo_stack = Vec::new();
247-
for _ in 0..=rand.unsigned(*EDIT_COUNT) {
246+
let edit_count = rand.unsigned(*EDIT_COUNT);
247+
let mut undo_stack = Vec::with_capacity(edit_count);
248+
for _ in 0..=edit_count {
248249
let edit = get_random_edit(&mut rand, &input);
249250
undo_stack.push(invert_edit(&input, &edit));
250251
perform_edit(&mut tree, &mut input, &edit).unwrap();

crates/cli/src/tests/highlight_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ fn test_highlighting_via_c_api() {
597597
let output_line_offsets =
598598
unsafe { slice::from_raw_parts(output_line_offsets, output_line_count as usize) };
599599

600-
let mut lines = Vec::new();
600+
let mut lines = Vec::with_capacity(output_line_count as usize);
601601
for i in 0..(output_line_count as usize) {
602602
let line_start = output_line_offsets[i] as usize;
603603
let line_end = output_line_offsets

crates/generate/src/build_tables/build_parse_table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ impl<'a> ParseTableBuilder<'a> {
908908

909909
let get_rule_names = |items: &[&ParseItem]| -> Vec<String> {
910910
let mut last_rule_id = None;
911-
let mut result = Vec::new();
911+
let mut result = Vec::with_capacity(items.len());
912912
for item in items {
913913
if last_rule_id == Some(item.variable_index) {
914914
continue;

crates/generate/src/parse_grammar.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,11 @@ pub(crate) fn parse_grammar(input: &str) -> ParseGrammarResult<InputGrammar> {
272272
.reserved
273273
.into_iter()
274274
.map(|(name, rule_values)| {
275-
let mut reserved_words = Vec::new();
276-
277275
let Value::Array(rule_values) = rule_values else {
278276
Err(ParseGrammarError::InvalidReservedWordSet)?
279277
};
280278

279+
let mut reserved_words = Vec::with_capacity(rule_values.len());
281280
for value in rule_values {
282281
reserved_words.push(parse_rule(serde_json::from_value(value)?, false)?);
283282
}

crates/generate/src/prepare_grammar/expand_tokens.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub fn expand_tokens(mut grammar: ExtractedLexicalGrammar) -> ExpandTokensResult
9090
Rule::repeat(Rule::choice(grammar.separators))
9191
};
9292

93-
let mut variables = Vec::new();
93+
let mut variables = Vec::with_capacity(grammar.variables.len());
9494
for (i, variable) in grammar.variables.into_iter().enumerate() {
9595
if variable.rule.is_empty() {
9696
Err(ExpandTokensError::EmptyString(variable.name.clone()))?;
@@ -195,7 +195,7 @@ impl NfaBuilder {
195195
Ok(!s.is_empty())
196196
}
197197
Rule::Choice(elements) => {
198-
let mut alternative_state_ids = Vec::new();
198+
let mut alternative_state_ids = Vec::with_capacity(elements.len());
199199
for element in elements {
200200
if self.expand_rule(element, next_state_id)? {
201201
alternative_state_ids.push(self.nfa.last_state_id());
@@ -338,7 +338,7 @@ impl NfaBuilder {
338338
Ok(result)
339339
}
340340
HirKind::Alternation(alternations) => {
341-
let mut alternative_state_ids = Vec::new();
341+
let mut alternative_state_ids = Vec::with_capacity(alternations.len());
342342
for hir in alternations {
343343
if self.expand_regex(hir, next_state_id)? {
344344
alternative_state_ids.push(self.nfa.last_state_id());

crates/generate/src/prepare_grammar/extract_tokens.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub(super) fn extract_tokens(
8686
// that pointed to that variable will need to be updated to point to the
8787
// variable in the lexical grammar. Symbols that pointed to later variables
8888
// will need to have their indices decremented.
89-
let mut variables = Vec::new();
89+
let mut variables = Vec::with_capacity(grammar.variables.len());
9090
let mut symbol_replacer = SymbolReplacer {
9191
replacements: HashMap::new(),
9292
};
@@ -200,9 +200,9 @@ pub(super) fn extract_tokens(
200200
word_token = Some(token);
201201
}
202202

203-
let mut reserved_word_contexts = Vec::new();
203+
let mut reserved_word_contexts = Vec::with_capacity(grammar.reserved_word_sets.len());
204204
for reserved_word_context in grammar.reserved_word_sets {
205-
let mut reserved_words = Vec::new();
205+
let mut reserved_words = Vec::with_capacity(reserved_word_contexts.len());
206206
for reserved_rule in reserved_word_context.reserved_words {
207207
if let Rule::Symbol(symbol) = reserved_rule {
208208
reserved_words.push(symbol_replacer.replace_symbol(symbol));

crates/generate/src/prepare_grammar/flatten_grammar.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ impl RuleFlattener {
5757
}
5858

5959
fn flatten_variable(&mut self, variable: Variable) -> FlattenGrammarResult<SyntaxVariable> {
60-
let mut productions = Vec::new();
61-
for rule in extract_choices(variable.rule) {
60+
let choices = extract_choices(variable.rule);
61+
let mut productions = Vec::with_capacity(choices.len());
62+
for rule in choices {
6263
let production = self.flatten_rule(rule)?;
6364
if !productions.contains(&production) {
6465
productions.push(production);
@@ -195,7 +196,7 @@ fn extract_choices(rule: Rule) -> Vec<Rule> {
195196
let mut result = vec![Rule::Blank];
196197
for element in elements {
197198
let extraction = extract_choices(element);
198-
let mut next_result = Vec::new();
199+
let mut next_result = Vec::with_capacity(result.len());
199200
for entry in result {
200201
for extraction_entry in &extraction {
201202
next_result.push(Rule::Seq(vec![entry.clone(), extraction_entry.clone()]));
@@ -206,7 +207,7 @@ fn extract_choices(rule: Rule) -> Vec<Rule> {
206207
result
207208
}
208209
Rule::Choice(elements) => {
209-
let mut result = Vec::new();
210+
let mut result = Vec::with_capacity(elements.len());
210211
for element in elements {
211212
for rule in extract_choices(element) {
212213
result.push(rule);

0 commit comments

Comments
 (0)