From b1d5bf30d277c9cdee94d242e6072fd30bd27364 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 29 Jan 2021 13:52:33 +0900 Subject: [PATCH] Remove potentially too costly Packets::default() (#14821) (#14915) * Remove potentially too costly Packets::default() * Fix test... * Restore Packets::default() * Restore Packets::default() more (cherry picked from commit d6873b82ab61071eb87f4c18b2f40cd24d44bbc3) Co-authored-by: Ryo Onodera --- core/src/banking_stage.rs | 8 ++++---- core/src/cluster_info_vote_listener.rs | 2 +- perf/benches/sigverify.rs | 6 +++--- perf/src/packet.rs | 10 ++-------- streamer/src/packet.rs | 2 +- 5 files changed, 11 insertions(+), 17 deletions(-) diff --git a/core/src/banking_stage.rs b/core/src/banking_stage.rs index 15386c2a2a..a7d80c607f 100644 --- a/core/src/banking_stage.rs +++ b/core/src/banking_stage.rs @@ -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}, @@ -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); @@ -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])) @@ -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])) diff --git a/core/src/cluster_info_vote_listener.rs b/core/src/cluster_info_vote_listener.rs index d03956690c..cd6fe62401 100644 --- a/core/src/cluster_info_vote_listener.rs +++ b/core/src/cluster_info_vote_listener.rs @@ -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); } diff --git a/perf/benches/sigverify.rs b/perf/benches/sigverify.rs index c6fb568471..f1f41ced97 100644 --- a/perf/benches/sigverify.rs +++ b/perf/benches/sigverify.rs @@ -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; @@ -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::>()); + let batches = to_packets_chunked(&std::iter::repeat(tx).take(128).collect::>(), 128); let recycler = Recycler::default(); let recycler_out = Recycler::default(); @@ -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::>()); + let batches = to_packets_chunked(&std::iter::repeat(tx).take(1024).collect::>(), 1024); let recycler = Recycler::default(); // verify packets diff --git a/perf/src/packet.rs b/perf/src/packet.rs index 1c4210c441..b29a8201bb 100644 --- a/perf/src/packet.rs +++ b/perf/src/packet.rs @@ -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, } -//auto derive doesn't support large arrays -impl Default for Packets { - fn default() -> Packets { - Self::with_capacity(NUM_RCVMMSGS) - } -} - pub type PacketsRecycler = Recycler>; impl Packets { @@ -75,6 +68,7 @@ pub fn to_packets_chunked(xs: &[T], chunks: usize) -> Vec out } +#[cfg(test)] pub fn to_packets(xs: &[T]) -> Vec { to_packets_chunked(xs, NUM_PACKETS) } diff --git a/streamer/src/packet.rs b/streamer/src/packet.rs index 1fda96834c..83202aa0ee 100644 --- a/streamer/src/packet.rs +++ b/streamer/src/packet.rs @@ -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, };