Skip to content

Commit

Permalink
WIP: parallelize muhash calculation in utxo receive
Browse files Browse the repository at this point in the history
MuHash is additive, so have each thread process a part of the current chunk, then combine results
  • Loading branch information
coderofstuff committed Sep 29, 2024
1 parent 180114e commit 7243cfd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ kaspa-txscript.workspace = true
kaspa-txscript-errors.workspace = true
kaspa-utils.workspace = true
log.workspace = true
num_cpus.workspace = true
once_cell.workspace = true
parking_lot.workspace = true
rayon.workspace = true
Expand Down
21 changes: 19 additions & 2 deletions consensus/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pub mod services;
pub mod storage;
pub mod test_consensus;

use rayon::prelude::*;

#[cfg(feature = "devnet-prealloc")]
mod utxo_set_override;

Expand Down Expand Up @@ -771,8 +773,23 @@ impl ConsensusApi for Consensus {
fn append_imported_pruning_point_utxos(&self, utxoset_chunk: &[(TransactionOutpoint, UtxoEntry)], current_multiset: &mut MuHash) {
let mut pruning_utxoset_write = self.pruning_utxoset_stores.write();
pruning_utxoset_write.utxo_set.write_many(utxoset_chunk).unwrap();
for (outpoint, entry) in utxoset_chunk {
current_multiset.add_utxo(outpoint, entry);

// TODO: Get this from config instead
let num_threads = num_cpus::get();
// Parallelize processing
let inner_multisets: Vec<MuHash> = utxoset_chunk
.par_chunks(utxoset_chunk.len() / num_threads)
.map(|chunk| {
let mut inner_multiset = MuHash::new();
for (outpoint, entry) in chunk {
inner_multiset.add_utxo(outpoint, entry);
}
inner_multiset
})
.collect();

for inner_multiset in inner_multisets {
current_multiset.combine(&inner_multiset);
}
}

Expand Down

0 comments on commit 7243cfd

Please sign in to comment.