#distance #ann #algorithm

anndists

some distances used in Ann related crates

4 releases

Uses new Rust 2024

0.1.3 Jun 11, 2025
0.1.2 May 13, 2024
0.1.1 Apr 29, 2024
0.1.0 Apr 29, 2024

#358 in Algorithms

Download history 6536/week @ 2025-10-13 5182/week @ 2025-10-20 6102/week @ 2025-10-27 7093/week @ 2025-11-03 5171/week @ 2025-11-10 4107/week @ 2025-11-17 4460/week @ 2025-11-24 5171/week @ 2025-12-01 6082/week @ 2025-12-08 7530/week @ 2025-12-15 6240/week @ 2025-12-22 4270/week @ 2025-12-29 9830/week @ 2026-01-05 7914/week @ 2026-01-12 5744/week @ 2026-01-19 8026/week @ 2026-01-26

32,055 downloads per month
Used in 60 crates (9 directly)

MIT/Apache

72KB
1.5K SLoC

anndists

This crate provides distances computations used in some related crates hnsw_rs, annembed and coreset

All distances implement the trait Distance:

pub trait Distance<T: Send + Sync> {  
    fn eval(&self, va: &[T], vb: &[T]) -> f32;
}

Functionalities

The crate provides:

  • usual distances as L1, L2, Cosine, Jaccard, Hamming for vectors of standard numeric types, Levenshtein distance on u16.

  • Hellinger distance and Jeffreys divergence between probability distributions (f32 and f64). It must be noted that the Jeffreys divergence (a symetrized Kullback-Leibler divergence) do not satisfy the triangle inequality. (Neither Cosine distance !).

  • Jensen-Shannon distance between probability distributions (f32 and f64). It is defined as the square root of the Jensen-Shannon divergence and is a bounded metric. See Nielsen F. in Entropy 2019, 21(5), 485.

  • A Trait to enable the user to implement its own distances. It takes as data slices of types T satisfying T:Serialize+Clone+Send+Sync. It is also possible to use C extern functions or closures.

  • Simd implementation is provided for the most often used case.

Implementation

Simd support is provided with the simdeez crate on Intel and partial implementation with std::simd for general case.

Building

Simd

  • The simd provided by the simdeez crate is accessible with the feature "simdeez_f" for x86_64 processors. Compile with cargo build --release --features "simdeez_f" .... To compile this crate on a M1 chip just do not activate this feature.

  • It is nevertheless possible to experiment with std::simd. Compiling with the feature stdsimd (cargo build --release --features "stdsimd"), activates the portable_simd feature on rust nightly. This requires nightly compiler. Only the Hamming distance with the u32x16 and u64x8 types and DistL1,DistL2 and DistDot on f32*16 are provided for now.

Benchmarks and Examples

The speed is illustated in the hnsw_rs, annembed crates

Changes

Version 0.1.3 switched to edition=2024

Contributions

Petter Egesund added the DistLevenshtein distance.

License

Licensed under either of

at your option.

Dependencies

~1.5–2.1MB
~38K SLoC