Rust-DiskANN: Dynamic on-disk graph-based approximate nearest neighbor search with In-Place Insert and Delete 🦀
A Rust implementation of DiskANN using the Vamana graph algorithm, with a fixed-layout mutable mmap index for incremental insertion and in-place deletion. Deletion follows MERIT: bounded recovery of approximate in-neighbors, local k_r-MST repair, and versioned-edge invalidation. Conflict-aware repair waves parallelize independent deletion work while preserving the order of overlapping graph updates.
The static version uses the DiskANN API while dynamic workloads use the separate MmapDynamicDiskANN type.
Key Algorithms
Static Vamana Construction
This implementation follows the DiskANN paper's approach:
- Using the Vamana graph algorithm for index construction, pruning and refinement (in parallel)
- Memory-mapping the index file for efficient disk-based access (via memmap2)
- Implementing beam search with medoid entry points (in parallel)
- Supporting Euclidean, Cosine, Hamming and other distance metrics via a generic distance trait
- Maintaining minimal memory footprint during search operations
Dynamic MERIT Deletion
Dynamic deletion follows the MERIT algorithm:
- Logically invalidate the deleted node so it disappears from search immediately.
- Combine its outgoing neighbors with approximate in-neighbors recovered by a bounded graph search.
- Reconnect the local candidate set with incremental
k_r-MST repair and RobustPrune. - Increment the deleted slot's version so all stale incoming edges become invalid without a graph-wide scan.
- Reuse the deleted fixed-capacity slot for a later insertion.
For batch deletion, repair plans declare their candidate-node write sets. Plans with no overlapping writes run in parallel within the same dependency wave. Conflicting plans run in ordered waves, preventing lost adjacency updates without unsafe concurrent mmap writes.
Dynamic Mmap Layout
The dynamic index keeps fixed-offset regions in one file:
[ metadata and padding to 1 MiB ]
[ vectors: capacity * dim * sizeof(T) ]
[ adjacency targets: capacity * R * u32 ]
[ edge versions: capacity * R * u16 ]
[ node versions: capacity * u16 ]
[ validity bytes: capacity * u8 ]
The vector and u32 adjacency regions retain the static row-major organization. Updates modify fixed slots in place, so the dynamic index file does not grow during insertion and deletion.
Features
-
Single-file storage: All index data stored in one memory-mapped file in the following way:
[ metadata_len:u64 ][ metadata (bincode) ][ padding up to vectors_offset ] [ vectors (num * dim * f32) ][ adjacency (num * max_degree * u32) ] `vectors_offset` is a fixed 1 MiB gap by default. -
Vamana graph construction: Builds an approximate nearest-neighbor graph with progressive RobustPrune. Each prune starts at α = 1.0 to preserve diverse routing edges, then relaxes toward the configured α (default 1.2). The candidate pool is capped at 750, matching the official DiskANN algorithm.
-
Parallel batched graph refinement: Uses rayon to parallelize candidate generation and batched symmetrization/re-pruning during construction for high build throughput.
-
Build-optimized data layout: Uses flat contiguous storage instead of Vec<Vec> during construction to improve cache locality and reduce allocation overhead.
-
Memory-mapped on-disk index: Stores vectors and fixed-degree adjacency lists in a single file and memory-maps it for low-overhead loading and search.
-
MERIT in-place deletion: Uses bounded in-neighbor recovery, local
k_r-MST repair, and versioned-edge invalidation to maintain graph connectivity without rebuilding the index. -
Dynamic mmap index: Converts a static index into a fixed-capacity mutable mmap supporting
insert,insert_batch,delete, anddelete_batch. -
Conflict-aware parallel repair: Executes disjoint deletion repair plans concurrently while preserving deterministic ordering for overlapping candidate-node writes.
-
Stale-edge safety: Stores edge and node versions in parallel fixed-size regions, preventing old incoming edges from becoming valid when a deleted slot is reused.
-
Beam-search query algorithm: Uses a medoid entry point and beam search over the graph, typically visiting only a small fraction of indexed vectors
-
Generic over vector element type and distance: Works with generic T and any anndists::Distance, supporting use cases beyond standard floating-point ANN
-
Distance metrics: Support for Euclidean, Cosine and Hamming similarity et.al. via anndists. A generic distance trait that can be extended to other distances
-
Medoid-based entry points: Uses an approximate medoid as the default search entry point
-
Parallel query processing: Supports concurrent queries with rayon; depending on access patterns, this can increase page activity in the memory-mapped index
-
Minimal memory footprint: Keeps RAM usage well below full index size by relying on mmap rather than fully loading the index into memory.
-
Extensitve benchmarks: Speed, accuracy and memory consumption benchmark with HNSW (both in-memory and on-disk)
Visualization of Vamana graph build and search
The Vamana graph build plot is in 2D with L2 distance. See diskann-vamana-viz crate for details.
For search, the final graph was used. The path from entry node (red) to nearest node (green) for the query (pink) in the graph was labeled in orange.
Usage in Rust 🦀
Building a New Index
use ; // or your own Distance types
use ;
// Your vectors to index (all rows must share the same dimension)
let vectors: = vec!;
// Easiest: build with defaults (M=64, L_build=128, alpha=1.2)
let index = build_index_default?;
// Or: custom construction parameters
let params = DiskAnnParams ;
let index2 = build_index_with_params?;
Opening an Existing Index
use DistL2;
use DiskANN;
// If you built with DistL2 and defaults:
let index = open_index_default_metric?;
// Or, explicitly provide the distance you built with:
let index2 = open_index_with?;
Searching the Index
use DistL2;
use DiskANN;
let index = open_index_default_metric?;
let query: = vec!; // length must match the indexed dim
let k = 10;
let beam = 256; // search beam width
// (IDs, distance)
let hits: = index.search_with_dists;
// `neighbors` are the IDs of the k nearest vectors
let neighbors: = index.search;
Parallel Search
use DistL2;
use DiskANN;
use *;
let index = open_index_default_metric?;
// Suppose you have a batch of queries
let query_batch: = /* ... */;
let results: = query_batch
.par_iter
.map
.collect;
Dynamic Insert And Delete
The existing DiskANN build and search APIs and static file format are unchanged. Dynamic updates use the separate MmapDynamicDiskANN type and a fixed capacity chosen when converting the static index.
use DistL2;
use ;
let static_index = build_index_default?;
let mut index = create_from_static?;
// Independent graph searches are parallelized; adjacency commits preserve
// deterministic conflict ordering.
let inserted_ids = index.insert_batch?;
// MERIT defaults: repair beam 2R and k_r = 2.
let stats = index.delete_batch;
index.flush?;
let reopened = open?;
Deleted slots are reused for later inserts; insertion returns an error when the fixed capacity is exhausted. Versioned edges make stale incoming edges immediately invisible when a slot is deleted or reused. The file remains fixed-size during updates.
Space and time complexity analysis
- Index Build Time: O(n * max_degree * beam_width)
- Disk Space: n * (dimension * 4 + max_degree * 4) bytes
- Search Time: O(beam_width * log n) - typically visits < 1% of dataset
- Memory Usage: O(beam_width) during search
- Query Throughput: Scales linearly with CPU cores
Parameters Tuning
Index building Parameters
max_degree: 32-64 for most datasetsbuild_beam_width: 128-256 for good graph qualityalpha: 1.2-2.0 (higher = more diverse neighbors)
Index search Parameters
beam_width: 128 or larger (trade-off between speed and recall)- Higher beam_width = better recall but slower search
Index memory-mapping
When host RAM is not large enough for mapping the entire database file, it is possible to build the database in several smaller pieces (random split). Then users can search the query againt each piece and collect results from each piece before merging (rank by distance). This is equivalent to a single big database approach (as long as K'>=K) but requires a much smaller number of RAM for memory-mapping. In practice, the Microsoft Azure Cosmos DB found that this database shard idea can improve recall. Intutively, with smaller data points for each piece, we can use large M and build beam width to further improve accuracy. See their paper here
Building and Testing
# Build the library
# Run tests
# Run demo
# Run performance test
# test MNIST fashion dataset
# dynamic delete-only, insert-only, and mixed update tests with a fresh
# static rebuild baseline after every round
# test SIFT dataset
Examples
See the examples/ directory for:
demo.rs: Demo with 100k vectorsperf_test.rs: Performance benchmarking with 1M vectorsdiskann_mnist.rs: Performance benchmarking with MNIST fashion dataset (60K)diskann_sift.rs: Performance benchmarking with SIFT 1M datasetbigann.rs: Performance benchmarking with SIFT 10M datasethnsw_sift.rs: Comparison with in-memory HNSW
Benchmark against in-memory HNSW (hnsw_rs crate) for SIFT 1 million dataset
Results
SIFT1M Server Run
The following results were measured on the same 1,000,000-vector SIFT dataset with 10,000 queries and strict Recall@10. The search parameters are the tested operating points, not matched-latency settings: DiskANN used search beam 512, while HNSW used ef_search=64.
| Index | Build parameters | Search parameter | Build CPU time | Build wall time | Recall@10 | Queries/s |
|---|---|---|---|---|---|---|
| DiskANN | R=64, L=128, alpha=1.2, extra_seeds=1 |
beam 512 | 21,080.34 s | 204.10 s | 0.99999 | 14,211.89 |
HNSW (hnsw_rs) |
16 layers, ef_construction=256 |
ef_search=64 |
8,654.87 s | not reported | 0.98765 | 48,650.67 |
DiskANN returned all requested neighbors, with a mean last-distance ratio of 1.0000006. HNSW also returned all requested neighbors, with a mean last-distance ratio of 1.0003949. CPU time can substantially exceed wall time during parallel construction because it is accumulated across worker threads.
SIFT1M M4 Max Run
# DiskANN, SIFT1M, M4 Max
### sift1m, hnsw_rs, M4 Max
)
)
)
License
MIT This project is licensed under the MIT License - see the LICENSE file for details.
References
Jayaram Subramanya, S., Devvrit, F., Simhadri, H.V., Krishnawamy, R. and Kadekodi, R., 2019. Diskann: Fast accurate billion-point nearest neighbor search on a single node. Advances in neural information processing Systems, 32.
Zekai Wu, Jiabao Jin, Peng Cheng, Wangze Ni, Haoyang Li, Lei Chen, Junjie Yao, Jingkuan Song, and Heng Tao Shen. 2026. MERIT: Efficient In-Place Deletion for Dynamic Graph-Based Approximate Nearest Neighbor Indexes. arXiv:2607.29173.
Acknowledgments
This implementation is based on the DiskANN paper and the official Microsoft implementation. It was also largely inspired by the implementation here.