perf(engine): normalize with the already-computed norm in embedding validation

Greptile follow-up: the zero-norm guard computed the norm and then
normalize_vector recomputed it (a wasted pass at 1536+ dims), with the
inner zero guard unreachable by construction. Normalize in place with
the validated norm; normalize_vector stays for its other caller (the
mock provider).
This commit is contained in:
aaltshuler 2026-07-05 14:50:35 +03:00 committed by Andrew Altshuler
parent 4834c6a0dc
commit db96e7000d

View file

@ -579,7 +579,14 @@ fn validate_and_normalize_embedding(
if norm <= f32::EPSILON {
return Err("embedding has zero norm (no direction)".to_string());
}
Ok(normalize_vector(values))
// Normalize in place with the norm just computed — `normalize_vector`
// would recompute it (and its zero guard is unreachable here, since a
// zero norm already returned Err above).
let mut values = values;
for value in &mut values {
*value /= norm;
}
Ok(values)
}
fn normalize_vector(mut values: Vec<f32>) -> Vec<f32> {