blockifier: pre-reserve capacity when rebuilding trimmed state maps - #14937
blockifier: pre-reserve capacity when rebuilding trimmed state maps#14937gkaempfer wants to merge 1 commit into
Conversation
trim_to_accessed_keys() rebuilds each StateMaps HashMap via collect() on a filter_map iterator, whose lower size-hint bound is 0. That forces incremental reallocation and rehashing as entries are inserted. Reserve capacity for accessed_keys.len() up front since accessed keys are expected to almost always be present in the map being intersected. Follow-up to #14625. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KzWvNVLye1Jcrh1u2Pu5hd
PR SummaryLow Risk Overview Because Behavior is unchanged; this targets per-block overhead on the batcher path where Reviewed by Cursor Bugbot for commit 305e3ab. Bugbot is set up for automated code reviews on this repo. Configure here. |
|
Artifacts upload workflows: |
Summary
Follow-up performance optimization to #14625.
StateMaps::trim_to_accessed_keysrebuilds eachHashMapvia.collect()on afilter_mapiterator over the accessed-keysBTreeSet.filter_map's size hint has a lower bound of 0 (since the filter can drop any number of elements), socollect()'s internalreserve()call is a no-op — the resultingHashMapgrows through repeated reallocation and rehashing as elements are inserted instead of being sized once up front.This PR pre-reserves capacity for
min(keys.len(), map.len())before extending, which is a tight upper bound on the final size: accessed keys are (almost always) a subset of the map being intersected, sinceget_os_initial_readsalready force-reads the nonce/class-hash/compiled-class-hash of every contract that ends up inaccessed_keys.Why this matters:
trim_to_accessed_keysruns once per block inapollo_batcher's hot path. Avoiding the incremental grow-and-rehash cycle removes a constant-factor overhead from a function whose cost already scales with block size.Test plan
cargo build -p blockifiercargo clippy -p blockifier --lib(clean)SEED=0 cargo test -p blockifier --lib state::cached_state(43 passed)scripts/rust_fmt.sh(no diff beyond the change)Generated by Claude Code