add cow_bytes: O(dirty_nodes) pairwise tree diff#100
Open
dapplion wants to merge 6 commits into
Open
Conversation
…rement Add `cow_bytes(base, derived)` — computes the bytes owned by `derived` that are not shared with `base` by walking both trees in parallel. When Arc pointers match, the subtree is shared and skipped immediately (O(1)). When they differ, the derived node is counted and children are recursed. This is dramatically faster than MemoryTracker for measuring COW cost: - 1M entries, 1 dirty leaf: 180ns vs 450ms (2,500,000x faster) - 1M entries, 128 dirty: 5.8µs vs 450ms (76,000x faster) - 1M entries, all dirty: 4.6ms vs 450ms (100x faster) The key difference: no HashMap allocation (MemoryTracker spends 26% of time on HashMap insert/rehash), and shared subtrees are never visited (MemoryTracker must walk the entire base to build its seen-set). API: - `List::cow_bytes(&self, base: &Self) -> usize` - `List::total_tree_bytes(&self) -> usize` - `Vector::cow_bytes(&self, base: &Self) -> usize` - `Vector::total_tree_bytes(&self) -> usize` - `mem::cow_tree_bytes(base, derived)` — operates on raw `Arc<Tree<T>>` - `mem::total_tree_bytes(tree)` — total bytes, no sharing baseline
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #100 +/- ##
==========================================
+ Coverage 70.15% 71.02% +0.86%
==========================================
Files 22 22
Lines 1280 1346 +66
==========================================
+ Hits 898 956 +58
- Misses 382 390 +8 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…iple states Walks multiple derived trees against a shared base, deduplicating COW'd nodes across states using a HashSet. Nodes shared with base are skipped via Arc::ptr_eq (no base walk). Nodes already counted from another derived state are skipped via HashSet lookup. This is O(total_unique_dirty_nodes) — each unique COW'd node is visited exactly once across all states. The HashSet only contains dirty nodes, keeping it small. Benchmarks at 1M entries: | Scenario | Cache | Time | Dedup | |-----------------------------|-------|---------|-------| | Slot transitions (indep) | 128 | 638 µs | 1.0x | | Slot transitions (chained) | 128 | 738 µs | 27.9x | | Effective balance updates | 128 | 37 ms | 1.0x | | Epoch transitions | 8 | 232 ms | 1.0x | | Mixed (4 epoch + 124 chain) | 128 | 109 ms | - | API: - `mem::total_unique_cow_tree_bytes(base, &[derived_roots])` — raw trees - `List::tree_root()` — access inner Arc<Tree> for use with the above
Add MiniState with 4 fields (validators 128B×1M, balances u64×1M, inactivity u64×1M, participation u8×1M) matching the dominant BeaconState fields. Results at 1M validators: - 128 chained slot states: 1.9ms, 4 MB unique - 128 independent slot states: 1.8ms, 4 MB unique - 4 epoch + 124 slot chain: 225ms, 360 MB unique - 4 epoch + 4 eff_bal + 120: 229ms, 362 MB unique Epoch boundary states dominate — each has ~89 MB of fully-rewritten trees. Slot-only caches are sub-2ms.
Member
|
I would like to merge this, it looks good. Not sure why the memory size test is failing. |
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
Add
cow_bytes(base, derived)for measuring the COW memory cost of a derived treerelative to its base. Walks both trees in parallel — when
Arcpointers match, thesubtree is shared and skipped immediately. Only divergent nodes are visited and counted.
This is intended for Lighthouse's state cache byte budget: measuring how many bytes
each cached state owns beyond the shared finalized base, cheaply enough to run on
every slot transition.
Benchmarks (1M u64 entries, capacity 2^40)
API
List::cow_bytes(&self, base: &Self) -> usizeList::total_tree_bytes(&self) -> usizeVector::cow_bytes(&self, base: &Self) -> usizeVector::total_tree_bytes(&self) -> usizemem::cow_tree_bytes(base, derived)— rawArc<Tree<T>>versionmem::total_tree_bytes(tree)— full size, no sharing baselineTest plan
cow_bytes_identical_is_zero— clone has 0 costcow_bytes_single_mutation— single dirty leaf, cost << totalcow_bytes_all_dirty— all leaves dirty, cost ≈ totalcow_bytes_matches_tracker_differential— agrees with MemoryTracker within List struct overheadcow_bytes_vector— works on Vector toocow_bytes_chain— A→B→C: C vs A >= C vs Btotal_tree_bytes_nonzero— sanity check