Skip to content

Commit

Permalink
refactor: minor template optimization to reduce allocs; rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jqnatividad committed Nov 27, 2024
1 parent d16f9d2 commit 12ce22b
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/cmd/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,16 +768,21 @@ fn lookup_filter(
}

let case_sensitive = case_sensitive.unwrap_or(true);
let value_lower = if case_sensitive {
value.to_string()

// Avoid allocating if case-sensitive
let value_compare = if case_sensitive {
value
} else {
value.to_lowercase()
// Only allocate for lowercase when needed
&value.to_lowercase()
};

// safety: FILTER_ERROR was initialized in run section
let filter_error = FILTER_ERROR.get().unwrap();

let mut lowercase_buffer = String::new();
// Reuse buffer for case-insensitive comparisons
let mut lowercase_buffer = String::with_capacity(32); // Pre-allocate reasonable size

Ok(LOOKUP_MAP
.get()
.and_then(|lock| lock.read().ok())
Expand All @@ -791,7 +796,7 @@ fn lookup_filter(
.iter()
.find(|(k, _)| {
util::to_lowercase_into(k, &mut lowercase_buffer);
lowercase_buffer == value_lower
lowercase_buffer == value_compare
})
.and_then(|(_, row)| row.get(field).cloned())
}
Expand All @@ -800,9 +805,9 @@ fn lookup_filter(
if filter_error.is_empty() {
String::new()
} else {
format!(
r#"{filter_error} - lookup: "{lookup_name}" key: "{lookup_key}-{field}" not found for: "{value}""#
)
}
format!(
r#"{filter_error} - lookup: "{lookup_name}" key: "{lookup_key}-{field}" not found for: "{value}""#
)
}
}))
}

0 comments on commit 12ce22b

Please sign in to comment.