Skip to content

Commit

Permalink
Slightly refactor start_verify_transactions to avoid an unnecessary l…
Browse files Browse the repository at this point in the history
…oop through the vector (solana-labs#25559)

* Small refactoring to remove an unnecessary loop through the vector
  • Loading branch information
ryleung-solana authored May 26, 2022
1 parent 1fda511 commit 178eba4
Showing 1 changed file with 13 additions and 25 deletions.
38 changes: 13 additions & 25 deletions entry/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ use {
std::{
cmp,
ffi::OsStr,
sync::{
atomic::{AtomicUsize, Ordering},
Arc, Mutex, Once,
},
sync::{Arc, Mutex, Once},
thread::{self, JoinHandle},
time::Instant,
},
Expand Down Expand Up @@ -473,48 +470,38 @@ pub fn start_verify_transactions(
let entries = verify_transactions(entries, Arc::new(verify_func));
match entries {
Ok(entries) => {
let num_transactions: usize = entries
let entry_txs: Vec<&SanitizedTransaction> = entries
.iter()
.map(|entry: &EntryType| -> usize {
match entry {
EntryType::Transactions(transactions) => transactions.len(),
EntryType::Tick(_) => 0,
}
.filter_map(|entry_type| match entry_type {
EntryType::Tick(_) => None,
EntryType::Transactions(transactions) => Some(transactions),
})
.sum();
.flatten()
.collect::<Vec<_>>();

if num_transactions == 0 {
if entry_txs.is_empty() {
return Ok(EntrySigVerificationState {
verification_status: EntryVerificationStatus::Success,
entries: Some(entries),
device_verification_data: DeviceSigVerificationData::Cpu(),
gpu_verify_duration_us: 0,
});
}
let entry_txs: Vec<&SanitizedTransaction> = entries
.iter()
.filter_map(|entry_type| match entry_type {
EntryType::Tick(_) => None,
EntryType::Transactions(transactions) => Some(transactions),
})
.flatten()
.collect::<Vec<_>>();
let total_packets = AtomicUsize::new(0);

let mut packet_batches = entry_txs
.par_iter()
.chunks(PACKETS_PER_BATCH)
.map(|slice| {
let vec_size = slice.len();
total_packets.fetch_add(vec_size, Ordering::Relaxed);
let mut packet_batch = PacketBatch::new_with_recycler(
verify_recyclers.packet_recycler.clone(),
vec_size,
"entry-sig-verify",
);
// We use set_len here instead of resize(num_transactions, Packet::default()), to save
// We use set_len here instead of resize(vec_size, Packet::default()), to save
// memory bandwidth and avoid writing a large amount of data that will be overwritten
// soon afterwards. As well, Packet::default() actually leaves the packet data
// uninitialized anyway, so the initilization would simply write junk into
// uninitialized, so the initialization would simply write junk into
// the vector anyway.
unsafe {
packet_batch.set_len(vec_size);
Expand All @@ -537,14 +524,15 @@ pub fn start_verify_transactions(

let tx_offset_recycler = verify_recyclers.tx_offset_recycler;
let out_recycler = verify_recyclers.out_recycler;
let num_packets = entry_txs.len();
let gpu_verify_thread = thread::spawn(move || {
let mut verify_time = Measure::start("sigverify");
sigverify::ed25519_verify(
&mut packet_batches,
&tx_offset_recycler,
&out_recycler,
false,
total_packets.load(Ordering::Relaxed),
num_packets,
);
let verified = packet_batches
.iter()
Expand Down

0 comments on commit 178eba4

Please sign in to comment.