Skip to content

Commit

Permalink
Merge pull request ProvableHQ#2425 from ljedrz/perf/tx_speculation_or…
Browse files Browse the repository at this point in the history
…dering2

Use a parallel sort to order txs while speculating
  • Loading branch information
howardwu authored Apr 12, 2024
2 parents 933f12f + 38fe8ff commit 51c3d27
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 4 additions & 3 deletions synthesizer/src/vm/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use super::*;

use ledger_committee::{MAX_DELEGATORS, MIN_DELEGATOR_STAKE, MIN_VALIDATOR_STAKE};
use utilities::cfg_sort_by_cached_key;

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

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

// Return the valid and invalid transactions.
Ok((valid_transactions, aborted_transactions))
Expand Down
24 changes: 24 additions & 0 deletions utilities/src/parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,27 @@ macro_rules! cfg_zip_fold {
result
}};
}

/// Performs an unstable sort
#[macro_export]
macro_rules! cfg_sort_unstable_by {
($self: expr, $closure: expr) => {{
#[cfg(feature = "serial")]
$self.sort_unstable_by($closure);

#[cfg(not(feature = "serial"))]
$self.par_sort_unstable_by($closure);
}};
}

/// Performs a sort that caches the extracted keys
#[macro_export]
macro_rules! cfg_sort_by_cached_key {
($self: expr, $closure: expr) => {{
#[cfg(feature = "serial")]
$self.sort_by_cached_key($closure);

#[cfg(not(feature = "serial"))]
$self.par_sort_by_cached_key($closure);
}};
}

0 comments on commit 51c3d27

Please sign in to comment.