Encode a vector: for each subvector, find the nearest centroid index. This is a per-vector hot-path operation. Governor charging is intentionally skipped here to avoid atomic overhead on every candidate during search; use [`encode_batch`] for bulk encoding with budget enforcement.
(&self, vector: &[f32])
| 121 | /// during search; use [`encode_batch`] for bulk encoding with budget |
| 122 | /// enforcement. |
| 123 | pub fn encode(&self, vector: &[f32]) -> Vec<u8> { |
| 124 | debug_assert_eq!(vector.len(), self.dim); |
| 125 | let mut code = Vec::with_capacity(self.m); |
| 126 | for sub in 0..self.m { |
| 127 | let offset = sub * self.sub_dim; |
| 128 | let sub_vec = &vector[offset..offset + self.sub_dim]; |
| 129 | let nearest = self.nearest_centroid(sub, sub_vec); |
| 130 | code.push(nearest as u8); |
| 131 | } |
| 132 | code |
| 133 | } |
| 134 | |
| 135 | /// Batch encode all vectors into a contiguous byte array. |
| 136 | /// |