Skip to content

Commit 51c3d27

Browse files
authored
Merge pull request #2425 from ljedrz/perf/tx_speculation_ordering2
Use a parallel sort to order txs while speculating
2 parents 933f12f + 38fe8ff commit 51c3d27

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

synthesizer/src/vm/finalize.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use super::*;
1616

1717
use ledger_committee::{MAX_DELEGATORS, MIN_DELEGATOR_STAKE, MIN_VALIDATOR_STAKE};
18+
use utilities::cfg_sort_by_cached_key;
1819

1920
impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
2021
/// Speculates on the given list of transactions in the VM.
@@ -835,9 +836,9 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
835836
}
836837

837838
// Sort the valid and aborted transactions based on their position in the original list.
838-
let position: IndexMap<_, _> = cfg_iter!(transactions).enumerate().map(|(i, &tx)| (tx.id(), i)).collect();
839-
valid_transactions.sort_by(|a, b| position.get(&a.id()).cmp(&position.get(&b.id())));
840-
aborted_transactions.sort_by(|a, b| position.get(&a.0.id()).cmp(&position.get(&b.0.id())));
839+
let position: IndexSet<_> = transactions.iter().map(|tx| tx.id()).collect();
840+
cfg_sort_by_cached_key!(valid_transactions, |tx| position.get_index_of(&tx.id()));
841+
cfg_sort_by_cached_key!(aborted_transactions, |tx| position.get_index_of(&tx.0.id()));
841842

842843
// Return the valid and invalid transactions.
843844
Ok((valid_transactions, aborted_transactions))

utilities/src/parallel.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,27 @@ macro_rules! cfg_zip_fold {
268268
result
269269
}};
270270
}
271+
272+
/// Performs an unstable sort
273+
#[macro_export]
274+
macro_rules! cfg_sort_unstable_by {
275+
($self: expr, $closure: expr) => {{
276+
#[cfg(feature = "serial")]
277+
$self.sort_unstable_by($closure);
278+
279+
#[cfg(not(feature = "serial"))]
280+
$self.par_sort_unstable_by($closure);
281+
}};
282+
}
283+
284+
/// Performs a sort that caches the extracted keys
285+
#[macro_export]
286+
macro_rules! cfg_sort_by_cached_key {
287+
($self: expr, $closure: expr) => {{
288+
#[cfg(feature = "serial")]
289+
$self.sort_by_cached_key($closure);
290+
291+
#[cfg(not(feature = "serial"))]
292+
$self.par_sort_by_cached_key($closure);
293+
}};
294+
}

0 commit comments

Comments
 (0)