pub trait Quantizer: Send + Sync {
type Code: Clone + Send + Sync;
// Required methods
fn encode(&self, vector: &[f32]) -> Self::Code;
fn distance_approx(&self, a: &Self::Code, b: &Self::Code) -> f32;
fn distance_exact(&self, a: &[f32], b: &[f32]) -> f32;
fn is_accelerated(&self) -> bool;
fn name(&self) -> &str;
// Provided methods
fn needs_training(&self) -> bool { ... }
fn train(&mut self, _sample: &[&[f32]]) { ... }
}Expand description
Acceleration strategy for distance computations.
Implementations encode vectors into compact codes and provide fast approximate distance between codes.
The graph uses distance_approx for candidate exploration (hot path)
and distance_exact for final neighbor selection (quality-critical).
Required Associated Types§
Required Methods§
Sourcefn encode(&self, vector: &[f32]) -> Self::Code
fn encode(&self, vector: &[f32]) -> Self::Code
Encode a full-precision vector into a compact code.
Called once per insert. The code is stored alongside the vector.
Sourcefn distance_approx(&self, a: &Self::Code, b: &Self::Code) -> f32
fn distance_approx(&self, a: &Self::Code, b: &Self::Code) -> f32
Fast approximate distance between two codes.
This is the hot path: called O(ef_construction × log N) times per insert.
Must be significantly faster than distance_exact for the acceleration
to be worthwhile.
Sourcefn distance_exact(&self, a: &[f32], b: &[f32]) -> f32
fn distance_exact(&self, a: &[f32], b: &[f32]) -> f32
Exact distance between full-precision vectors.
Used for final neighbor selection (heuristic pruning) where distance quality matters more than speed.
Sourcefn is_accelerated(&self) -> bool
fn is_accelerated(&self) -> bool
Whether this quantizer provides actual acceleration.
When false, distance_approx is unused and the graph
calls distance_exact directly. This avoids storing codes.
Provided Methods§
Sourcefn needs_training(&self) -> bool
fn needs_training(&self) -> bool
Whether this quantizer needs training on a data sample before use.
Product Quantization requires training a codebook; Scalar and Binary don’t.