Skip to the content
hinny
Back to news

Engineering

A million vectors in 0.13 ms

Hinny finds the 10 nearest of a million 256-dimensional vectors in 0.13 ms at the median, single-threaded, at 0.997 recall@10. Here is where that time goes and what it took to get there.

A vector search answers one question: of everything stored, which ten vectors are closest to this one? Comparing the query with every vector answers it exactly, and for a million vectors of 256 dimensions that takes Hinny 13 ms. Its index answers in 0.13 ms and agrees with the exact answer on 99.7% of the neighbours. It gets there by not looking at most of the data, and by looking at the rest in a very small form.

  1. 1,000,000vectors in the collection
  2. 1,000cluster centres ranked
  3. 22,0001-bit codes scored2.2% of the collection
  4. 400stored vectors read to rerank0.04%
  5. 10results
What one query over a million vectors reads. Synthetic, clustered vectors of 256 dimensions, at the index's calibrated probe budget, recall@10 0.997; counted on an M1 Max under Linux, at the start of the work on the file layout described below.

One bit a dimension

Hinny keeps every vector twice. Once as a code of one bit a dimension: after a random rotation, the signs of the vector's offset from the centre of its cluster, with two small correction factors. This is RaBitQ. The codes of a million 256-dimension vectors take about 40 MB where the vectors themselves take a gigabyte, so the part of the index a query scans stays in the fast tiers of memory.

A code gives an estimate of the distance and, more usefully, a bound on how wrong that estimate can be. The search scores codes, keeps the best results so far, and reads a stored vector only when its bound says it could still belong among them. On the million that is about 400 vectors a query, 0.04% of the collection.

The second copy, the one read for that last step, is in half precision. It is read only to rerank, half precision keeps distances within 0.1%, and it halves the file: the index of BEIR FiQA went from 64 to 34 MB at the same recall.

Clusters, and how many to look into

Vectors are grouped by k-means into about √n clusters, and a cluster's codes are stored together. A query ranks the cluster centres and scans the codes of the nearest few. How many is the setting that decides both speed and recall, and no fixed number is right for all data: probing 16 clusters found 99% of the true neighbours on a mailbox's embeddings, 99.9% on clustered synthetic data and 91% on FiQA's.

So each index calibrates itself when it is built. It takes 256 of its own vectors, finds each one's neighbours both exactly and approximately, and stores the smallest budget, from 8 clusters up, that finds 99.7% of their ten nearest. It also stops where more probes stop helping: on the synthetic million, 8 probes already reach 0.996 and it is the codes' estimates, not the probing, that miss the rest, so it keeps 8. Calibration costs about 30 ms for 57,000 vectors.

CollectionRecall@10Median99th percentileExact scanVectors read to rerankIndex
BEIR FiQA57,638 documents, its 648 real questions0.9920.10 ms0.22 ms0.78 ms0.39%34 MB
A mailbox23,037 emails, held-out emails as queries0.9980.04 ms0.07 ms0.31 ms0.34%14 MB
1 million vectorssynthetic, clustered0.9970.13 ms0.25 ms13 ms0.04%596 MB512 MB of it the stored vectors
Recall@10 against exact search, at each index's calibrated default. Single-threaded queries, warm, on an Apple M5 Pro. A fixed budget of 16 clusters, the default before calibration, gave FiQA 0.913.

Thirty-two codes at a time

Scoring a code means adding up the query's values over the code's set bits. Hinny quantizes the query's offset from a cluster's centre to 4 bits a dimension and stores the codes regrouped in blocks of 32, four dimensions to a group. For each probed cluster the query gets a 16-entry table per group, and one SIMD table lookup scores 16 codes on four dimensions at once. This is FastScan, as the RaBitQ authors use it.

The sums are the same as scoring code by code, which a test checks, so recall does not move. On an Apple M5 Pro, FiQA's median went from 0.150 to 0.094 ms, and the synthetic million's from 0.21 to 0.13 ms.

A file laid out the way a search reads it

With scoring that cheap, the profile of one query over the million changed. Of 0.215 ms, about 65 µs went into scoring 22,000 codes and 30 µs into ranking 1,000 cluster centres. The rest, around half of the query, was reading 400 stored vectors to rerank: each one a cache miss somewhere in a 512 MB file.

  • Scoring 22,000 codesabout 65 µs
  • Ranking 1,000 cluster centresabout 30 µs
  • Reading 400 stored vectors to rerankthe rest
One query over the synthetic million before the changes below, 0.215 ms in all. M1 Max under Linux.

Three changes followed.

  • Stored vectors are fetched a few candidates ahead of being read, so the memory is on its way before it is needed.
  • The lower bounds of a block's 32 codes are worked out four at a time, and only the codes that pass are estimated one by one. Once there are ten results, nearly none do.
  • The file now holds what a search reads: the codes already in their blocks of 32, the per-vector factors worked out, and the stored vectors in the order of the codes instead of the order of the documents. A search maps the file and builds nothing, reranking needs no lookup from document to row, and a cluster's vectors sit next to each other. The file is 1.4% larger.

A query over a million vectors, change by change

Median, synthetic million at its calibrated budget, recall@10 0.997, M1 Max under Linux

  • The release before1.09 ms
  • At the start of this work0.215 ms
  • Vectors fetched ahead0.163 ms
  • The file holds what a search reads0.132 ms
ChangeBeforeAfter
Stored vectors fetched ahead of being read0.195 ms0.163 ms
The file holds what a search reads0.163 ms0.132 ms
Altogether, from the start of this work0.215 ms0.132 ms
From the release before, which counted deletions in every search1.09 ms0.132 ms
Median query over the synthetic million at its calibrated budget, recall@10 0.997 throughout. M1 Max under Linux. With the last change the 99th percentile went from 0.35 to 0.26 ms.

One set of clusters for the whole index

Hinny stores data in immutable segments that are merged in the background, and at first each segment had clusters of its own. A search then pays for every segment: S segments scan about √S times the codes of one merged segment. The million as writing leaves it, in 8 segments, scanned 5.7% of the codes against 2.2% merged into one, and took 0.26 against 0.13 ms.

Segments now share their cluster centres. Writing a new segment assigns its vectors to the centres the namespace already has, with no clustering and no calibration. A search ranks the centres once and scans every segment's list for a probed cluster into one top ten. A merge copies the lists as they are, so it changes no answer. The centres are trained for twice the vectors there were, and trained again once there are four times as many.

The million as writing leaves it is now 5 segments: 2.6% of the codes scanned, 0.15 ms at recall@10 0.996, and 49,000 searches a second on 8 threads, where there had been 10,500. These last figures were measured while other work ran on the machine.

What did not help

  • Probing every cluster within a factor of the nearest centre's distance, a rule that adapts to the query, traded recall for work no better than a fixed budget on FiQA, with a longer tail.
  • Stopping once the top ten had not changed for 48 clusters gave FiQA 0.990 at 0.10 ms against 0.993 at 0.15 ms: a little better than a smaller fixed budget, but below the target the budget is calibrated for.
  • Putting each vector in a second cluster, simulated on FiQA with exact distances: 0.2 to 1.3 points of recall more at equal work, for twice the codes.

What is next

Ranking the cluster centres is now a visible share of a query, and a larger collection would want more of them. The plan is to rank centres through 1-bit codes of their own, and then to use more clusters. From the measured costs, 22 ns a centre and 3 ns a code, the index's part of a search should fall from about 95 µs to between 35 and 50 µs. That is an estimate, not a measurement.

Also open: clusters are retrained when a namespace grows but not yet when its data drifts, and the x86-64 versions of these kernels pass the test suite under emulation but have not been timed on an x86 processor. The current numbers for full text, vectors and hybrid search are on the benchmarks page.