Skip to content

Fix: Keep key lookups usable when the rehash cannot allocate - #822

Merged
ashvardanian merged 1 commit into
unum-cloud:main-devfrom
cpegeric:fix_rehash_oom_20261023
Oct 24, 2026
Merged

ashvardanian merged 1 commit into
unum-cloud:main-devfrom
cpegeric:fix_rehash_oom_20261023

Conversation

@cpegeric

Copy link
Copy Markdown
Contributor

Problem

flat_hash_multi_set_gt::try_reserve() is noexcept, but it allocates with std::allocator<char>, which throws std::bad_alloc instead of returning nullptr. An allocation failure therefore calls std::terminate().

Since try_emplace() started counting tombstones toward the load factor, it also rehashes under ordinary remove/add churn on a reserved index, not only during reserve(). So an allocation failure inside index_dense_gt::add() aborts the process instead of returning an error. add_() also ignored the result of try_emplace(), so a failed key mapping would have been reported as success.

Repro: reserve 128 entries, add keys 0..127, then repeat "remove key i, add key 128+i" while operator new for the replacement table fails. The process aborts with SIGABRT once the tombstones reach the rehash threshold (round 51 in the reviewer's run).

Fix

  • try_reserve() catches the allocation failure and returns false, leaving the table unchanged. Without exceptions, behaviour is the same as before.
  • try_emplace(), when the rehash fails, still places the element in a free or deleted slot. It fails only when every slot holds a live entry. Tombstones are reclaimed by the next rehash that can allocate. Until then, probes for missing keys may walk further, but they stay bounded and correct.
  • index_dense_gt::add_() returns failed("Failed to map the key to its slot") if the key mapping fails, instead of ignoring it.

Tests

The new test_slot_lookup_rehash_failure runs 1,024 erase/emplace rounds on a 128-entry set with an allocator that throws, then checks:

  • the reserve reports failure instead of aborting;
  • every insert still succeeds;
  • every live key is found and no erased key is;
  • a later rehash that can allocate succeeds.

Without the fix the test aborts with SIGABRT; with it, the full test_cpp suite passes. The existing test_slot_lookup_churn still covers churn when the rehash succeeds.

Found by reviewers of matrixorigin/matrixone#30823, which vendors this fix.

The key-to-slot lookup and the free-slot ring allocated through
`std::allocator`, which throws inside their `noexcept` reserve paths, so
running out of memory terminated the process. Tombstones count toward the
load factor, so ordinary remove and add churn on a reserved index could
trigger that rehash.

Both now use `aligned_allocator_gt`, which returns `nullptr` like every
other buffer in `index_dense_gt`. When the rehash fails, `try_emplace`
still places the element in a free or deleted slot, `add` reports a failed
key mapping instead of ignoring it, and `load`, `view` and `compact`
report a failed reindex. A failed copy assignment of the lookup now leaves
an empty table instead of stale counts over a released buffer.
@ashvardanian
ashvardanian force-pushed the fix_rehash_oom_20261023 branch from 289616e to cd80088 Compare October 24, 2026 22:10
@ashvardanian
ashvardanian merged commit 1633e25 into unum-cloud:main-dev Oct 24, 2026
11 checks passed
xet7 pushed a commit to xet7/usearch that referenced this pull request Nov 12, 2026
The 2.x line released v2.25.2 through v2.26.6 after 3.0 branched at
v2.25.1, and its public main ends at 33d3ab6. Installs place headers
under GNUInstallDirs' include directory (14f9807, unum-cloud#800), the README
lists the integrations 2.x added (0fd2f32, unum-cloud#795), and CONTRIBUTING
states the commit conventions in place of stale links (06ad060, unum-cloud#748).

Its correctness fixes already hold on 3.0's code:

- Keys move with their entries when `compact` permutes slots
  (0a4baa6, unum-cloud#795), and the insert-only key table never rehashes
  (1633e25, unum-cloud#822; 9d77be5, unum-cloud#759)
- File loads bound every column slice from the header (9c25176, unum-cloud#824;
  64d10d8, unum-cloud#776), and allocation sizes are checked (89da7c5, unum-cloud#763)
- JavaScript keys are range-checked (b65a420, unum-cloud#812), Go filters ride
  `cgo.Handle`s (ad77fe7, unum-cloud#825), `splitmix64_t` survives `windows.h`
  (0f1c5f2, unum-cloud#806), and integer key hashes mix with SplitMix64
  (9fc3500, unum-cloud#773)
- C install targets and relocatable pkg-config (4160e47, 679e190,
  unum-cloud#800), the GIL released in long calls (d8be67d, unum-cloud#764), and graph
  cursors in C and Rust (55c43f7, unum-cloud#748)

Thread-context reserves, tombstones, renames, `change_metric`, OpenMP
metrics and hand-written BF16 casts are absent from 3.0, so their
fixes have nothing to land on. The Rust `compact()` binding (749d03b,
unum-cloud#771) waits for all bindings to wrap `usearch_dense_compact`.

The README's performance lead now quotes the SIFT 100M rows at 99%
recall on a B300 and on 192 Graviton5 cores beside FAISS HNSW, and
the verification notes link ForkUnion's `main`.

The sibling pins move to the libraries' own merges with their released
lines, so this line builds against them.

Co-Authored-By: Jonathan Ringer <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants