This library is a rust port of spotify/annoy , currently only index serving is supported.
A live demo using web assembly is available at https://annoy-web-demo.vercel.app/
It also provides FFI bindings for jvm, dotnet and dart
Metric | Serve | Build | jvm binding | dotnet binding | dart binding | WASM support |
---|---|---|---|---|---|---|
Angular | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ |
Euclidean | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ |
Manhattan | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ |
Dot | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ |
Hamming | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
Install via crates.io
# Cargo.toml
[dependencies]
annoy-rs = "0.1"
use annoy_rs::*;
let index = AnnoyIndex::load(10, "index.ann", IndexType::Angular).unwrap();
let v0 = index.get_item_vector(0);
let nearest = index.get_nearest(v0.as_ref(), 5, -1, true);
SIMD is supported via std::simd
on nightly rust. Note that avx intrinsics need to be enabled explicitly by setting your cpu features in RUSTFLAGS
environment variable.
RUSTFLAGS="-Ctarget-feature=+avx" cargo +nightly build --release
# or
RUSTFLAGS="-Ctarget-cpu=native" cargo +nightly build --release
Install wasm-pack
wasm-pack build
wasm-pack test --node
simd128 is supported in chrome by default.
To enable simd128, build with below command
RUSTFLAGS="-Ctarget-feature=+simd128" cargo +nightly build --release --target wasm32-unknown-unknown
An example site is deployed at https://annoy-web-demo.vercel.app/
Source code is under example/web
It uses JNI bindings to rust crate and is ~5-10x faster than pure java implementation in benchmark scenario
Note that the prebuilt dynamically linked libraries are built with simd support, avx cpu feature is required.
Install via jitpack.io
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.hanabi1224:RuAnnoy:<tag>'
}
val index = AnnoyIndex.tryLoad("index.5d.ann", 5, IndexType.Angular)
Runtimes | Nuget package |
---|---|
RuAnnoy | |
RuAnnoy-Batteries-Windows-x64 | |
RuAnnoy-Batteries-Linux-x64 | |
RuAnnoy-Batteries-Darwin-x64 |
<ItemGroup>
<PackageReference Include="RuAnnoy" Version="*" />
<PackageReference Include="RuAnnoy-Batteries-Windows-x64" Version="*" />
</ItemGroup>
var index = AnnoyIndex.Load("index.5d.ann", 5, IndexType.Angular);
Install via pub.dev
# pubspec.yaml
dependencies:
dart_native_annoy: ^0.1.0
import 'dart:ffi';
import 'package:dart_native_annoy/annoy.dart';
/// Creat factory from DynamicLibrary
final indexFactory = AnnoyIndexFactory(lib: DynamicLibrary.open('libannoy_rs_ffi.so'));
/// Load index
final index = indexFactory.loadIndex(
'index.euclidean.5d.ann', 5, IndexType.Euclidean)!;
print('size: ${index.size}');
final v3 = index.getItemVector(3);
final nearest = index.getNearest(v0, 5, includeDistance: true);
- Index building support
- CLI tool to build index from file