Don't allocate to see if transactions will fit in a blob

This commit is contained in:
Greg Fitzgerald 2018-10-12 21:16:51 -06:00
parent 6aaa350145
commit 32aab82e32
2 changed files with 8 additions and 9 deletions

View File

@ -8,6 +8,7 @@ use packet::{SharedBlob, BLOB_DATA_SIZE};
use poh::Poh; use poh::Poh;
use solana_program_interface::pubkey::Pubkey; use solana_program_interface::pubkey::Pubkey;
use std::io::Cursor; use std::io::Cursor;
use std::mem::size_of;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::mpsc::{Receiver, Sender}; use std::sync::mpsc::{Receiver, Sender};
use transaction::Transaction; use transaction::Transaction;
@ -96,13 +97,11 @@ impl Entry {
blob blob
} }
pub fn will_fit(transactions: Vec<Transaction>) -> bool { pub fn will_fit(transactions: &[Transaction]) -> bool {
serialized_size(&Entry { let txs_size = serialized_size(transactions).unwrap() as usize;
num_hashes: 0,
id: Hash::default(), // Estimate serialized_size of Entry without creating an Entry.
transactions, size_of::<u64>() + size_of::<Hash>() + txs_size <= BLOB_DATA_SIZE
}).unwrap()
<= BLOB_DATA_SIZE as u64
} }
pub fn num_will_fit(transactions: &[Transaction]) -> usize { pub fn num_will_fit(transactions: &[Transaction]) -> usize {
@ -122,7 +121,7 @@ impl Entry {
next, next,
transactions.len() transactions.len()
); );
if Entry::will_fit(transactions[..num].to_vec()) { if Self::will_fit(&transactions[..num]) {
next = (upper + num) / 2; next = (upper + num) / 2;
lower = num; lower = num;
debug!("num {} fits, maybe too well? trying {}", num, next); debug!("num {} fits, maybe too well? trying {}", num, next);

View File

@ -549,7 +549,7 @@ pub fn next_entries_mut(
next, next,
transactions.len() 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; next = (upper + chunk_end) / 2;
lower = chunk_end; lower = chunk_end;
debug!( debug!(