Skip to content

blockifier: use HashSet for O(1) lookups in trim_to_accessed_keys#14625

Open
gkaempfer wants to merge 1 commit into
mainfrom
claude/perf/blockifier-btree-hash-retain
Open

blockifier: use HashSet for O(1) lookups in trim_to_accessed_keys#14625
gkaempfer wants to merge 1 commit into
mainfrom
claude/perf/blockifier-btree-hash-retain

Conversation

@gkaempfer

Copy link
Copy Markdown
Contributor

Summary

Follow-up performance optimization to #14595.

AccessedKeys stores its three key sets as BTreeSet, so contains() is O(log K). trim_to_accessed_keys calls retain() on each StateMaps HashMap — iterating over all O(M) initial-read entries and calling contains() for each one, giving O(M log K) total across the four maps.

This PR pre-builds temporary HashSets from the BTreeSet iterators (O(K) each) before the retain calls, 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×.

AccessedKeys keeps its BTreeSet fields unchanged; the sorted order they provide for trie traversal is unaffected.


Generated by Claude Code

@gkaempfer gkaempfer requested a review from yoavGrs June 24, 2026 14:24
@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

@gkaempfer gkaempfer force-pushed the claude/perf/blockifier-btree-hash-retain branch from a1367c2 to 90edee6 Compare June 24, 2026 14:26
… 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>
@yoavGrs yoavGrs force-pushed the claude/perf/blockifier-btree-hash-retain branch from 90edee6 to 04dab46 Compare June 25, 2026 08:48

yoavGrs commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions

Copy link
Copy Markdown

Artifacts upload workflows:

@yoavGrs yoavGrs requested a review from itamar-starkware June 25, 2026 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants