Repository navigation
Fix: Keep key lookups usable when the rehash cannot allocate - #822
Merged
ashvardanian merged 1 commit intoOct 24, 2026
Merged
Conversation
2 of 7 tasks
ashvardanian
force-pushed
the
fix_rehash_oom_20261023
branch
2 times, most recently
from
October 24, 2026 22:04
ccb7dd4 to
289616e
Compare
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
force-pushed
the
fix_rehash_oom_20261023
branch
from
October 24, 2026 22:10
289616e to
cd80088
Compare
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
flat_hash_multi_set_gt::try_reserve()isnoexcept, but it allocates withstd::allocator<char>, which throwsstd::bad_allocinstead of returningnullptr. An allocation failure therefore callsstd::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 duringreserve(). So an allocation failure insideindex_dense_gt::add()aborts the process instead of returning an error.add_()also ignored the result oftry_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 newfor 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 returnsfalse, 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_()returnsfailed("Failed to map the key to its slot")if the key mapping fails, instead of ignoring it.Tests
The new
test_slot_lookup_rehash_failureruns 1,024 erase/emplace rounds on a 128-entry set with an allocator that throws, then checks:Without the fix the test aborts with SIGABRT; with it, the full
test_cppsuite passes. The existingtest_slot_lookup_churnstill covers churn when the rehash succeeds.Found by reviewers of matrixorigin/matrixone#30823, which vendors this fix.