-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf(katana): optimisations + benchmark setup #2900
Draft
remybar
wants to merge
1
commit into
dojoengine:main
Choose a base branch
from
remybar:katana-core-parallelized
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,130 @@ | ||||||||||||||
use std::collections::BTreeMap; | ||||||||||||||
|
||||||||||||||
use arbitrary::{Arbitrary, Unstructured}; | ||||||||||||||
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion}; | ||||||||||||||
use katana_core::backend::UncommittedBlock; | ||||||||||||||
use katana_primitives::block::{GasPrices, PartialHeader}; | ||||||||||||||
use katana_primitives::da::L1DataAvailabilityMode; | ||||||||||||||
use katana_primitives::receipt::{Receipt, ReceiptWithTxHash}; | ||||||||||||||
use katana_primitives::state::StateUpdates; | ||||||||||||||
use katana_primitives::transaction::{Tx, TxWithHash}; | ||||||||||||||
use katana_primitives::version::CURRENT_STARKNET_VERSION; | ||||||||||||||
use katana_primitives::{ContractAddress, Felt}; | ||||||||||||||
use katana_provider::providers::db::DbProvider; | ||||||||||||||
|
||||||||||||||
const NB_OF_TXS: usize = 20; | ||||||||||||||
const NB_OF_RECEIPTS: usize = 20; | ||||||||||||||
const NB_OF_NONCES: usize = 100; | ||||||||||||||
const NB_OF_STORAGE_KEYS: usize = 100; | ||||||||||||||
const NB_OF_STORAGE_VALUES: usize = 100; | ||||||||||||||
const NB_OF_CLASSES: usize = 100; | ||||||||||||||
const NB_OF_CONTRACTS: usize = 100; | ||||||||||||||
|
||||||||||||||
pub fn commit(block: UncommittedBlock<'_, DbProvider>) { | ||||||||||||||
let _ = block.commit(); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
pub fn commit_parallel(block: UncommittedBlock<'_, DbProvider>) { | ||||||||||||||
let _ = block.commit_parallel(); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[inline(always)] | ||||||||||||||
pub fn random_array(size: usize) -> Vec<u8> { | ||||||||||||||
(0..size).map(|_| rand::random::<u8>()).collect() | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[inline(always)] | ||||||||||||||
pub fn random_felt() -> Felt { | ||||||||||||||
Felt::arbitrary(&mut Unstructured::new(&random_array(Felt::size_hint(0).0))).unwrap() | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[inline(always)] | ||||||||||||||
pub fn random_tx() -> Tx { | ||||||||||||||
Tx::arbitrary(&mut Unstructured::new(&random_array(Tx::size_hint(0).0))).unwrap() | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[inline(always)] | ||||||||||||||
pub fn random_tx_with_hash() -> TxWithHash { | ||||||||||||||
TxWithHash { hash: random_felt(), transaction: random_tx() } | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[inline(always)] | ||||||||||||||
pub fn random_receipt() -> Receipt { | ||||||||||||||
Receipt::arbitrary(&mut Unstructured::new(&random_array(Receipt::size_hint(0).0))).unwrap() | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[inline(always)] | ||||||||||||||
pub fn random_receipt_with_hash() -> ReceiptWithTxHash { | ||||||||||||||
ReceiptWithTxHash { tx_hash: random_felt(), receipt: random_receipt() } | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[inline(always)] | ||||||||||||||
pub fn random_felt_to_felt_map(size: usize) -> BTreeMap<Felt, Felt> { | ||||||||||||||
(0..size).map(|_| (random_felt(), random_felt())).collect() | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[inline(always)] | ||||||||||||||
pub fn random_address_to_felt_map(size: usize) -> BTreeMap<ContractAddress, Felt> { | ||||||||||||||
(0..size).map(|_| (ContractAddress::new(random_felt()), random_felt())).collect() | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
pub fn commit_benchmark(c: &mut Criterion) { | ||||||||||||||
let provider = DbProvider::new_ephemeral(); | ||||||||||||||
|
||||||||||||||
let gas_prices = GasPrices { eth: 100 * u128::pow(10, 9), strk: 100 * u128::pow(10, 9) }; | ||||||||||||||
let sequencer_address = ContractAddress(1u64.into()); | ||||||||||||||
|
||||||||||||||
let header = PartialHeader { | ||||||||||||||
protocol_version: CURRENT_STARKNET_VERSION, | ||||||||||||||
number: 1, | ||||||||||||||
timestamp: 100, | ||||||||||||||
sequencer_address, | ||||||||||||||
parent_hash: 123u64.into(), | ||||||||||||||
l1_gas_prices: gas_prices.clone(), | ||||||||||||||
l1_data_gas_prices: gas_prices.clone(), | ||||||||||||||
l1_da_mode: L1DataAvailabilityMode::Calldata, | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
let transactions: Vec<TxWithHash> = (0..NB_OF_TXS).map(|_| random_tx_with_hash()).collect(); | ||||||||||||||
let receipts: Vec<ReceiptWithTxHash> = | ||||||||||||||
(0..NB_OF_RECEIPTS).map(|_| random_receipt_with_hash()).collect(); | ||||||||||||||
|
||||||||||||||
let nonce_updates: BTreeMap<ContractAddress, Felt> = | ||||||||||||||
(0..NB_OF_NONCES).map(|_| (ContractAddress::new(random_felt()), random_felt())).collect(); | ||||||||||||||
|
||||||||||||||
let storage_updates: BTreeMap<ContractAddress, BTreeMap<Felt, Felt>> = (0..NB_OF_STORAGE_KEYS) | ||||||||||||||
.map(|_| { | ||||||||||||||
(ContractAddress::new(random_felt()), random_felt_to_felt_map(NB_OF_STORAGE_VALUES)) | ||||||||||||||
}) | ||||||||||||||
.collect(); | ||||||||||||||
|
||||||||||||||
let declared_classes: BTreeMap<Felt, Felt> = random_felt_to_felt_map(NB_OF_CLASSES); | ||||||||||||||
let deployed_contracts: BTreeMap<ContractAddress, Felt> = | ||||||||||||||
random_address_to_felt_map(NB_OF_CONTRACTS); | ||||||||||||||
|
||||||||||||||
let state_updates = StateUpdates { | ||||||||||||||
nonce_updates, | ||||||||||||||
storage_updates, | ||||||||||||||
declared_classes, | ||||||||||||||
deployed_contracts, | ||||||||||||||
..Default::default() | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
let block = | ||||||||||||||
UncommittedBlock::new(header, transactions, receipts.as_slice(), &state_updates, provider); | ||||||||||||||
|
||||||||||||||
c.bench_function("commit", |b| { | ||||||||||||||
b.iter_batched(|| block.clone(), |input| commit(black_box(input)), BatchSize::SmallInput); | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
c.bench_function("commit_parallel", |b| { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
|
||||||||||||||
b.iter_batched( | ||||||||||||||
|| block.clone(), | ||||||||||||||
|input| commit_parallel(black_box(input)), | ||||||||||||||
BatchSize::SmallInput, | ||||||||||||||
); | ||||||||||||||
}); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
criterion_group!(benches, commit_benchmark); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: