diff --git a/execution/executor/src/block_executor.rs b/execution/executor/src/block_executor.rs index b9ae93b5ce62c..7cbc3811f718f 100644 --- a/execution/executor/src/block_executor.rs +++ b/execution/executor/src/block_executor.rs @@ -374,8 +374,7 @@ where parent_block.output.state().base_version, false, result_in_memory_state, - // TODO(grao): Avoid this clone. - ledger_update.state_updates_until_last_checkpoint.clone(), + ledger_update.state_updates_until_last_checkpoint.as_ref(), Some(&ledger_update.sharded_state_cache), )?; TRANSACTIONS_SAVED.observe(ledger_update.num_txns() as f64); diff --git a/execution/executor/src/chunk_executor.rs b/execution/executor/src/chunk_executor.rs index d54497c000ca9..c826ceb7fcc56 100644 --- a/execution/executor/src/chunk_executor.rs +++ b/execution/executor/src/chunk_executor.rs @@ -264,11 +264,10 @@ impl ChunkExecutorInner { chunk.ledger_info.as_ref(), false, // sync_commit chunk.result_state.clone(), - // TODO(aldenhu): avoid cloning chunk .ledger_update_output .state_updates_until_last_checkpoint - .clone(), + .as_ref(), Some(&chunk.ledger_update_output.sharded_state_cache), )?; } diff --git a/execution/executor/src/db_bootstrapper.rs b/execution/executor/src/db_bootstrapper.rs index 5015a48e84b9c..c27d91bef7375 100644 --- a/execution/executor/src/db_bootstrapper.rs +++ b/execution/executor/src/db_bootstrapper.rs @@ -112,7 +112,7 @@ impl GenesisCommitter { self.output .ledger_update_output .state_updates_until_last_checkpoint - .clone(), + .as_ref(), Some(&self.output.ledger_update_output.sharded_state_cache), )?; info!("Genesis commited."); diff --git a/execution/executor/src/fuzzing.rs b/execution/executor/src/fuzzing.rs index 58ae0bfac412a..d801df7234792 100644 --- a/execution/executor/src/fuzzing.rs +++ b/execution/executor/src/fuzzing.rs @@ -120,7 +120,7 @@ impl DbWriter for FakeDb { _base_state_version: Option, _sync_commit: bool, _latest_in_memory_state: StateDelta, - _state_updates_until_last_checkpoint: Option, + _state_updates_until_last_checkpoint: Option<&ShardedStateUpdates>, _sharded_state_cache: Option<&ShardedStateCache>, ) -> aptos_storage_interface::Result<()> { Ok(()) diff --git a/execution/executor/src/tests/mod.rs b/execution/executor/src/tests/mod.rs index e91e689e4b9a2..89b1e68dae89e 100644 --- a/execution/executor/src/tests/mod.rs +++ b/execution/executor/src/tests/mod.rs @@ -508,10 +508,9 @@ fn apply_transaction_by_writeset( ledger_info.as_ref(), true, /* sync_commit */ result_state, - // TODO(aldenhu): avoid clone ledger_update_output .state_updates_until_last_checkpoint - .clone(), + .as_ref(), Some(&ledger_update_output.sharded_state_cache), ) .unwrap(); @@ -720,10 +719,9 @@ fn run_transactions_naive( ledger_info.as_ref(), true, /* sync_commit */ result_state, - // TODO(aldenhu): avoid clone ledger_update_output .state_updates_until_last_checkpoint - .clone(), + .as_ref(), Some(&ledger_update_output.sharded_state_cache), ) .unwrap(); diff --git a/state-sync/state-sync-driver/src/tests/mocks.rs b/state-sync/state-sync-driver/src/tests/mocks.rs index 462bd0a76f4ee..382c2d24ec579 100644 --- a/state-sync/state-sync-driver/src/tests/mocks.rs +++ b/state-sync/state-sync-driver/src/tests/mocks.rs @@ -332,7 +332,7 @@ mock! { ledger_info_with_sigs: Option<&'a LedgerInfoWithSignatures>, sync_commit: bool, in_memory_state: StateDelta, - state_updates_until_last_checkpoint: Option, + state_updates_until_last_checkpoint: Option<&'b ShardedStateUpdates>, sharded_state_cache: Option<&'b ShardedStateCache>, ) -> Result<()>; } diff --git a/storage/aptosdb/src/db/fake_aptosdb.rs b/storage/aptosdb/src/db/fake_aptosdb.rs index 325c9fe4228c9..7c576d12e039c 100644 --- a/storage/aptosdb/src/db/fake_aptosdb.rs +++ b/storage/aptosdb/src/db/fake_aptosdb.rs @@ -104,7 +104,7 @@ impl FakeBufferedState { pub fn update( &mut self, - updates_until_next_checkpoint_since_current_option: Option, + updates_until_next_checkpoint_since_current_option: Option<&ShardedStateUpdates>, new_state_after_checkpoint: StateDelta, ) -> Result<()> { ensure!( diff --git a/storage/aptosdb/src/db/include/aptosdb_testonly.rs b/storage/aptosdb/src/db/include/aptosdb_testonly.rs index 5e67968b5fa3b..bf8ca7580850a 100644 --- a/storage/aptosdb/src/db/include/aptosdb_testonly.rs +++ b/storage/aptosdb/src/db/include/aptosdb_testonly.rs @@ -150,7 +150,7 @@ impl AptosDB { ledger_info_with_sigs, sync_commit, latest_in_memory_state, - state_updates_until_last_checkpoint, + state_updates_until_last_checkpoint.as_ref(), None, ) } diff --git a/storage/aptosdb/src/db/include/aptosdb_writer.rs b/storage/aptosdb/src/db/include/aptosdb_writer.rs index 28f8008ee57f0..f38a62c65e1ab 100644 --- a/storage/aptosdb/src/db/include/aptosdb_writer.rs +++ b/storage/aptosdb/src/db/include/aptosdb_writer.rs @@ -11,7 +11,7 @@ impl DbWriter for AptosDB { base_state_version: Option, sync_commit: bool, latest_in_memory_state: StateDelta, - state_updates_until_last_checkpoint: Option, + state_updates_until_last_checkpoint: Option<&ShardedStateUpdates>, sharded_state_cache: Option<&ShardedStateCache>, ) -> Result<()> { gauged_api("pre_commit_ledger", || { diff --git a/storage/aptosdb/src/db/test_helper.rs b/storage/aptosdb/src/db/test_helper.rs index 68265f72c0e34..59b1634da3f7c 100644 --- a/storage/aptosdb/src/db/test_helper.rs +++ b/storage/aptosdb/src/db/test_helper.rs @@ -1010,7 +1010,8 @@ pub fn test_sync_transactions_impl( cur_ver, &in_memory_state, txns_to_commit_batch, - ), + ) + .as_ref(), None, ) .unwrap(); @@ -1025,7 +1026,8 @@ pub fn test_sync_transactions_impl( Some(ledger_info_with_sigs), false, /* sync_commit */ in_memory_state.clone(), - gather_state_updates_until_last_checkpoint(ver, &in_memory_state, txns_to_commit_batch), + gather_state_updates_until_last_checkpoint(ver, &in_memory_state, txns_to_commit_batch) + .as_ref(), None, ) .unwrap(); diff --git a/storage/aptosdb/src/fast_sync_storage_wrapper.rs b/storage/aptosdb/src/fast_sync_storage_wrapper.rs index 703cfe9326f37..4b1fac3e7e284 100644 --- a/storage/aptosdb/src/fast_sync_storage_wrapper.rs +++ b/storage/aptosdb/src/fast_sync_storage_wrapper.rs @@ -176,7 +176,7 @@ impl DbWriter for FastSyncStorageWrapper { base_state_version: Option, sync_commit: bool, latest_in_memory_state: StateDelta, - state_updates_until_last_checkpoint: Option, + state_updates_until_last_checkpoint: Option<&ShardedStateUpdates>, sharded_state_cache: Option<&ShardedStateCache>, ) -> Result<()> { self.get_aptos_db_write_ref().pre_commit_ledger( diff --git a/storage/aptosdb/src/state_store/buffered_state.rs b/storage/aptosdb/src/state_store/buffered_state.rs index 83f5f55609071..253343be00c6a 100644 --- a/storage/aptosdb/src/state_store/buffered_state.rs +++ b/storage/aptosdb/src/state_store/buffered_state.rs @@ -154,7 +154,7 @@ impl BufferedState { /// This method updates the buffered state with new data. pub fn update( &mut self, - updates_until_next_checkpoint_since_current_option: Option, + updates_until_next_checkpoint_since_current_option: Option<&ShardedStateUpdates>, new_state_after_checkpoint: StateDelta, sync_commit: bool, ) -> Result<()> { diff --git a/storage/aptosdb/src/state_store/mod.rs b/storage/aptosdb/src/state_store/mod.rs index 1b30c6343eb89..3902125a08ae1 100644 --- a/storage/aptosdb/src/state_store/mod.rs +++ b/storage/aptosdb/src/state_store/mod.rs @@ -541,7 +541,7 @@ impl StateStore { // synchronously commit the snapshot at the last checkpoint here if not committed to disk yet. buffered_state.update( - updates_until_last_checkpoint, + updates_until_last_checkpoint.as_ref(), state_after_last_checkpoint, true, /* sync_commit */ )?; diff --git a/storage/storage-interface/src/lib.rs b/storage/storage-interface/src/lib.rs index ff09eca5e7095..8a357eda90116 100644 --- a/storage/storage-interface/src/lib.rs +++ b/storage/storage-interface/src/lib.rs @@ -548,7 +548,7 @@ pub trait DbWriter: Send + Sync { ledger_info_with_sigs: Option<&LedgerInfoWithSignatures>, sync_commit: bool, latest_in_memory_state: StateDelta, - state_updates_until_last_checkpoint: Option, + state_updates_until_last_checkpoint: Option<&ShardedStateUpdates>, sharded_state_cache: Option<&ShardedStateCache>, ) -> Result<()> { // For reconfig suffix. @@ -596,7 +596,7 @@ pub trait DbWriter: Send + Sync { base_state_version: Option, sync_commit: bool, latest_in_memory_state: StateDelta, - state_updates_until_last_checkpoint: Option, + state_updates_until_last_checkpoint: Option<&ShardedStateUpdates>, sharded_state_cache: Option<&ShardedStateCache>, ) -> Result<()> { unimplemented!() diff --git a/storage/storage-interface/src/state_delta.rs b/storage/storage-interface/src/state_delta.rs index a4534e323a9fd..bbe4016a86b15 100644 --- a/storage/storage-interface/src/state_delta.rs +++ b/storage/storage-interface/src/state_delta.rs @@ -74,7 +74,7 @@ impl StateDelta { pub fn merge(&mut self, other: StateDelta) { assert!(other.follow(self)); - combine_sharded_state_updates(&mut self.updates_since_base, other.updates_since_base); + combine_sharded_state_updates(&mut self.updates_since_base, &other.updates_since_base); self.current = other.current; self.current_version = other.current_version; diff --git a/types/src/state_store/mod.rs b/types/src/state_store/mod.rs index f9632a48ff86c..3b16a4f75d6fe 100644 --- a/types/src/state_store/mod.rs +++ b/types/src/state_store/mod.rs @@ -102,25 +102,14 @@ pub fn create_empty_sharded_state_updates() -> ShardedStateUpdates { arr![HashMap::new(); 16] } -pub fn combine_or_add_sharded_state_updates( - lhs: &mut Option, - rhs: ShardedStateUpdates, -) { - if let Some(lhs) = lhs { - combine_sharded_state_updates(lhs, rhs); - } else { - *lhs = Some(rhs); - } -} - -pub fn combine_sharded_state_updates(lhs: &mut ShardedStateUpdates, rhs: ShardedStateUpdates) { +pub fn combine_sharded_state_updates(lhs: &mut ShardedStateUpdates, rhs: &ShardedStateUpdates) { use rayon::prelude::*; THREAD_MANAGER.get_exe_cpu_pool().install(|| { lhs.par_iter_mut() - .zip_eq(rhs.into_par_iter()) + .zip_eq(rhs.par_iter()) .for_each(|(l, r)| { - l.extend(r); + l.extend(r.clone()); }) }) }