Skip to content

Commit

Permalink
remove vec allocation in CRDS deserialize (#5143)
Browse files Browse the repository at this point in the history
* remove vec allocation in CRDS deserialize

---------

Co-authored-by: Alex Pyattaev <alex.pyattaev@anza.xyz>
  • Loading branch information
alexpyattaev and Alex Pyattaev authored Mar 9, 2025
1 parent 1bcf743 commit 49fb512
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gossip/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license = { workspace = true }
edition = { workspace = true }

[dependencies]
arrayvec = { workspace = true }
assert_matches = { workspace = true }
bincode = { workspace = true }
bv = { workspace = true, features = ["serde"] }
Expand Down
10 changes: 8 additions & 2 deletions gossip/src/crds_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ use {
duplicate_shred::DuplicateShredIndex,
epoch_slots::EpochSlots,
},
arrayvec::ArrayVec,
bincode::serialize,
rand::Rng,
serde::de::{Deserialize, Deserializer},
solana_hash::Hash,
solana_keypair::{signable::Signable, Keypair},
solana_packet::PACKET_DATA_SIZE,
solana_pubkey::Pubkey,
solana_sanitize::{Sanitize, SanitizeError},
solana_signature::Signature,
Expand Down Expand Up @@ -227,8 +229,12 @@ impl<'de> Deserialize<'de> for CrdsValue {
data: CrdsData,
}
let CrdsValue { signature, data } = CrdsValue::deserialize(deserializer)?;
let bincode_serialized_data = bincode::serialize(&data).unwrap();
let hash = solana_sha256_hasher::hashv(&[signature.as_ref(), &bincode_serialized_data]);
// To compute the hash of the received CrdsData we need to re-serialize it
// PACKET_DATA_SIZE is always enough since we have just received the value in a packet
// ArrayVec allows us to write serialized data into stack memory without initializing it
let mut buffer = ArrayVec::<u8, PACKET_DATA_SIZE>::new();
bincode::serialize_into(&mut buffer, &data).map_err(serde::de::Error::custom)?;
let hash = solana_sha256_hasher::hashv(&[signature.as_ref(), &buffer]);
Ok(Self {
signature,
data,
Expand Down
1 change: 1 addition & 0 deletions programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions svm/examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 49fb512

Please sign in to comment.