Skip to content

blockifier,apollo_consensus_orchestrator: avoid state-diff clone in synced-block accessed keys - #14971

Open
gkaempfer wants to merge 3 commits into
mainfrom
claude/perf/apollo-consensus-orchestrator-avoid-state-diff-clone-83417
Open

blockifier,apollo_consensus_orchestrator: avoid state-diff clone in synced-block accessed keys#14971
gkaempfer wants to merge 3 commits into
mainfrom
claude/perf/apollo-consensus-orchestrator-avoid-state-diff-clone-83417

Conversation

@gkaempfer

Copy link
Copy Markdown
Contributor

Summary

Follow-up performance optimization on #14826 (merged), which added accessed-keys computation to try_sync in sequencer_consensus_context.rs.

When fetch_accessed_keys_from_centralized is enabled, try_sync computed the synced block's accessed keys via:

Some(block_data.compute_accessed_keys(&sync_block.state_diff.clone().into()))

sync_block.state_diff is a ThinStateDiff; .clone().into() deep-clones it and converts it into a CommitmentStateDiff purely so compute_accessed_keys (which only reads the state diff by reference) can use it — the clone is thrown away once the call returns, while sync_block (with its original, un-cloned state_diff) is then moved whole into self.deps.batcher.add_sync_block(sync_block, accessed_keys). This clone runs on every synced block once the feature is enabled, at a cost proportional to the block's state-diff size.

Since AccessedKeys::from_call_infos (and the alias-compression prediction it calls into) only ever read the state diff by reference, this PR adds a StateDiffView trait (crates/blockifier/src/state/cached_state.rs) implemented for both CommitmentStateDiff (used when building a block) and ThinStateDiff (used when syncing one), and generalizes AccessedKeys::new/from_call_infos and predicted_alias_storage_entries to accept &impl StateDiffView instead of a concrete &CommitmentStateDiff. compute_accessed_keys now takes &ThinStateDiff directly, and the try_sync call site borrows sync_block.state_diff instead of cloning it.

As a side effect, CommitmentStateDiff::get_contract_addresses — whose only two callers were migrated to the trait's contract_addresses() default method — is now dead and was removed.

All existing callers of AccessedKeys::new (apollo_batcher, starknet_os_flow_tests) pass a concrete &CommitmentStateDiff, which still implements the trait, so they're unaffected.

Test plan

  • cargo build -p blockifier, cargo build -p apollo_consensus_orchestrator — clean
  • cargo test -p blockifier (state::accessed_keys, state::stateful_compression, including test_predicted_alias_storage_entries_parity) — all pass
  • cargo test -p apollo_consensus_orchestrator (cende::, including compute_accessed_keys, and try_sync) — all pass
  • cargo check -p apollo_batcher --tests, cargo check -p starknet_os_flow_tests --tests, cargo check -p central_systest_blobs --tests — clean (confirms the other AccessedKeys::new call sites are unaffected)
  • cargo clippy -p blockifier -p apollo_consensus_orchestrator -p apollo_batcher --tests --all-targets -- -D warnings — clean
  • scripts/rust_fmt.sh — no changes needed
  • Reviewed by an independent pass focused on correctness of the field mapping between ThinStateDiff and CommitmentStateDiff (in particular the intentional drop of deprecated_declared_classes, mirroring the existing From<ThinStateDiff> for CommitmentStateDiff impl) and trait design; findings addressed (moved the trait next to CommitmentStateDiff, gave accessors neutral names, deduplicated the repeated address-chain into a default trait method, removed the now-dead get_contract_addresses)

Generated by Claude Code

yoavGrs and others added 3 commits August 13, 2026 15:29
When fetch_accessed_keys_from_centralized is enabled, try_sync fetches the
synced block's accessed keys from the recorder and forwards them to the
batcher so it can build the block's witness locally; on recorder failure it
logs and falls back to CommitBlock with None.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
… computing synced-block accessed keys

try_sync cloned the entire synced block's ThinStateDiff on every synced
block (when fetch_accessed_keys_from_centralized is enabled) just to
convert it into a CommitmentStateDiff for accessed-keys computation.
AccessedKeys::from_call_infos only ever reads the state diff by
reference, so add a StateDiffAccessedKeysView trait implemented for
both CommitmentStateDiff and ThinStateDiff, letting the accessed-keys
computation borrow the synced block's state diff directly instead of
cloning it.
…_addresses

Code review follow-up on the previous commit: move the state-diff view
trait next to CommitmentStateDiff (its natural home, and avoids a
module cycle with stateful_compression), give its accessors neutral
names shared by both impls instead of names borrowed from
CommitmentStateDiff's own fields, fold the repeated
storage/nonce/class-hash address chain into one default
`contract_addresses` method instead of duplicating it in two files, and
delete CommitmentStateDiff::get_contract_addresses now that both of its
callers were migrated to the trait.
@cursor

cursor Bot commented Aug 16, 2026

Copy link
Copy Markdown

PR Summary

Low Risk
Refactor with equivalent read-only field mapping; existing CommitmentStateDiff callers unchanged. Sync-path behavior when the centralized flag is enabled should match prior logic without the extra clone.

Overview
Avoids deep-cloning the synced block’s ThinStateDiff into a CommitmentStateDiff when computing accessed keys during try_sync (when fetch_accessed_keys_from_centralized is on). The sync path now borrows sync_block.state_diff and passes it through compute_accessed_keys into the batcher’s add_sync_block.

Introduces a StateDiffView trait in blockifier, implemented for both CommitmentStateDiff and ThinStateDiff, and generalizes AccessedKeys::new / from_call_infos and predicted_alias_storage_entries to take &impl StateDiffView. BlockAccessedKeysData::compute_accessed_keys now accepts &ThinStateDiff directly. CommitmentStateDiff::get_contract_addresses is removed in favor of the trait’s contract_addresses().

Tests are updated to use ThinStateDiff in cende accessed-keys tests and to cover try_sync forwarding (or skipping) accessed keys based on the config flag.

Reviewed by Cursor Bugbot for commit 20b07d6. Bugbot is set up for automated code reviews on this repo. Configure here.

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

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.

4 participants