MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / build_distance_table

Method build_distance_table

nodedb-vector/src/quantize/pq.rs:158–174  ·  view source on GitHub ↗

Build an asymmetric distance table for a query vector. Returns `table[sub][centroid]` = distance from query's sub-vector to each centroid. Pre-computing this table makes distance evaluation O(M) per candidate instead of O(D). Charges `m * k * size_of:: ()` bytes to the governor (if set) before allocating the table.

(&self, query: &[f32])

Source from the content-addressed store, hash-verified

156 /// Charges `m * k * size_of::<f32>()` bytes to the governor (if set)
157 /// before allocating the table.
158 pub fn build_distance_table(&self, query: &[f32]) -> Result<Vec<Vec<f32>>, VectorError> {
159 debug_assert_eq!(query.len(), self.dim);
160 let total_bytes = self.m * self.k * size_of::<f32>();
161 let _g = try_reserve_or_skip(&self.governor, total_bytes)?;
162 let mut table = Vec::with_capacity(self.m);
163 for sub in 0..self.m {
164 let offset = sub * self.sub_dim;
165 let sub_query = &query[offset..offset + self.sub_dim];
166 let mut dists = Vec::with_capacity(self.k);
167 for centroid in &self.codebooks[sub] {
168 let d = l2_sub(sub_query, centroid);
169 dists.push(d);
170 }
171 table.push(dists);
172 }
173 Ok(table)
174 }
175
176 /// Compute asymmetric distance using a precomputed distance table.
177 ///

Callers 5

searchMethod · 0.80
quantized_searchFunction · 0.80
prepare_queryMethod · 0.80

Calls 3

try_reserve_or_skipFunction · 0.85
l2_subFunction · 0.85
pushMethod · 0.45