Skip to content

Commit e772f9e

Browse files
committed
perf: use rayon to obtain pending certificates
Signed-off-by: ljedrz <ljedrz@users.noreply.github.com>
1 parent e582dc8 commit e772f9e

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

node/bft/src/helpers/storage.rs

+16-19
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ use snarkvm::{
2222
narwhal::{BatchCertificate, BatchHeader, Transmission, TransmissionID},
2323
},
2424
prelude::{Address, Field, Network, Result, anyhow, bail, ensure},
25+
utilities::{cfg_into_iter, cfg_sorted_by},
2526
};
2627

2728
use indexmap::{IndexMap, IndexSet, map::Entry};
2829
use parking_lot::RwLock;
30+
use rayon::iter::{IntoParallelIterator, ParallelIterator};
2931
use std::{
3032
collections::{HashMap, HashSet},
3133
sync::{
@@ -327,30 +329,25 @@ impl<N: Network> Storage<N> {
327329
/// Returns the certificates that have not yet been included in the ledger.
328330
/// Note that the order of this set is by round and then insertion.
329331
pub(crate) fn get_pending_certificates(&self) -> IndexSet<BatchCertificate<N>> {
330-
let mut pending_certificates = IndexSet::new();
331-
332332
// Obtain the read locks.
333333
let rounds = self.rounds.read();
334334
let certificates = self.certificates.read();
335335

336336
// Iterate over the rounds.
337-
for (_, certificates_for_round) in rounds.clone().sorted_by(|a, _, b, _| a.cmp(b)) {
338-
// Iterate over the certificates for the round.
339-
for (certificate_id, _, _) in certificates_for_round {
340-
// Skip the certificate if it already exists in the ledger.
341-
if self.ledger.contains_certificate(&certificate_id).unwrap_or(false) {
342-
continue;
343-
}
344-
345-
// Add the certificate to the pending certificates.
346-
match certificates.get(&certificate_id).cloned() {
347-
Some(certificate) => pending_certificates.insert(certificate),
348-
None => continue,
349-
};
350-
}
351-
}
352-
353-
pending_certificates
337+
cfg_sorted_by!(rounds.clone(), |a, _, b, _| a.cmp(b))
338+
.flat_map(|(_, certificates_for_round)| {
339+
// Iterate over the certificates for the round.
340+
cfg_into_iter!(certificates_for_round).filter_map(|(certificate_id, _, _)| {
341+
// Skip the certificate if it already exists in the ledger.
342+
if self.ledger.contains_certificate(&certificate_id).unwrap_or(false) {
343+
None
344+
} else {
345+
// Add the certificate to the pending certificates.
346+
certificates.get(&certificate_id).cloned()
347+
}
348+
})
349+
})
350+
.collect()
354351
}
355352

356353
/// Checks the given `batch_header` for validity, returning the missing transmissions from storage.

0 commit comments

Comments
 (0)