From b8f52210f973b0080d3f334af92abf9c9d27f898 Mon Sep 17 00:00:00 2001 From: WillLillis Date: Sun, 5 Oct 2025 01:42:01 -0400 Subject: [PATCH] perf: reduce needless allocations --- crates/cli/src/parse.rs | 10 ++++++---- crates/generate/src/render.rs | 12 ++++++------ crates/highlight/src/highlight.rs | 8 +++++--- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/crates/cli/src/parse.rs b/crates/cli/src/parse.rs index 4b941fe3..84bf0e85 100644 --- a/crates/cli/src/parse.rs +++ b/crates/cli/src/parse.rs @@ -674,10 +674,9 @@ pub fn parse_file_at_path( width = max_path_length )?; if let Some(node) = first_error { - let start = node.start_position(); - let end = node.end_position(); - let mut node_text = String::new(); - for c in node.kind().chars() { + let node_kind = node.kind(); + let mut node_text = String::with_capacity(node_kind.len()); + for c in node_kind.chars() { if let Some(escaped) = escape_invisible(c) { node_text += escaped; } else { @@ -694,6 +693,9 @@ pub fn parse_file_at_path( } else { write!(&mut stdout, "{node_text}")?; } + + let start = node.start_position(); + let end = node.end_position(); write!( &mut stdout, " [{}, {}] - [{}, {}])", diff --git a/crates/generate/src/render.rs b/crates/generate/src/render.rs index e4aea008..bcfc832e 100644 --- a/crates/generate/src/render.rs +++ b/crates/generate/src/render.rs @@ -34,6 +34,8 @@ macro_rules! add { macro_rules! add_whitespace { ($this:tt) => {{ + // 4 bytes per char, 2 spaces per indent level + $this.buffer.reserve(4 * 2 * $this.indent_level); for _ in 0..$this.indent_level { write!(&mut $this.buffer, " ").unwrap(); } @@ -688,13 +690,14 @@ impl Generator { flat_field_map.push((field_name.clone(), *location)); } } + let field_map_len = flat_field_map.len(); field_map_ids.push(( self.get_field_map_id( - flat_field_map.clone(), + flat_field_map, &mut flat_field_maps, &mut next_flat_field_map_index, ), - flat_field_map.len(), + field_map_len, )); } } @@ -962,10 +965,7 @@ impl Generator { large_char_set_ix = Some(char_set_ix); } - let mut line_break = "\n".to_string(); - for _ in 0..self.indent_level + 2 { - line_break.push_str(" "); - } + let line_break = format!("\n{}", " ".repeat(self.indent_level + 2)); let has_positive_condition = large_char_set_ix.is_some() || !asserted_chars.is_empty(); let has_negative_condition = !negated_chars.is_empty(); diff --git a/crates/highlight/src/highlight.rs b/crates/highlight/src/highlight.rs index bb81fc08..9a78d1ac 100644 --- a/crates/highlight/src/highlight.rs +++ b/crates/highlight/src/highlight.rs @@ -344,11 +344,13 @@ impl HighlightConfiguration { locals_query: &str, ) -> Result { // Concatenate the query strings, keeping track of the start offset of each section. - let mut query_source = String::new(); + let mut query_source = String::with_capacity( + injection_query.len() + locals_query.len() + highlights_query.len(), + ); query_source.push_str(injection_query); - let locals_query_offset = query_source.len(); + let locals_query_offset = injection_query.len(); query_source.push_str(locals_query); - let highlights_query_offset = query_source.len(); + let highlights_query_offset = injection_query.len() + locals_query.len(); query_source.push_str(highlights_query); // Construct a single query by concatenating the three query strings, but record the