Skip to content

Commit

Permalink
Remove potentially too costly Packets::default() (solana-labs#14821)
Browse files Browse the repository at this point in the history
* Remove potentially too costly Packets::default()

* Fix test...

* Restore Packets::default()

* Restore Packets::default() more
  • Loading branch information
ryoqun authored Jan 29, 2021
1 parent 8993ac0 commit d6873b8
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 17 deletions.
8 changes: 4 additions & 4 deletions core/src/banking_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ mod tests {
genesis_utils::{create_genesis_config, GenesisConfigInfo},
get_tmp_ledger_path,
};
use solana_perf::packet::to_packets;
use solana_perf::packet::to_packets_chunked;
use solana_sdk::{
instruction::InstructionError,
signature::{Keypair, Signer},
Expand Down Expand Up @@ -1292,7 +1292,7 @@ mod tests {
let tx_anf = system_transaction::transfer(&keypair, &to3, 1, start_hash);

// send 'em over
let packets = to_packets(&[tx_no_ver, tx_anf, tx]);
let packets = to_packets_chunked(&[tx_no_ver, tx_anf, tx], 3);

// glad they all fit
assert_eq!(packets.len(), 1);
Expand Down Expand Up @@ -1368,7 +1368,7 @@ mod tests {
let tx =
system_transaction::transfer(&mint_keypair, &alice.pubkey(), 2, genesis_config.hash());

let packets = to_packets(&[tx]);
let packets = to_packets_chunked(&[tx], 1);
let packets = packets
.into_iter()
.map(|packets| (packets, vec![1u8]))
Expand All @@ -1379,7 +1379,7 @@ mod tests {
// Process a second batch that uses the same from account, so conflicts with above TX
let tx =
system_transaction::transfer(&mint_keypair, &alice.pubkey(), 1, genesis_config.hash());
let packets = to_packets(&[tx]);
let packets = to_packets_chunked(&[tx], 1);
let packets = packets
.into_iter()
.map(|packets| (packets, vec![1u8]))
Expand Down
2 changes: 1 addition & 1 deletion core/src/cluster_info_vote_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ mod tests {
use bincode::serialized_size;
info!("max vote size {}", serialized_size(&vote_tx).unwrap());

let msgs = packet::to_packets(&[vote_tx]); // panics if won't fit
let msgs = packet::to_packets_chunked(&[vote_tx], 1); // panics if won't fit

assert_eq!(msgs.len(), 1);
}
Expand Down
6 changes: 3 additions & 3 deletions perf/benches/sigverify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

extern crate test;

use solana_perf::packet::to_packets;
use solana_perf::packet::to_packets_chunked;
use solana_perf::recycler::Recycler;
use solana_perf::sigverify;
use solana_perf::test_tx::test_tx;
Expand All @@ -13,7 +13,7 @@ fn bench_sigverify(bencher: &mut Bencher) {
let tx = test_tx();

// generate packet vector
let batches = to_packets(&std::iter::repeat(tx).take(128).collect::<Vec<_>>());
let batches = to_packets_chunked(&std::iter::repeat(tx).take(128).collect::<Vec<_>>(), 128);

let recycler = Recycler::default();
let recycler_out = Recycler::default();
Expand All @@ -28,7 +28,7 @@ fn bench_get_offsets(bencher: &mut Bencher) {
let tx = test_tx();

// generate packet vector
let batches = to_packets(&std::iter::repeat(tx).take(1024).collect::<Vec<_>>());
let batches = to_packets_chunked(&std::iter::repeat(tx).take(1024).collect::<Vec<_>>(), 1024);

let recycler = Recycler::default();
// verify packets
Expand Down
10 changes: 2 additions & 8 deletions perf/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,11 @@ pub const PACKETS_PER_BATCH: usize = 256;
pub const NUM_RCVMMSGS: usize = 128;
pub const PACKETS_BATCH_SIZE: usize = PACKETS_PER_BATCH * PACKET_DATA_SIZE;

#[derive(Debug, Clone)]
#[derive(Debug, Default, Clone)]
pub struct Packets {
pub packets: PinnedVec<Packet>,
}

//auto derive doesn't support large arrays
impl Default for Packets {
fn default() -> Packets {
Self::with_capacity(NUM_RCVMMSGS)
}
}

pub type PacketsRecycler = Recycler<PinnedVec<Packet>>;

impl Packets {
Expand Down Expand Up @@ -75,6 +68,7 @@ pub fn to_packets_chunked<T: Serialize>(xs: &[T], chunks: usize) -> Vec<Packets>
out
}

#[cfg(test)]
pub fn to_packets<T: Serialize>(xs: &[T]) -> Vec<Packets> {
to_packets_chunked(xs, NUM_PACKETS)
}
Expand Down
2 changes: 1 addition & 1 deletion streamer/src/packet.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! The `packet` module defines data structures and methods to pull data from the network.
use crate::recvmmsg::{recv_mmsg, NUM_RCVMMSGS};
pub use solana_perf::packet::{
limited_deserialize, to_packets, to_packets_chunked, Packets, PacketsRecycler, NUM_PACKETS,
limited_deserialize, to_packets_chunked, Packets, PacketsRecycler, NUM_PACKETS,
PACKETS_BATCH_SIZE, PACKETS_PER_BATCH,
};

Expand Down

0 comments on commit d6873b8

Please sign in to comment.