avoid data copies in writing to cache (#23674)

This commit is contained in:
Jeff Washington (jwash) 2022-03-15 16:42:26 -05:00 committed by GitHub
parent 29af42f428
commit b5a99b9b09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 1 deletions

View File

@ -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)]

View File

@ -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> {