Skip to content

Commit acf86a8

Browse files
committed
Add benchmarks to instant-distance-py
1 parent d51b99b commit acf86a8

File tree

7 files changed

+71
-6
lines changed

7 files changed

+71
-6
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
- name: Test Python bindings
5454
run: |
5555
sudo apt-get install -y wamerican
56-
cp target/release/libinstant_distance.so instant-distance-py/test/instant_distance.so
56+
cp target/release/libinstant_distance_py.so instant-distance-py/test/instant_distance.so
5757
PYTHONPATH=instant-distance-py/test/ python3 -m test
5858
5959
lint:

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
test-python:
2-
cargo build --release
3-
cp target/release/libinstant_distance.dylib instant-distance-py/test/instant_distance.so
1+
instant-distance-py/test/instant_distance.so: instant-distance-py/src/lib.rs
2+
RUSTFLAGS="-C target-cpu=native" cargo build --release
3+
([ -f target/release/libinstant_distance_py.dylib ] && cp target/release/libinstant_distance_py.dylib instant-distance-py/test/instant_distance.so) || \
4+
([ -f target/release/libinstant_distance_py.so ] && cp target/release/libinstant_distance_py.so instant-distance-py/test/instant_distance.so)
5+
6+
test-python: instant-distance-py/test/instant_distance.so
47
PYTHONPATH=instant-distance-py/test/ python3 -m test
58

69
clean:

instant-distance-py/Cargo.toml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,23 @@ repository = "https://github.com/InstantDomain/instant-distance"
1111
readme = "../README.md"
1212

1313
[lib]
14+
name = "instant_distance_py"
15+
crate-type = ["cdylib", "lib"]
16+
17+
[package.metadata.maturin]
1418
name = "instant_distance"
15-
crate-type = ["cdylib"]
1619

1720
[dependencies]
1821
bincode = "1.3.1"
1922
instant-distance = { version = "0.6", path = "../instant-distance", features = ["with-serde"] }
2023
pyo3 = { version = "0.18.0", features = ["extension-module"] }
2124
serde = { version = "1", features = ["derive"] }
2225
serde-big-array = "0.5.0"
26+
27+
[dev-dependencies]
28+
bencher = "0.1.5"
29+
rand = { version = "0.8", features = ["small_rng"] }
30+
31+
[[bench]]
32+
name = "all"
33+
harness = false

instant-distance-py/benches/all.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use bencher::{benchmark_group, benchmark_main, Bencher};
2+
3+
use instant_distance::{Builder, Point, Search};
4+
use instant_distance_py::FloatArray;
5+
use rand::{rngs::StdRng, Rng, SeedableRng};
6+
7+
benchmark_main!(benches);
8+
benchmark_group!(benches, distance, build, query);
9+
10+
fn distance(bench: &mut Bencher) {
11+
let mut rng = StdRng::seed_from_u64(SEED);
12+
let point_a = FloatArray([rng.gen(); 300]);
13+
let point_b = FloatArray([rng.gen(); 300]);
14+
15+
bench.iter(|| point_a.distance(&point_b));
16+
}
17+
18+
fn build(bench: &mut Bencher) {
19+
let mut rng = StdRng::seed_from_u64(SEED);
20+
let points = (0..1024)
21+
.into_iter()
22+
.map(|_| FloatArray([rng.gen(); 300]))
23+
.collect::<Vec<_>>();
24+
25+
bench.iter(|| Builder::default().seed(SEED).build_hnsw(points.clone()));
26+
}
27+
28+
fn query(bench: &mut Bencher) {
29+
let mut rng = StdRng::seed_from_u64(SEED);
30+
let points = (0..1024)
31+
.into_iter()
32+
.map(|_| FloatArray([rng.gen(); 300]))
33+
.collect::<Vec<_>>();
34+
let (hnsw, _) = Builder::default().seed(SEED).build_hnsw(points);
35+
let point = FloatArray([rng.gen(); 300]);
36+
37+
bench.iter(|| {
38+
let mut search = Search::default();
39+
let _ = hnsw.search(&point, &mut search);
40+
});
41+
}
42+
43+
const SEED: u64 = 123456789;

instant-distance-py/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ name = "instant-distance"
44
[build-system]
55
requires = ["maturin >= 0.14, < 0.15"]
66
build-backend = "maturin"
7+
8+
[tool.maturin]
9+
python-source = "python"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .instant_distance import *
2+
3+
__doc__ = instant_distance.__doc__
4+
if hasattr(instant_distance, "__all__"):
5+
__all__ = instant_distance.__all__

instant-distance-py/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ impl Neighbor {
348348

349349
#[repr(align(32))]
350350
#[derive(Clone, Deserialize, Serialize)]
351-
struct FloatArray(#[serde(with = "BigArray")] [f32; DIMENSIONS]);
351+
pub struct FloatArray(#[serde(with = "BigArray")] pub [f32; DIMENSIONS]);
352352

353353
impl TryFrom<&PyAny> for FloatArray {
354354
type Error = PyErr;

0 commit comments

Comments
 (0)