perf: pre-allocate collections with known capacity in hot paths

- Use with_capacity for chunk_by_source, results, per_entity_traces,
  and selected_chunks in assemble() where bound is known
- Pre-allocate tokens/terms vectors in normalize_fts_query and
  extract_keywords based on input length
- Pre-allocate neighbor_ids, seen, and ordered in graph expansion
  based on relationship count
This commit is contained in:
Per Stark
2026-05-26 15:49:01 +02:00
parent 6c7b586fc5
commit b4383bb227
2 changed files with 16 additions and 10 deletions
+3 -3
View File
@@ -44,8 +44,8 @@ pub async fn find_entities_by_relationship_by_id(
return Ok(Vec::new());
}
let mut neighbor_ids: Vec<String> = Vec::new();
let mut seen: HashSet<String> = HashSet::new();
let mut neighbor_ids: Vec<String> = Vec::with_capacity(relationships.len());
let mut seen: HashSet<String> = HashSet::with_capacity(relationships.len());
for rel in relationships {
if rel.in_ == entity_id {
if seen.insert(rel.out.clone()) {
@@ -97,7 +97,7 @@ pub async fn find_entities_by_relationship_by_id(
.map(|entity| (entity.id.clone(), entity))
.collect();
let mut ordered = Vec::new();
let mut ordered = Vec::with_capacity(neighbor_ids.len());
for id in neighbor_ids {
if let Some(entity) = neighbor_map.remove(&id) {
ordered.push(entity);