From 32aab82e3242274a61f36ce21c621e77b1adf746 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Fri, 12 Oct 2018 21:16:51 -0600 Subject: [PATCH] Don't allocate to see if transactions will fit in a blob --- src/entry.rs | 15 +++++++-------- src/ledger.rs | 2 +- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/entry.rs b/src/entry.rs index 21f3c67e77..96db77170f 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -8,6 +8,7 @@ use packet::{SharedBlob, BLOB_DATA_SIZE}; use poh::Poh; use solana_program_interface::pubkey::Pubkey; use std::io::Cursor; +use std::mem::size_of; use std::net::SocketAddr; use std::sync::mpsc::{Receiver, Sender}; use transaction::Transaction; @@ -96,13 +97,11 @@ impl Entry { blob } - pub fn will_fit(transactions: Vec) -> bool { - serialized_size(&Entry { - num_hashes: 0, - id: Hash::default(), - transactions, - }).unwrap() - <= BLOB_DATA_SIZE as u64 + pub fn will_fit(transactions: &[Transaction]) -> bool { + let txs_size = serialized_size(transactions).unwrap() as usize; + + // Estimate serialized_size of Entry without creating an Entry. + size_of::() + size_of::() + txs_size <= BLOB_DATA_SIZE } pub fn num_will_fit(transactions: &[Transaction]) -> usize { @@ -122,7 +121,7 @@ impl Entry { next, transactions.len() ); - if Entry::will_fit(transactions[..num].to_vec()) { + if Self::will_fit(&transactions[..num]) { next = (upper + num) / 2; lower = num; debug!("num {} fits, maybe too well? trying {}", num, next); diff --git a/src/ledger.rs b/src/ledger.rs index 1a89b853f2..c821f6d804 100644 --- a/src/ledger.rs +++ b/src/ledger.rs @@ -549,7 +549,7 @@ pub fn next_entries_mut( next, transactions.len() ); - if Entry::will_fit(transactions[chunk_start..chunk_end].to_vec()) { + if Entry::will_fit(&transactions[chunk_start..chunk_end]) { next = (upper + chunk_end) / 2; lower = chunk_end; debug!(