perf: add direct-mapped node cache to BTreeMap#416
Draft
sasa-tomic wants to merge 1 commit intomainfrom
Draft
Conversation
Add a 32-slot direct-mapped node cache to BTreeMap that avoids re-loading hot nodes from stable memory. Modeled after CPU caches: O(1) lookup via (address / page_size) % 32, collision = eviction. Read paths (get, contains_key, first/last_key_value) use a take+return pattern to borrow nodes from the cache without RefCell lifetime issues. Write paths (insert, remove, split, merge) invalidate affected cache slots. Key changes: - Switch get() from destructive extract_entry_at to node.value() - Remove unused extract_entry_at method - Change traverse() closure from Fn(&mut Node) to Fn(&Node) - Invalidate cache in save_node, deallocate_node, merge, clear_new Expected improvement: ~15-20% for random reads, ~65% for hot-key workloads, ~0% overhead for writes (cache.get_mut() bypasses RefCell).
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.
Summary
(address / page_size) % 32, collision = eviction (no LRU tracking)get,contains_key,first/last_key_value) use a take+return pattern to avoid re-loading hot upper-tree nodes from stable memorysave_node,deallocate_node,merge, andclear_newget()from destructiveextract_entry_at(swap_remove) to non-destructivenode.value()(borrows via OnceCell)extract_entry_atmethodThis subsumes all four previous caching approaches (root-only, LRU+clone, LRU+Rc, page cache) into a single design that:
Node<K>directly (no Rc, no Clone, no heap allocation per cache entry)cache.get_mut()on write paths (zero RefCell overhead)Expected improvement: ~15-20% for random reads, ~65% for hot-key workloads, ~0% overhead for writes.