Skip to content

Commit b8f5221

Browse files
WillLillisclason
authored andcommitted
perf: reduce needless allocations
1 parent ecc787e commit b8f5221

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

crates/cli/src/parse.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -674,10 +674,9 @@ pub fn parse_file_at_path(
674674
width = max_path_length
675675
)?;
676676
if let Some(node) = first_error {
677-
let start = node.start_position();
678-
let end = node.end_position();
679-
let mut node_text = String::new();
680-
for c in node.kind().chars() {
677+
let node_kind = node.kind();
678+
let mut node_text = String::with_capacity(node_kind.len());
679+
for c in node_kind.chars() {
681680
if let Some(escaped) = escape_invisible(c) {
682681
node_text += escaped;
683682
} else {
@@ -694,6 +693,9 @@ pub fn parse_file_at_path(
694693
} else {
695694
write!(&mut stdout, "{node_text}")?;
696695
}
696+
697+
let start = node.start_position();
698+
let end = node.end_position();
697699
write!(
698700
&mut stdout,
699701
" [{}, {}] - [{}, {}])",

crates/generate/src/render.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ macro_rules! add {
3434

3535
macro_rules! add_whitespace {
3636
($this:tt) => {{
37+
// 4 bytes per char, 2 spaces per indent level
38+
$this.buffer.reserve(4 * 2 * $this.indent_level);
3739
for _ in 0..$this.indent_level {
3840
write!(&mut $this.buffer, " ").unwrap();
3941
}
@@ -688,13 +690,14 @@ impl Generator {
688690
flat_field_map.push((field_name.clone(), *location));
689691
}
690692
}
693+
let field_map_len = flat_field_map.len();
691694
field_map_ids.push((
692695
self.get_field_map_id(
693-
flat_field_map.clone(),
696+
flat_field_map,
694697
&mut flat_field_maps,
695698
&mut next_flat_field_map_index,
696699
),
697-
flat_field_map.len(),
700+
field_map_len,
698701
));
699702
}
700703
}
@@ -962,10 +965,7 @@ impl Generator {
962965
large_char_set_ix = Some(char_set_ix);
963966
}
964967

965-
let mut line_break = "\n".to_string();
966-
for _ in 0..self.indent_level + 2 {
967-
line_break.push_str(" ");
968-
}
968+
let line_break = format!("\n{}", " ".repeat(self.indent_level + 2));
969969

970970
let has_positive_condition = large_char_set_ix.is_some() || !asserted_chars.is_empty();
971971
let has_negative_condition = !negated_chars.is_empty();

crates/highlight/src/highlight.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,13 @@ impl HighlightConfiguration {
344344
locals_query: &str,
345345
) -> Result<Self, QueryError> {
346346
// Concatenate the query strings, keeping track of the start offset of each section.
347-
let mut query_source = String::new();
347+
let mut query_source = String::with_capacity(
348+
injection_query.len() + locals_query.len() + highlights_query.len(),
349+
);
348350
query_source.push_str(injection_query);
349-
let locals_query_offset = query_source.len();
351+
let locals_query_offset = injection_query.len();
350352
query_source.push_str(locals_query);
351-
let highlights_query_offset = query_source.len();
353+
let highlights_query_offset = injection_query.len() + locals_query.len();
352354
query_source.push_str(highlights_query);
353355

354356
// Construct a single query by concatenating the three query strings, but record the

0 commit comments

Comments
 (0)