Slightly refactor start_verify_transactions to avoid an unnecessary loop through the vector (#25559)

* Small refactoring to remove an unnecessary loop through the vector
This commit is contained in:
ryleung-solana 2022-05-25 23:27:30 -04:00 committed by GitHub
parent 1fda5110af
commit 178eba4912
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 27 deletions

View File

@ -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,
},
@ -473,24 +470,6 @@ pub fn start_verify_transactions(
let entries = verify_transactions(entries, Arc::new(verify_func));
match entries {
Ok(entries) => {
let num_transactions: usize = entries
.iter()
.map(|entry: &EntryType| -> usize {
match entry {
EntryType::Transactions(transactions) => transactions.len(),
EntryType::Tick(_) => 0,
}
})
.sum();
if num_transactions == 0 {
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 {
@ -499,22 +478,30 @@ pub fn start_verify_transactions(
})
.flatten()
.collect::<Vec<_>>();
let total_packets = AtomicUsize::new(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 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);
@ -537,6 +524,7 @@ 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(
@ -544,7 +532,7 @@ pub fn start_verify_transactions(
&tx_offset_recycler,
&out_recycler,
false,
total_packets.load(Ordering::Relaxed),
num_packets,
);
let verified = packet_batches
.iter()