QaNN: Making Vector Search Fast
November 7, 2026
QaNN is a nearest-neighbor search library I’m building in C++20, with a NumPy API and wheels on PyPI. This note follows one benchmark: finding the 10 nearest neighbors in SIFT1M, a standard dataset of one million 128-dimensional vectors.
Brute force gets every answer right, but on one thread of an M3 it handles only 58 queries per second. The goal is to get close to that accuracy far faster, using much less work per query.
Searching less
An inverted file (IVF) groups vectors into clusters with k-means, then scans only the clusters nearest the query. With 1,000 clusters, scanning 16 of them reaches 93.2% recall@10 at about 3,900 queries per second, or 67x brute force.
Those scans still read 512 bytes of raw floats per vector, so memory became the next limit.
Reading less
Product quantization (PQ) splits each vector into 16 slices and replaces each slice with one byte: the ID of its nearest centroid. Each vector drops from 512 bytes to 16, and a distance becomes 16 table lookups.
Compression alone isn’t enough. PQ with 16 slices levels off near 0.56 recall, because the approximate distances blur together.
The fix is re-ranking. Asking PQ for 160 candidates and rescoring them with the exact vectors brings recall back to 0.929. The lossy step only has to keep the true neighbors somewhere in its shortlist, not rank them perfectly.
That changes what the fast path has to do, and it’s the main idea behind the rest of the project.
Following the profiler
From there, I timed each section of a query and worked on whatever was largest. Some of the bigger wins:
- Precomputed tables: most of a PQ distance table can be computed once per query or once at training time, instead of rebuilding it for every cluster. Table builds dropped from 37.8 to 9.0 µs.
- A better top-k buffer: replacing a max-heap with a buffer that trims itself using
nth_elementgave 15% to 20% more queries per second. - Nearest clusters first: scanning the closest clusters first tightens the top-k threshold sooner. Candidates kept per query dropped from 1,192 to 899.
Training got the same treatment. Computing k-means assignments with one BLAS matrix multiply cut IVF training on SIFT1M from 37 seconds to 3.3 seconds, and it’s 2.3 seconds today.
Fast-scan
Another big jump came from fast-scan, the technique André et al. described in 2015. Instead of 8-bit codes, each slice gets 4 bits, so its lookup table has only 16 entries. Sixteen bytes fit in a SIMD register, so a single shuffle instruction performs 16 table lookups at once, and QaNN scores vectors in blocks of 32.
4-bit codes are less accurate, so fast-scan uses 64 slices instead of 32 to fill the same 32 bytes. Before re-ranking, recall falls from 0.706 to 0.638. After re-ranking, the two are effectively identical at 0.931 and 0.932, while a query took 49 µs instead of 91 µs.
Re-ranking made a later change cheap too. The kernel spent half its work widening values, so I capped table entries at 63. Four slices can then be added in a byte before widening, which made the kernel 1.22x faster per block. Recall before re-ranking moved from 0.638 to 0.635, and after re-ranking it didn’t move at all.
What the numbers look like now
| Index | Bytes per vector | Recall@10 | Queries per second | vs. brute force |
|---|---|---|---|---|
| Brute force | 512 | 0.999 | 58 | 1x |
| IVF | 516 | 0.932 | 3.9K | 67x |
| IVF + PQ | 20 | 0.555 | 11.8K | 204x |
| IVF + PQ + re-rank | 532 | 0.929 | 9.9K | 171x |
| IVF + fast-scan | 36 | 0.635 | 20.1K | 348x |
| IVF + fast-scan + re-rank | 548 | 0.931 | 16.3K | 282x |
| HNSW | 644 | 0.963 | 17.6K | 305x |
One thread on an Apple M3, 10,000 queries, 16 clusters scanned, on a machine that wasn’t idle, so treat speeds as approximate. HNSW is a graph index, shown with M=16 and ef=64.
Against other libraries
ann-benchmarks is the standard public benchmark for nearest-neighbor search. It compares libraries by how many queries per second they handle at a given recall. At recall@10 of at least 0.99, on one thread of an AMD EPYC (Zen 4) VM:
| Dataset | QaNN | hnswlib | FAISS fast-scan |
|---|---|---|---|
| sift-128 | 5,920 | 3,664 | 2,220 |
| fashion-mnist-784 | 10,110 | 6,511 | 5,275 |
| gist-960 | 673 | 272 | 493 |
| glove-25 | 6,175 | 5,629 | 942 |
| glove-100 | 1,030 | 418 | 662 |
| nytimes-256 | 1,255 | 248 | didn’t reach 0.99 |
Best queries per second at recall@10 ≥ 0.99, using QaNN’s IVF fast-scan with re-ranking, except glove-25, where its HNSW with 8-bit SQ was fastest. Recall is ann-benchmarks’ definition, which counts a neighbor tied with the 10th true distance as found.
QaNN was the fastest of the three on all six datasets, at 1.1x to 5.1x hnswlib and 1.4x to 6.6x FAISS’s fast-scan index.
What didn’t work
Several ideas sounded good and failed once measured:
- Link-time optimization: no query speedup, and k-means training got about 35% slower.
MADV_WILLNEEDbefore re-ranking: memory-mapped loading itself worked well. On an x86 VM, a 1M × 384 index opens in 0.1 s using 93 MB, against 0.89 s and 1.6 GB loaded fully. But asking the kernel to prefetch the candidates’ pages made cold queries only 1.3x faster, and warm queries 24% to 74% slower.- Non-parametric OPQ: this iterative rotation of the vectors gained nothing over a simpler PCA rotation. The PCA rotation does raise fast-scan recall from 0.635 to 0.718, but with re-ranking it adds only about 5% more queries per second, and it does nothing on GloVe.
- ACORN-style two-hop search for filtered HNSW: at equal recall it was slower than a plain search with a smaller beam. Rejected nodes weren’t the real cost, the oversized beam was.
- 4-bit SQ on HNSW: smaller vectors weren’t faster than 8-bit ones, because graph search waits on the latency of each random read, not on bytes moved.
How I keep it honest
Performance work is easy to fool yourself with, so two checks run throughout. CI fails if recall on a small SIFT subset drops by more than 0.02 from a committed baseline. For speed, a local script builds two commits and alternates between them on the same machine, because shared CI runners vary by 10% to 20% from run to run.
The main lesson is that speed work and accuracy work can’t be separated. Most of QaNN’s speed comes from approximations, and each one was only safe because re-ranking and a recall check were there to catch the cost.
References
[1] Jégou, H., et al. “Product Quantization for Nearest Neighbor Search.” IEEE TPAMI 33(1). 2011.
[2] Jégou, H., et al. “Searching in one billion vectors: re-rank with source coding.” ICASSP 2011.
[3] André, F., et al. “Cache locality is not enough: High-Performance Nearest Neighbor Search with Product Quantization Fast Scan.” PVLDB 9(4). 2015.
[4] Douze, M., et al. “The Faiss library.” arXiv:2401.08281. 2024.
[5] Ge, T., et al. “Optimized Product Quantization.” IEEE TPAMI 36(4). 2014.
[6] Malkov, Y. A. & Yashunin, D. A. “Efficient and Robust Approximate Nearest Neighbor Search Using Hierarchical Navigable Small World Graphs.” IEEE TPAMI 42(4). 2020.
[7] Aumüller, M., et al. “ANN-Benchmarks: A benchmarking tool for approximate nearest neighbor algorithms.” Information Systems 87. 2020.
[8] TEXMEX. “Evaluation of Approximate nearest neighbors: large datasets.” The SIFT1M dataset.