perf: reduce needless allocations

This commit is contained in:
WillLillis 2025-10-05 01:42:01 -04:00 committed by Christian Clason
parent ecc787e221
commit b8f52210f9
3 changed files with 17 additions and 13 deletions

View file

@ -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,
" [{}, {}] - [{}, {}])",

View file

@ -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();

View file

@ -344,11 +344,13 @@ impl HighlightConfiguration {
locals_query: &str,
) -> Result<Self, QueryError> {
// 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