superbit
A lightweight, in-memory vector index for approximate nearest-neighbor (ANN) search using Locality-Sensitive Hashing.
Overview
superbit provides fast approximate nearest-neighbor search over
high-dimensional vectors without the operational overhead of a full vector
database. It implements random hyperplane LSH (SimHash), a
locality-sensitive hashing scheme that hashes similar vectors into the same
buckets with high probability. Candidate vectors retrieved from the hash
tables are then re-ranked with an exact distance computation, giving a good
balance between speed and recall.
Target use cases:
- Retrieval-augmented generation (RAG) prototyping
- Recommendation system experiments
- Embedding similarity search during development
- Anywhere you need sub-linear ANN queries and want to avoid external infrastructure
Features
- Random hyperplane LSH (SimHash) for cosine, Euclidean, and dot-product similarity
- Multi-probe querying -- probe neighboring hash buckets to improve recall without adding more tables
- Thread-safe concurrent access via
parking_lot::RwLock(parallel reads, exclusive writes) - Builder pattern for ergonomic index configuration
- Auto-tuning --
suggest_paramsrecommendsnum_hashes,num_tables, andnum_probesgiven a target recall and dataset size - Runtime metrics -- lock-free atomic counters track query latency, candidate counts, and bucket hit rates
- Optional features:
parallel-- parallel bulk insert and batch query via rayonpersistence-- save/load indexes to disk with serde + bincode (or JSON)python-- Python bindings via PyO3
Architecture
Module Structure
graph TD
A[<b>LshIndex</b><br/>Public API] --> B[RwLock<IndexInner><br/>Thread-safe wrapper]
A --> M[MetricsCollector<br/>Atomic counters]
B --> V[vectors<br/>HashMap<id, Array1<f32>>]
B --> T[tables<br/>Vec<HashMap<u64, Vec<id>>>]
B --> H[hashers<br/>Vec<RandomProjectionHasher>]
B --> C[IndexConfig]
H --> |"sign(dot(v, proj))"| T
subgraph Optional Features
P[parallel<br/>rayon batch ops]
S[persistence<br/>serde + bincode/JSON]
PY[python<br/>PyO3 bindings]
end
A -.-> P
A -.-> S
A -.-> PY
Query Flow
flowchart LR
Q[Query Vector] --> N{Normalize?}
N -->|Cosine| NORM[L2 Normalize]
N -->|Other| HASH
NORM --> HASH
HASH[Hash with L Hashers] --> PROBE[Multi-probe:<br/>flip uncertain bits]
PROBE --> T1[Table 1<br/>base + probes]
PROBE --> T2[Table 2<br/>base + probes]
PROBE --> TL[Table L<br/>base + probes]
T1 --> UNION[Candidate Union<br/>deduplicate IDs]
T2 --> UNION
TL --> UNION
UNION --> RANK[Exact Re-rank<br/>compute true distance]
RANK --> TOPK[Return Top-K]
Insert Flow
flowchart LR
I[Insert: id, vector] --> DUP{ID exists?}
DUP -->|Yes| REM[Remove old<br/>hash entries]
DUP -->|No| NORM
REM --> NORM{Normalize?}
NORM -->|Cosine| DO_NORM[L2 Normalize]
NORM -->|Other| STORE
DO_NORM --> STORE
STORE[Compute L hashes] --> BUCK[Push id into<br/>L hash buckets]
BUCK --> VEC[Store vector in<br/>central HashMap]
Quick Start
Add the crate to your Cargo.toml:
[dependencies]
superbit_lsh = "0.1"
Build an index, insert vectors, and query:
use superbit::{LshIndex, DistanceMetric};
fn main() -> superbit::Result<()> {
// Build a 128-dimensional index with cosine similarity.
let index = LshIndex::builder()
.dim(128)
.num_hashes(8)
.num_tables(16)
.num_probes(3)
.distance_metric(DistanceMetric::Cosine)
.seed(42)
.build()?;
// Insert vectors (ID, slice).
let v = vec![0.1_f32; 128];
index.insert(0, &v)?;
let v2 = vec![0.2_f32; 128];
index.insert(1, &v2)?;
// Query for the 5 nearest neighbors.
let results = index.query(&v, 5)?;
for r in &results {
println!("id={} distance={:.4}", r.id, r.distance);
}
Ok(())
}
Feature Flags
| Flag | Effect |
|---|---|
parallel |
Parallel bulk insert and batch query via rayon |
persistence |
Save/load index to disk (serde + bincode + JSON) |
python |
Python bindings via PyO3 |
full |
Enables parallel + persistence |
Enable features in your Cargo.toml:
[dependencies]
superbit_lsh = { version = "0.1", features = ["full"] }
Configuration Guide
The three main knobs that control the speed/recall/memory trade-off are:
| Parameter | What it controls | Higher value means |
|---|---|---|
num_hashes |
Hash bits per table (1--64) | Fewer, more precise buckets; lower recall per table but less wasted work |
num_tables |
Number of independent hash tables | Better recall (more chances to find a neighbor); more memory |
num_probes |
Extra neighboring buckets probed per table | Better recall without adding tables; slightly more query time |
Rules of thumb:
- Start with the defaults (
num_hashes=8,num_tables=16,num_probes=3) and measure recall on a held-out set. - If recall is too low, increase
num_tablesornum_probesfirst. - If queries are too slow (too many candidates), increase
num_hashesto make buckets more selective. - For cosine similarity the index L2-normalizes vectors on insertion by default
(
normalize_vectors=true).
Auto-Tuning
Use suggest_params to get a starting configuration based on your dataset size
and target recall:
use superbit::{suggest_params, DistanceMetric};
let params = suggest_params(
0.90, // target recall
100_000, // expected dataset size
768, // vector dimensionality
DistanceMetric::Cosine, // distance metric
);
println!("Suggested: hashes={}, tables={}, probes={}, est. recall={:.2}",
params.num_hashes, params.num_tables, params.num_probes, params.estimated_recall);
You can also estimate the recall of a specific configuration without building an index:
use superbit::{estimate_recall, DistanceMetric};
let recall = estimate_recall(16, 8, 2, DistanceMetric::Cosine);
println!("Estimated recall: {:.2}", recall);
Performance
LSH-based indexing provides sub-linear query time by reducing the search space to a small set of candidate vectors. In practice:
- For datasets under ~10,000 vectors, brute-force linear scan is often fast enough and gives exact results. LSH adds overhead that may not pay off at this scale.
- For datasets above ~10,000 vectors, LSH becomes increasingly beneficial. Query time grows much more slowly than dataset size.
- With well-tuned parameters you can typically achieve 80--95% recall while examining only a small fraction of the dataset.
The parallel feature flag enables rayon-based parallelism for bulk inserts
(par_insert_batch) and batch queries (par_query_batch), which can
significantly speed up workloads that operate on many vectors at once.
Use the built-in metrics collector (.enable_metrics() on the builder) to
monitor query latency, candidate counts, and bucket hit rates in production.
Comparison with Other Rust ANN Crates
| Crate | Algorithm | Notes |
|---|---|---|
| superbit | Random hyperplane LSH | Lightweight, pure Rust, no C/C++ deps. Good for prototyping and moderate-scale workloads. |
| usearch | HNSW | High performance, C++ core with Rust bindings. Better for large-scale production. |
| hora | HNSW / IVF-PQ | Pure Rust, multiple algorithms. More complex API. |
| hnsw_rs | HNSW | Pure Rust HNSW implementation. |
superbit is intentionally simple: a single algorithm, a small API
surface, and no native dependencies. It is a good fit for prototyping,
moderate-scale applications, and situations where you want to understand and
control the indexing behavior. For very large datasets (millions of vectors) or
when you need maximum throughput, a graph-based index like HNSW will generally
outperform LSH.
License
Licensed under either of
at your option.
Metadata
Release files for superbit-lsh 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| superbit_lsh-0.1.0.tar.gz | 45.2 kB | Details |
Built distributions (wheels)
Total release size: 28.9 MB
Release files / superbit_lsh-0.1.0.tar.gz
| Download URL | superbit_lsh-0.1.0.tar.gz |
|---|---|
| Size | 45.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c4b6aa1e47c1dca2f04f6a2826f8cb30b89cef6213b0ec0a85f7d5b4de16802a
|
|
BLAKE2b-256 checksum How to use checksums |
606ae9869fac47632277ed800db12518859af77d9a564338040a07b3d2234b31
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / superbit_lsh-0.1.0-cp313-cp313-win_amd64.whl
| Download URL | superbit_lsh-0.1.0-cp313-cp313-win_amd64.whl |
|---|---|
| Size | 205.1 kB |
| Tags | CPython 3.13 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
1e9babe1935edbd755051f2ba3353fe71e5f4dee4ec30cf3a23c0031aeae195f
|
|
BLAKE2b-256 checksum How to use checksums |
09b70562476a750a018dd20157f612b287d73044cfbd7ed9275ad7e29fe4db35
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / superbit_lsh-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | superbit_lsh-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 2.9 MB |
| Tags | CPython 3.13 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
584b473e64d12174073973adae4790781b1864a939a60b29cdfbeebede760506
|
|
BLAKE2b-256 checksum How to use checksums |
a71ce60128a17965b543333c0127018fe0fa952ca7a3822a19fcb6093727853d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / superbit_lsh-0.1.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | superbit_lsh-0.1.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 2.7 MB |
| Tags | CPython 3.13 Linux glibc 2.17+ ARM64 |
|
SHA-256 checksum How to use checksums |
481f43476258d755be405f2ef3940ed8e7b61d1fefcf43a7b605af3140e7a968
|
|
BLAKE2b-256 checksum How to use checksums |
099747f6215d3e2371d3fdb6e93aea65b38424a10b5258b35176f6eb9cffde2f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / superbit_lsh-0.1.0-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | superbit_lsh-0.1.0-cp313-cp313-macosx_11_0_arm64.whl |
|---|---|
| Size | 326.2 kB |
| Tags | CPython 3.13 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
b4275615eb9b8b866374f53ad24bcbc3ae7924e02679e715033ab967230b731d
|
|
BLAKE2b-256 checksum How to use checksums |
2679785981b9f7370f677edc3084427bcd7fdb98d40a7778164ec4a9ba3cad98
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / superbit_lsh-0.1.0-cp313-cp313-macosx_10_12_x86_64.whl
| Download URL | superbit_lsh-0.1.0-cp313-cp313-macosx_10_12_x86_64.whl |
|---|---|
| Size | 326.3 kB |
| Tags | CPython 3.13 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
79f72330a2820b211ba1179e9f3e292f85d01e7329b9576ff4ceba1a2efcab68
|
|
BLAKE2b-256 checksum How to use checksums |
7438c3e7280e5d8561d9365046fa7ddbe8b9e7145598a2cfc87b903d24b58f24
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / superbit_lsh-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | superbit_lsh-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 2.9 MB |
| Tags | CPython 3.12 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
4f5e99fb7cd9961dad4e65e4a5efdfb4d9cc61126282b2c1cac40047eaf147e1
|
|
BLAKE2b-256 checksum How to use checksums |
4e9b73ecc2686d551ed43ce5945982aca7052c10d25dd0a21a66f984a193dfaf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / superbit_lsh-0.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | superbit_lsh-0.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 2.7 MB |
| Tags | CPython 3.12 Linux glibc 2.17+ ARM64 |
|
SHA-256 checksum How to use checksums |
9c40620d00e64184081fd0c29522380cad319c50210eef6de5f39ed477cd76f6
|
|
BLAKE2b-256 checksum How to use checksums |
e0cf4590b43aeb8b03bd8dc4d01020f1abb3531492161d08e9e6f6da681bcc97
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / superbit_lsh-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | superbit_lsh-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 2.9 MB |
| Tags | CPython 3.11 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
47410055b353bec7b6f285cdf1c6647ee169845752f32c50d3d51bc54ed4f895
|
|
BLAKE2b-256 checksum How to use checksums |
05cb60c024a110b26213fc22f42549cc24cd0ed873c191b8464b950690ad0489
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / superbit_lsh-0.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | superbit_lsh-0.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 2.7 MB |
| Tags | CPython 3.11 Linux glibc 2.17+ ARM64 |
|
SHA-256 checksum How to use checksums |
db9fe6c5ad7156aca4019016bc99b1d456652bb9c874a0cc659cca76468059c0
|
|
BLAKE2b-256 checksum How to use checksums |
98392553e5e7e898cb82baef68cf3abe53c53ce62af83872b354875b425420e0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / superbit_lsh-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | superbit_lsh-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 2.9 MB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
01694312ca7d7f512359a4f53a69834c1c334a90a1681b5e1c16464a1c2f6ac3
|
|
BLAKE2b-256 checksum How to use checksums |
da8cc4293f0acf0fde0a442025a655f3107a4f7e58b8d79ca7f9d319df775458
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / superbit_lsh-0.1.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | superbit_lsh-0.1.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 2.7 MB |
| Tags | CPython 3.10 Linux glibc 2.17+ ARM64 |
|
SHA-256 checksum How to use checksums |
5acad0938af6ede3e34b18ea57519b356327462d5087b4ba7866d8d5468f8a44
|
|
BLAKE2b-256 checksum How to use checksums |
4c26bd58e919d5506f9a480b62cc942ad89449adee38a79e79644a5bb052d020
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / superbit_lsh-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | superbit_lsh-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 2.9 MB |
| Tags | CPython 3.9 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
5785b7a5e5e35d7fba7a03d728a57372a5ad8744017da5bb7022279c7d3ada3f
|
|
BLAKE2b-256 checksum How to use checksums |
d395d3a2b6a27a15fb8559e85a549adc8af6f3f32dc564fa7886e51264c45de1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / superbit_lsh-0.1.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | superbit_lsh-0.1.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 2.7 MB |
| Tags | CPython 3.9 Linux glibc 2.17+ ARM64 |
|
SHA-256 checksum How to use checksums |
922a45d3aab460d5883d65ec248fa238de09a7ce4077b7a0f3ffa7e439b07355
|
|
BLAKE2b-256 checksum How to use checksums |
2532e4c372e5486c78df6e2d0f134b834b080162061f8e4802a5391cbadc852a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|