From b5a99b9b09cf8759744e17fa8b93f9a9025738fb Mon Sep 17 00:00:00 2001 From: "Jeff Washington (jwash)" Date: Tue, 15 Mar 2022 16:42:26 -0500 Subject: [PATCH] avoid data copies in writing to cache (#23674) --- runtime/src/accounts_db.rs | 15 ++++++++++++++- sdk/src/account.rs | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/runtime/src/accounts_db.rs b/runtime/src/accounts_db.rs index 4893f3104c..67ff75aece 100644 --- a/runtime/src/accounts_db.rs +++ b/runtime/src/accounts_db.rs @@ -57,7 +57,7 @@ use { solana_measure::measure::Measure, solana_rayon_threadlimit::get_thread_count, solana_sdk::{ - account::{AccountSharedData, ReadableAccount}, + account::{AccountSharedData, ReadableAccount, WritableAccount}, clock::{BankId, Epoch, Slot, SlotCount}, epoch_schedule::EpochSchedule, genesis_config::{ClusterType, GenesisConfig}, @@ -566,6 +566,19 @@ impl<'a> ReadableAccount for LoadedAccount<'a> { LoadedAccount::Cached(cached_account) => cached_account.account.rent_epoch(), } } + fn to_account_shared_data(&self) -> AccountSharedData { + match self { + LoadedAccount::Stored(_stored_account_meta) => AccountSharedData::create( + self.lamports(), + self.data().to_vec(), + *self.owner(), + self.executable(), + self.rent_epoch(), + ), + // clone here to prevent data copy + LoadedAccount::Cached(cached_account) => cached_account.account.clone(), + } + } } #[derive(Clone, Default, Debug)] diff --git a/sdk/src/account.rs b/sdk/src/account.rs index f5b27fed56..d0cae18c24 100644 --- a/sdk/src/account.rs +++ b/sdk/src/account.rs @@ -253,6 +253,10 @@ impl ReadableAccount for AccountSharedData { fn rent_epoch(&self) -> Epoch { self.rent_epoch } + fn to_account_shared_data(&self) -> AccountSharedData { + // avoid data copy here + self.clone() + } } impl ReadableAccount for Ref<'_, AccountSharedData> { @@ -271,6 +275,16 @@ impl ReadableAccount for Ref<'_, AccountSharedData> { fn rent_epoch(&self) -> Epoch { self.rent_epoch } + fn to_account_shared_data(&self) -> AccountSharedData { + AccountSharedData { + lamports: self.lamports(), + // avoid data copy here + data: Arc::clone(&self.data), + owner: *self.owner(), + executable: self.executable(), + rent_epoch: self.rent_epoch(), + } + } } impl ReadableAccount for Ref<'_, Account> {