Repository navigation
Pre-allocate the slab in reserve() - #123
Merged
Merged
Conversation
reserve() only grew the hash table, not the LinkedSlab Vec that holds the actual entries. Under a large insert burst, that Vec doubled and memcpy'd hundreds of MB while the shard write lock was held, stalling all concurrent readers on that shard (issue #119). Now reserve() also pre-allocates the slab, sized for the requested entries plus the bounded ghost-key space (capacity_non_resident) instead of a fixed 50% guess.
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
Cache::reserve()only reserved the internal hash table, not theLinkedSlabVecthat holds the actual entries. The slab is the structure that reallocates as the cache grows.During a large insert burst, when a shard's slab fills,
Vec::pushreallocates by doubling — allocating a new buffer (2×) andmemcpying the old contents — all while the shard's exclusive write lock is held. Every concurrentgeton that shard needs the shared read lock and blocks for the full duration. With ~80-byte entries this is hundreds of MB copied per growth at large sizes, producing multi-hundred-ms to second-level reader stalls at power-of-two thresholds. See #119.Because
reserve()didn't touch the slab, pre-reserving did nothing to prevent these stalls — only the (much smaller) hash table was actually pre-allocated.Fix
CacheShard::reservenow also pre-allocates the slab via a newLinkedSlab::reserve. The reservation is sized for the requested entries plus the bounded ghost-key space (capacity_non_resident) rather than the previous fixed+50%guess (which only matched the defaultghost_allocationof 0.5).With this, the slab is allocated once up front and inserts no longer reallocate/copy under the lock — the page-fault cost is paid lazily and incrementally per insert.
Tests
LinkedSlab::reserveunit test asserting no reallocation occurs for inserts up to the reserved capacity.