Removes copying owner when serializing Account (#35118)

* Removes copying owner when serializing Account

* Provide generic AbiExample impls for &T and &[T]

---------

Co-authored-by: Ryo Onodera <ryoqun@gmail.com>
This commit is contained in:
Brooks 2024-02-20 10:16:46 -05:00 committed by GitHub
parent 13f232436a
commit 012f588482
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 17 additions and 27 deletions

1
Cargo.lock generated
View File

@ -6007,7 +6007,6 @@ dependencies = [
"either",
"generic-array 0.14.7",
"im",
"lazy_static",
"log",
"memmap2",
"rustc_version 0.4.0",

View File

@ -12,7 +12,6 @@ edition = { workspace = true }
[dependencies]
bs58 = { workspace = true }
bv = { workspace = true, features = ["serde"] }
lazy_static = { workspace = true }
log = { workspace = true, features = ["std"] }
serde = { workspace = true, features = ["derive", "rc"] }
serde_bytes = { workspace = true }

View File

@ -1,6 +1,5 @@
use {
crate::abi_digester::{AbiDigester, DigestError, DigestResult},
lazy_static::lazy_static,
log::*,
serde::Serialize,
std::any::type_name,
@ -350,6 +349,20 @@ fn leak_and_inhibit_drop<'a, T>(t: T) -> &'a mut T {
Box::leak(Box::new(t))
}
impl<T: AbiExample> AbiExample for &T {
fn example() -> Self {
info!("AbiExample for (&T): {}", type_name::<Self>());
leak_and_inhibit_drop(T::example())
}
}
impl<T: AbiExample> AbiExample for &[T] {
fn example() -> Self {
info!("AbiExample for (&[T]): {}", type_name::<Self>());
leak_and_inhibit_drop(vec![T::example()])
}
}
impl<T: AbiExample> AbiExample for std::sync::Weak<T> {
fn example() -> Self {
info!("AbiExample for (Arc's Weak<T>): {}", type_name::<Self>());
@ -434,25 +447,6 @@ impl<T: AbiExample> AbiExample for Vec<T> {
}
}
lazy_static! {
/// we need &Vec<u8>, so we need something with a static lifetime
static ref VEC_U8: Vec<u8> = vec![u8::default()];
}
impl AbiExample for &Vec<u8> {
fn example() -> Self {
info!("AbiExample for (&Vec<u8>): {}", type_name::<Self>());
&VEC_U8
}
}
impl AbiExample for &[u8] {
fn example() -> Self {
info!("AbiExample for (&[u8]): {}", type_name::<Self>());
&VEC_U8[..]
}
}
impl<T: AbiExample> AbiExample for VecDeque<T> {
fn example() -> Self {
info!("AbiExample for (Vec<T>): {}", type_name::<Self>());

View File

@ -5037,7 +5037,6 @@ dependencies = [
"either",
"generic-array 0.14.7",
"im",
"lazy_static",
"log",
"memmap2",
"rustc_version",

View File

@ -63,15 +63,14 @@ mod account_serialize {
#[serde(with = "serde_bytes")]
// a slice so we don't have to make a copy just to serialize this
data: &'a [u8],
// can't be &pubkey because abi example doesn't support it
owner: Pubkey,
owner: &'a Pubkey,
executable: bool,
rent_epoch: Epoch,
}
/// allows us to implement serialize on AccountSharedData that is equivalent to Account::serialize without making a copy of the Vec<u8>
pub fn serialize_account<S>(
account: &(impl ReadableAccount + Serialize),
account: &impl ReadableAccount,
serializer: S,
) -> Result<S::Ok, S::Error>
where
@ -80,7 +79,7 @@ mod account_serialize {
let temp = Account {
lamports: account.lamports(),
data: account.data(),
owner: *account.owner(),
owner: account.owner(),
executable: account.executable(),
rent_epoch: account.rent_epoch(),
};