Repository navigation
Description
Summary
The key→slot lookup table inside index_dense_gt uses what amounts to an identity hash
for integer keys (std::hash<u64> is the identity function in libstdc++/libc++) over a
power-of-2-sized open-addressing table with linear probing. With keys that are ascending
(or sparse-but-ordered) integers — an extremely common pattern when keys are database row
ids or auto-increment entity ids — the table degenerates into very long contiguous
probe clusters. We hit this in production twice, in two different code paths:
add()(build time): key-table maintenance across incremental growth/rehash makes
insertion throughput collapse as the index grows.view()/ load (read time): the table is unconditionally rebuilt via
reindex_keys_()when key lookups are enabled, single-threaded — so even a
pre-built index pays the full cost again on every cold start.
enable_key_lookups = false avoids both, but it is only reachable from some bindings
(Python exposes it; the Rust cxx bridge does not — it never sets the field, so the C++
default true always applies).
Environment
- usearch 2.25.3 (Python wheel for the build; Rust crate
usearchvia the cxx bridge
for the serving process) - Linux x86_64, 64 GB+ RAM, index on local SSD
- Index: ~112.3M vectors, 384-dim,
i8, cosine, default connectivity/expansion
(c=16, ef_add=32, ef_search=64) - Keys: 64-bit integers, ascending, sparse, max key ≈ 1e9 (so ~10% density over
the key range, perfectly ordered insertion)
Measured impact
| scenario | config | throughput / wall |
|---|---|---|
add() build, ascending keys |
default (enable_key_lookups=true) |
degrades to ~169 rows/s as the index grows (build effectively never finishes — projected weeks) |
add() build, same data |
enable_key_lookups=false |
~33,000 rows/s sustained start-of-build, ~26,700 rows/s average over the full 112.3M; complete in ~70 min (16 threads) |
| controlled A/B, same slice of keys/vectors | lookups on vs off | ≈ 90× insertion-throughput difference (~144 rows/s vs ~12,900 rows/s on the post-rehash key range) |
view() of the finished 112.3M index (Rust bridge) |
default (flag not settable) | >1 hour pegging one core inside index_dense_gt::reindex_keys_() before we killed it |
view() of the same index |
enable_key_lookups=false (patched bridge, see below) |
~4 seconds to a query-ready view |
Where it happens (2.25.3 sources)
include/usearch/index_dense.hpp:138—index_dense_config_t::enable_key_lookups
defaults totrue.index_dense.hpp(~line 1054 in 2.25.3) —view()path: when key lookups are enabled,
the key table is unconditionally rebuilt viareindex_keys_(). Single-threaded O(N)
rebuild, with each insert paying the clustered-probe cost — for sequential keys this is
effectively quadratic-ish in practice.rust/lib.rs/rust/lib.cpp— the cxx bridge'sIndexOptionshas no
enable_key_lookupsmember andnew_native_indexnever sets
config.enable_key_lookups, so Rust users always get the defaulttrueand cannot opt
out, even for pureview()+search()consumers that never call
get/remove/contains/rename.
Minimal repro recipe (Python, build side)
import numpy as np
from usearch.index import Index
N = 20_000_000 # large enough to show the cliff clearly
keys = np.arange(N, dtype=np.int64) * 9 + 7 # ascending, sparse, max ~1.8e8
vecs = (np.random.rand(N, 384) * 254 - 127).astype(np.int8)
# A: default — watch rows/s collapse as the table grows/rehashes
ix = Index(ndim=384, metric="cos", dtype="i8")
ix.add(keys, vecs, log=True)
# B: identical, but
ix = Index(ndim=384, metric="cos", dtype="i8", enable_key_lookups=False)
ix.add(keys, vecs, log=True) # orders of magnitude faster, flatRead side: save the A index, then Index.restore(..., view=True) (or the Rust
view()) and time "open → first search". With lookups enabled the entire key table is
rebuilt before the view is usable.
Random (shuffled) keys do not exhibit the cliff — which is exactly the signature of
identity-hash + linear-probe clustering, not of HNSW itself.
Suggested fixes (any one of these would resolve it; they compose)
- Hash the key through a cheap bit-mixer finalizer before masking into the
power-of-2 table (e.g.splitmix64finalizer or Fibonacci multiply-shift). This fixes
sequential/sparse integer keys generically, at ~1-2 ns per lookup, with no API change. - Expose
enable_key_lookupsthrough all bindings — specifically the Rust cxx
bridge (IndexOptions), which currently hard-inherits the C++ default. We have a
working 3-line passthrough patch (vendored locally) and are happy to PR it:
IndexOptions.enable_key_lookups: booldefaulting totrue(behavior-preserving for
existing callers), wired throughnew_native_index. - Make
view()/load skip or deferreindex_keys_()— e.g. an explicit
view(reindex_keys=false)(or lazily build the table on firstget/remove). A
serving process that only ever callssearch()should not pay an O(N) single-thread
rebuild on every cold start of a read-only mmap view. - (Minor) When the final count is known at load/build time, reserve the key table to
final capacity once to avoid intermediate growth/rehash convoys.
Workaround for others hitting this
- Python builds: pass
enable_key_lookups=Falseif you never use
get/remove/contains/renameon the index. - Rust: not currently reachable; we vendored 2.25.3 with the passthrough described in
(2). Note the documented trade-off: with lookups disabled,get/remove/contains/
renameassert (index_dense.hpp:794/808/951/1596) — fine for add/search/view-only
workloads.
Thanks for usearch — outside this pathology it has been excellent at this scale
(112M × 384-dim i8 in a single index, ~60 GB file, mmap-served).
Activity
ashvardanian commentedon Jun 10, 2026
Thanks for the report! I'm open to a SplitMix override for the hasher and exposing the flag to other bindings in a compatible fashion. Feel free to open a PR 🤗
Fix: Mix integer key hashes with SplitMix64 (#773)
Merge: Node Lookups, Stats, & CI Upgrades
Fix: Mix integer key hashes with SplitMix64 (unum-cloud#773)
vector-store: usearch 2.26.4 with numkong's f32 angular kernels accum…