diff --git a/synthesizer/src/vm/finalize.rs b/synthesizer/src/vm/finalize.rs index 7b59956b28..46d1598166 100644 --- a/synthesizer/src/vm/finalize.rs +++ b/synthesizer/src/vm/finalize.rs @@ -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> VM { /// Speculates on the given list of transactions in the VM. @@ -835,9 +836,9 @@ impl> VM { } // 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)) diff --git a/utilities/src/parallel.rs b/utilities/src/parallel.rs index 05d95978af..be2520f7ef 100644 --- a/utilities/src/parallel.rs +++ b/utilities/src/parallel.rs @@ -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); + }}; +}