Skip to content

apollo_consensus_orchestrator: avoid cloning the synced block's state diff to compute accessed keys - #14974

Open
gkaempfer wants to merge 2 commits into
mainfrom
claude/perf/apollo-consensus-orchestrator-avoid-clone-27628
Open

apollo_consensus_orchestrator: avoid cloning the synced block's state diff to compute accessed keys#14974
gkaempfer wants to merge 2 commits into
mainfrom
claude/perf/apollo-consensus-orchestrator-avoid-clone-27628

Conversation

@gkaempfer

Copy link
Copy Markdown
Contributor

Summary

Follow-up performance optimization on top of #14962 (merged), which added BlockAccessedKeysData::compute_accessed_keys and wired it into try_sync in sequencer_consensus_context.rs.

In try_sync, computing the accessed keys for a synced block did:

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

This deep-clones the entire ThinStateDiff of the synced block — which can hold thousands of storage/nonce/class-hash entries for a full block — purely to obtain an owned value to convert into a borrowed CommitmentStateDiff. The clone exists only because sync_block is later moved by value into self.deps.batcher.add_sync_block(sync_block, accessed_keys), so the original state diff had to be preserved.

Since From<ThinStateDiff> for CommitmentStateDiff (and the reverse) are cheap field-moves (no allocation) in blockifier::state::cached_state, this PR instead moves the state diff into the CommitmentStateDiff via std::mem::take, computes the accessed keys, and moves it back — avoiding the clone of the block's storage diff on the sync path.

One subtlety: CommitmentStateDiff has no deprecated_declared_classes field (present on ThinStateDiff, used by the state-diff hash and by storage), so that field is carried across the conversion explicitly instead of being silently dropped.

This runs once per synced block height, on the consensus sync path.

Changes

  • sequencer_consensus_context.rs: avoid the clone in try_sync, preserving deprecated_declared_classes across the ThinStateDiffCommitmentStateDiff round-trip.
  • sequencer_consensus_context_test.rs: extended try_sync_forwards_accessed_keys_from_centralized to use a non-empty state diff (including deprecated_declared_classes) and assert add_sync_block receives it unchanged.

Test plan

  • cargo build -p apollo_consensus_orchestrator
  • SEED=0 cargo test -p apollo_consensus_orchestrator (203 passed)
  • cargo clippy -p apollo_consensus_orchestrator --all-targets (clean)
  • scripts/rust_fmt.sh
  • Reviewed by an Opus subagent; findings addressed (lossy round-trip of deprecated_declared_classes, missing test coverage)

Generated by Claude Code

@cursor

cursor Bot commented Aug 17, 2026

Copy link
Copy Markdown

PR Summary

Low Risk
Localized sync-path performance and correctness fix with explicit preservation of deprecated class declarations; behavior is covered by an extended unit test.

Overview
On the consensus sync path, when centralized accessed-keys fetching is enabled, try_sync no longer deep-clones the synced block's ThinStateDiff just to call compute_accessed_keys. It mem::takes the diff, converts to CommitmentStateDiff by move, computes keys, then rebuilds ThinStateDiff for add_sync_block.

deprecated_declared_classes is peeled off before conversion and reattached afterward, because CommitmentStateDiff omits that field and a naive round-trip would drop it.

The try_sync_forwards_accessed_keys_from_centralized test now uses a non-empty diff (including deprecated classes) and asserts add_sync_block gets the same state_diff as state sync returned.

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

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

claude added 2 commits August 17, 2026 09:41
…g accessed keys

try_sync cloned the full ThinStateDiff of the synced block just to borrow it as
a CommitmentStateDiff for compute_accessed_keys. Move the state diff into the
CommitmentStateDiff instead (a cheap field rename) and move it back afterward,
since sync_block is later forwarded to add_sync_block by value.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SJn1K1NgLztcwQaKCseg7m
…cross the diff conversion

Code review caught that the previous commit's round-trip through
CommitmentStateDiff silently dropped ThinStateDiff::deprecated_declared_classes
(no counterpart in CommitmentStateDiff), which feeds the state-diff hash and
storage. Carry the field across the conversion explicitly, and add test
coverage asserting add_sync_block receives the state diff unchanged.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SJn1K1NgLztcwQaKCseg7m
@gkaempfer
gkaempfer force-pushed the claude/perf/apollo-consensus-orchestrator-avoid-clone-27628 branch from 14da353 to 2b36420 Compare August 17, 2026 09:42
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