Skip to content

Commit

Permalink
Optimized Rothschild performance on high TPS (kaspanet#371)
Browse files Browse the repository at this point in the history
* Add rothschild tx benchmark

So we can compare the performance difference between serial generation and parallel generation

* Optimize high TPS generation

1. Parallelize TX generation - hashing is a bottleneck so generating TXs seriallize will slow
the entire loop down significantly
2. Optimize UTXO selection - previously it was running at O(UTXO) due to filtering logic

* Move criterion to dev-dependency

* Some more rothschild improvements

* Fix from_fmillis typo

* Add flags

* Parallelize txid hashing in simpa

* Moving ClientPool to grpc/client

* Fix rothschild sending less than 100 txs

Due to integer divide, when tps is < 100, we end up creating no tx

* Remove obsolete temp init comment

---------

Co-authored-by: Ori Newman <orinewman1@gmail.com>
  • Loading branch information
2 people authored and D-Stacks committed Jan 23, 2024
1 parent 2082253 commit 6d93644
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 68 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

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

23 changes: 13 additions & 10 deletions consensus/core/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,22 @@ impl Transaction {
gas: u64,
payload: Vec<u8>,
) -> Self {
let mut tx = Self {
version,
inputs,
outputs,
lock_time,
subnetwork_id,
gas,
payload,
id: Default::default(), // Temp init before the finalize below
};
let mut tx = Self::new_non_finalized(version, inputs, outputs, lock_time, subnetwork_id, gas, payload);
tx.finalize();
tx
}

pub fn new_non_finalized(
version: u16,
inputs: Vec<TransactionInput>,
outputs: Vec<TransactionOutput>,
lock_time: u64,
subnetwork_id: SubnetworkId,
gas: u64,
payload: Vec<u8>,
) -> Self {
Self { version, inputs, outputs, lock_time, subnetwork_id, gas, payload, id: Default::default() }
}
}

impl Transaction {
Expand Down
10 changes: 10 additions & 0 deletions rothschild/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@ kaspa-rpc-core.workspace = true
kaspa-addresses.workspace = true
kaspa-txscript.workspace = true
kaspa-utils.workspace = true
async-channel.workspace = true
parking_lot.workspace = true

clap.workspace = true
faster-hex.workspace = true
itertools.workspace = true
log.workspace = true
rayon.workspace = true
secp256k1 = { workspace = true, features = ["global-context", "rand-std"] }
tokio = { workspace = true, features = ["rt", "macros", "rt-multi-thread"] }

[dev-dependencies]
criterion.workspace = true

[[bench]]
name = "bench"
harness = false
47 changes: 47 additions & 0 deletions rothschild/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use criterion::{criterion_group, criterion_main, Criterion};
use rayon::prelude::*;

use kaspa_consensus_core::{
constants::TX_VERSION,
subnets::SUBNETWORK_ID_NATIVE,
tx::{ScriptPublicKey, Transaction, TransactionInput, TransactionOutpoint, TransactionOutput},
Hash,
};

fn constuct_tx() -> Transaction {
let inputs = vec![TransactionInput {
previous_outpoint: TransactionOutpoint { transaction_id: Hash::from_bytes([0xFF; 32]), index: 0 },
signature_script: vec![],
sequence: 0,
sig_op_count: 1,
}];
let outputs = vec![TransactionOutput { value: 10000, script_public_key: ScriptPublicKey::from_vec(0, vec![0xff; 35]) }];
Transaction::new(TX_VERSION, inputs, outputs, 0, SUBNETWORK_ID_NATIVE, 0, vec![])
}

fn construct_txs_serially() {
let _ = (0..10000)
.map(|_| {
constuct_tx();
})
.collect::<Vec<_>>();
}

fn construct_txs_parallel() {
let _ = (0..10000)
.into_par_iter()
.map(|_| {
constuct_tx();
})
.collect::<Vec<_>>();
}

pub fn bench_compare_tx_generation(c: &mut Criterion) {
let mut group = c.benchmark_group("compare txs");
group.bench_function("Transaction::SerialCreation", |b| b.iter(construct_txs_serially));
group.bench_function("Transaction::ParallelCreation", |b| b.iter(construct_txs_parallel));
group.finish();
}

criterion_group!(benches, bench_compare_tx_generation);
criterion_main!(benches);
Loading

0 comments on commit 6d93644

Please sign in to comment.