blockifier: use HashSet for O(1) lookups in trim_to_accessed_keys#14625
Open
gkaempfer wants to merge 1 commit into
Open
blockifier: use HashSet for O(1) lookups in trim_to_accessed_keys#14625gkaempfer wants to merge 1 commit into
gkaempfer wants to merge 1 commit into
Conversation
a1367c2 to
90edee6
Compare
… the accessed-key side trim_to_accessed_keys rebuilds each map by iterating the (smaller) AccessedKeys sets and probing the O(1) HashMaps, keeping the keys present in both. This is O(|accessed_keys|) and needs no temporary HashSet, beating both retain-with-BTreeSet-contains (O(M log N)) and retain-with-HashSet (O(M+N) plus three allocations). declared_contracts is intentionally left untrimmed. Document at the batcher call site why the trim is a two-way asymmetry: reverted-tx reads are dropped, and write-only accessed keys (aliases, proof-fact block hashes) are absent from the reads by design. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
90edee6 to
04dab46
Compare
|
Artifacts upload workflows: |
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
Follow-up performance optimization to #14595.
AccessedKeysstores its three key sets asBTreeSet, socontains()is O(log K).trim_to_accessed_keyscallsretain()on eachStateMapsHashMap — iterating over all O(M) initial-read entries and callingcontains()for each one, giving O(M log K) total across the four maps.This PR pre-builds temporary
HashSets from theBTreeSetiterators (O(K) each) before theretaincalls, reducing each lookup from O(log K) to O(1) and the full trim to O(K + M).Why this matters:
M= all state reads across the block (including reverted transactions). As block size grows, or as reverted-transaction overhead increases, this saves proportionally more. For a representative block with 10 000 storage reads and 8 000 accessed keys (log₂ ≈ 13), this reduces ~130 000 BTreeSet comparisons to ~18 000 hash operations — roughly 7×.AccessedKeyskeeps itsBTreeSetfields unchanged; the sorted order they provide for trie traversal is unaffected.Generated by Claude Code