Skip to main content

flannrust

CI License: BSD-2-Clause crates.io docs.rs PyPI

A Rust port of nanoflann (the C++ kd-tree library), targeting bit-exact result parity with the C++ reference and equal-or-better speed. Static and dynamic indexes, with Python bindings.

Features

  • Bit-exact parity with nanoflann 1.12.1 — every result index, distance, and internal tree permutation is cross-validated in-process against the vendored C++ reference on every commit (docs/testing.md)
  • Static kd-tree (KdTree) and dynamic Bentley–Saxe forest (DynamicKdTree) with point add/remove after construction
  • Matches or beats the C++ on most benchmarked workloads (table below)
  • L1 / L2 / L2-simple / SO2 / SO3 metrics; f32/f64; compile-time (ConstDim) or runtime (DynDim) dimension; u32/u64/usize indices
  • Optional parallel build via rayon (default feature parallel)
  • Python bindings: flannrust.KDTree / DynamicKDTree, NumPy in/out, GIL released during build and query
  • Exactly two unsafe blocks, both miri-verified in CI

Installation

Not yet published to crates.io / PyPI — until then, use git / build from source.

Rust:

[dependencies]
flannrust = { git = "https://github.com/sitzikbs/flannrust" }

Python (from a clone, inside a virtualenv):

pip install maturin numpy
maturin develop -m crates/flannrust-py/Cargo.toml --release

MSRV: Rust 1.98.0 (pinned in rust-toolchain.toml).

Quick start

Rust

use flannrust::{ConstDim, KdTreeBuilder};

let pts: &[[f64; 3]] = &[
    [0.0, 0.0, 0.0],
    [10.0, 10.0, 10.0],
    [1.0, 1.0, 1.0],
];
let tree = KdTreeBuilder::new(ConstDim::<3>, pts).build();

let mut indices = [0u32; 2];
let mut dists = [0.0f64; 2];
let found = tree.knn_search(&[0.1, 0.1, 0.1], &mut indices, &mut dists);

assert_eq!(found, 2);
assert_eq!(indices[0], 0); // nearest point is [0.0, 0.0, 0.0]

Python

import numpy as np
import flannrust

pts = np.random.default_rng(0).uniform(-10, 10, size=(100_000, 3)).astype(np.float32)
tree = flannrust.KDTree(pts, leaf_size=10, metric="l2", threads=None)

q = np.array([0.0, 0.0, 0.0], dtype=np.float32)
dists, idxs = tree.query(q, k=5)               # dists are SQUARED l2
r_idxs, r_dists = tree.query_radius(q, r=4.0)  # r is SQUARED too, strict `<`

dyn = flannrust.DynamicKDTree(dim=3, dtype="float32")
dyn.add_points(pts)
dyn.remove_point(0)                            # lazy tombstone
dists, idxs = dyn.query(q, k=5)

Distances and radii are SQUARED for l2/l2_simple — unlike scipy.spatial.cKDTree. Square your radius before calling; expect squared values back (l1 is an unsquared sum of absolute differences). This is the single most common mistake porting code from cKDTree.

Performance

Six gated workloads vs. the vendored C++ oracle, ratio = Rust time / C++ time (lower is better for Rust). Latest idle-host re-measurement (M2.6, 3 sessions, statistical harness):

Workload Ratio (Rust / C++)
build 100k, dim 3, f32, sequential 0.99–1.01
knn, fixed dim 3, f32, k=10 1.01–1.04
knn, runtime dim 8, f64, k=10 0.93–0.94
radius, dim 3, f32 0.83–0.87
dynamic add 20k, dim 3, f32 1.03–1.04
dynamic knn after churn, dim 3, f32 0.93–0.96

Measured on WSL2, AMD Ryzen 7 9800X3D, rustc 1.98.0, -C target-cpu=native vs. C++ -O3 -march=native -ffp-contract=off. Full methodology, history, honest residuals, and a portable repro kit: docs/benchmarks.md, docs/EXPERIMENTS.md, docs/benchkit.md.

Documentation

  • API docs: cargo doc -p flannrust --open (docs.rs after publish); Python docstrings on every class/method
  • docs/semantics.md — exact behavioral contracts, deliberate deviations from C++, input domain, feature flags, dynamic adaptor and Python API details
  • docs/testing.md — how bit-exact parity is verified (cross-validation matrix, dynamic op-sequence suite, miri, canaries)
  • docs/benchmarks.md / docs/benchkit.md — the numbers and how to reproduce them on your hardware
  • docs/ROADMAP.md — what's next (M3 incremental adaptor, M4 multithreaded wrapper, serialization)
  • CONTRIBUTING.md — dev setup; note that cross-validation against the C++ oracle needs a C++17 compiler (cargo test --workspace)

How this was built

Every line of Rust, C++ FFI, and Python-binding code here was written by an AI agent (Claude Code), directed and reviewed by Itzik Ben-Shabat. Correctness does not rest on human code review — it rests on the bit-exact cross-validation suite run against the real C++ library on every change, and every performance figure traces to a pasted, reproducible run. The full process record — plans, specs, and per-task reports: docs/agentic-development/.

License

BSD-2-Clause — see LICENSE. flannrust is a derivative work of nanoflann by Jose Luis Blanco-Claraco et al., which builds on FLANN by Marius Muja and David G. Lowe; the upstream copyright notices are retained in LICENSE. The vendored nanoflann.hpp (used only as a test/benchmark oracle, not part of the Rust library) keeps its original license header verbatim.

Metadata

Release files for flannrust 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flannrust 0.1.0
File Size Uploaded
flannrust-0.1.0.tar.gz 165.5 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for flannrust 0.1.0
File Interpreter ABI Platform
flannrust-0.1.0-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details
flannrust-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details
flannrust-0.1.0-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details

Total release size: 1.8 MB

Release files / flannrust-0.1.0.tar.gz

Download URL flannrust-0.1.0.tar.gz
Size 165.5 kB
Tags Source
SHA-256 checksum
How to use checksums
aacfdd243d4559c3a056d34950d6773029ee5f80fb6cd9d4ae8fb9eb9f0e94d9
BLAKE2b-256 checksum
How to use checksums
80160397eeef7f8655bde2ffc41a2e129972030c85b7e3461a17a8a70e1a5638
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 28, 2026.

Transparency log

Release files / flannrust-0.1.0-cp39-abi3-win_amd64.whl

Download URL flannrust-0.1.0-cp39-abi3-win_amd64.whl
Size 509.3 kB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
d0858c9ca2741382683e9751cf49bc62b531a8822f3234ccc08117f0fe5ccfef
BLAKE2b-256 checksum
How to use checksums
d63e5386e5e1e90154fd724de79d4f7b52f19b0cca62b2251108c8507cefff99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 28, 2026.

Transparency log

Release files / flannrust-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL flannrust-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 625.0 kB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
1407ffdc0f65978b4c2141f45e39d9f47b7af52624e880f55145a00f1f6b3ac0
BLAKE2b-256 checksum
How to use checksums
0757573908d5f8528cad80090c1eb14ea7071b90363ace7a9e779476e9cec9bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 28, 2026.

Transparency log

Release files / flannrust-0.1.0-cp39-abi3-macosx_11_0_arm64.whl

Download URL flannrust-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
Size 549.4 kB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
809a4b37a55fac456bd82d2fd1a728d3567c5cf552f8a2b51a400e12626f71ca
BLAKE2b-256 checksum
How to use checksums
4d35945bed6b9723bd4eae60efde86b3d2fdbc5e1712b3638a21adef20d949d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 28, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

4 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page