fix(dict): dict_remove no longer inflates the hash table exponentially#531
Merged
Merged
Conversation
env_hash_rebuild blindly doubled the old capacity — right for the grow-on-insert callers (all rebuild at >70% load), catastrophic for dict_remove's re-index rebuild: N removes on one dict grew its table by 2^N, so ~25 insert/remove cycles of a single key allocated gigabytes and aborted OOM. Size the new table from the live entry count instead — identical doubling behavior at the grow sites, no inflation on remove. Surfaced by liferaft's per-message pending-registry churn during the #523 task-layer migration; regression-pinned with a 200-cycle churn test (impossible pre-fix, instant post-fix) plus a survivors-resolve check in tests/test_dict.eigs. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
The bug
env_hash_rebuildunconditionally doubled the old capacity. Every grow-path caller rebuilds at >70% load, where doubling is correct — butdict_removerebuilds merely to re-index after a removal (indices shift down), and blind doubling there inflates a shrinking dict's table by 2^N over N removes.Minimal repro (aborts OOM at ~25 iterations pre-fix, 128MB+ single allocation):
This is also what repeatedly took down the dev box today: the #523 liferaft task-layer migration does one
dict_removeper delivered message on a small registry dict — the first workload to ever churndict_removeon a long-lived dict. Root-caused via gdb on the malloc-failure abort underulimit -v(backtrace:x_oom ← xcalloc ← env_hash_rebuild ← builtin_dict_remove).The fix
Size the rebuilt table from the live entry count (
new_cap = next_pow2(max(count*2, ENV_HASH_INIT_CAP))). At the >70%-load grow sitescount*2lands on exactly the old doubling; at the remove site the table now tracks the dict's actual size.Tests
tests/test_dict.eigs: 200-cycle single-key churn (impossible pre-fix, instant post-fix) + a churn-with-survivors check pinning lookup correctness across re-index rebuilds.Validation
detect_leaks=1: 2703/2703, leak tally 0Related: #530 (the second gap the same migration surfaced — task handle reaping).
🤖 Generated with Claude Code