Skip to content

Commit

Permalink
removes serde::Serialize trait bound from Packet::{from_data,populate…
Browse files Browse the repository at this point in the history
…_packet} (anza-xyz#3636)

As part of anza-xyz#3575
we need to implement bincode (de)serialization without deriving serde
Serialize/Deserialize traits.

Packet::{from_data,populate_packet} methods also only require that the
data to be bincode serializable and do not care about serde either.

Next version of bincode provides the necessary traits to achieve this
functionality:
https://docs.rs/bincode/2.0.0-rc.3/bincode/enc/trait.Encode.html
but that is not released yet.

In order to avoid serde::Serialize trait bound on
Packet::{from_data,populate_packet}, the commit adds a new Encode trait
which only requires implementing bincode serialization.
  • Loading branch information
behzadnouri authored Nov 18, 2024
1 parent c2880bb commit d131c2b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
6 changes: 3 additions & 3 deletions perf/src/packet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! The `packet` module defines data structures and methods to pull data from the network.
pub use solana_packet::{Meta, Packet, PacketFlags, PACKET_DATA_SIZE};
pub use solana_packet::{self, Meta, Packet, PacketFlags, PACKET_DATA_SIZE};
use {
crate::{cuda_runtime::PinnedVec, recycler::Recycler},
bincode::config::Options,
Expand Down Expand Up @@ -73,7 +73,7 @@ impl PacketBatch {
batch
}

pub fn new_unpinned_with_recycler_data_and_dests<T: Serialize>(
pub fn new_unpinned_with_recycler_data_and_dests<T: solana_packet::Encode>(
recycler: &PacketBatchRecycler,
name: &'static str,
dests_and_data: &[(SocketAddr, T)],
Expand All @@ -85,7 +85,7 @@ impl PacketBatch {

for ((addr, data), packet) in dests_and_data.iter().zip(batch.packets.iter_mut()) {
if !addr.ip().is_unspecified() && addr.port() != 0 {
if let Err(e) = Packet::populate_packet(packet, Some(addr), &data) {
if let Err(e) = Packet::populate_packet(packet, Some(addr), data) {
// TODO: This should never happen. Instead the caller should
// break the payload into smaller messages, and here any errors
// should be propagated.
Expand Down
25 changes: 20 additions & 5 deletions sdk/packet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

#[cfg(feature = "bincode")]
use bincode::{Options, Result};
#[cfg(feature = "frozen-abi")]
use solana_frozen_abi_macro::AbiExample;
#[cfg(feature = "bincode")]
use {
bincode::{Options, Result},
std::io::Write,
};
use {
bitflags::bitflags,
std::{
Expand All @@ -28,6 +31,18 @@ static_assertions::const_assert_eq!(PACKET_DATA_SIZE, 1232);
/// 8 bytes is the size of the fragment header
pub const PACKET_DATA_SIZE: usize = 1280 - 40 - 8;

#[cfg(feature = "bincode")]
pub trait Encode {
fn encode<W: Write>(&self, writer: W) -> Result<()>;
}

#[cfg(feature = "bincode")]
impl<T: ?Sized + serde::Serialize> Encode for T {
fn encode<W: Write>(&self, writer: W) -> Result<()> {
bincode::serialize_into::<W, T>(writer, self)
}
}

bitflags! {
#[repr(C)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
Expand Down Expand Up @@ -159,21 +174,21 @@ impl Packet {
}

#[cfg(feature = "bincode")]
pub fn from_data<T: serde::Serialize>(dest: Option<&SocketAddr>, data: T) -> Result<Self> {
pub fn from_data<T: Encode>(dest: Option<&SocketAddr>, data: T) -> Result<Self> {
let mut packet = Self::default();
Self::populate_packet(&mut packet, dest, &data)?;
Ok(packet)
}

#[cfg(feature = "bincode")]
pub fn populate_packet<T: serde::Serialize>(
pub fn populate_packet<T: Encode>(
&mut self,
dest: Option<&SocketAddr>,
data: &T,
) -> Result<()> {
debug_assert!(!self.meta.discard());
let mut wr = std::io::Cursor::new(self.buffer_mut());
bincode::serialize_into(&mut wr, data)?;
<T as Encode>::encode(data, &mut wr)?;
self.meta.size = wr.position() as usize;
if let Some(dest) = dest {
self.meta.set_socket_addr(dest);
Expand Down

0 comments on commit d131c2b

Please sign in to comment.