From 172b30dd6a2c61a134cae9270c9edea2f141d38f Mon Sep 17 00:00:00 2001 From: Tao Zhu Date: Wed, 8 May 2024 19:37:31 +0000 Subject: [PATCH 1/5] add bench for cdost-tracker --- cost-model/Cargo.toml | 1 + cost-model/benches/cost_tracker.rs | 81 ++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 cost-model/benches/cost_tracker.rs diff --git a/cost-model/Cargo.toml b/cost-model/Cargo.toml index 4a8b159bbf4cb6..6767b669e29c00 100644 --- a/cost-model/Cargo.toml +++ b/cost-model/Cargo.toml @@ -31,6 +31,7 @@ crate-type = ["lib"] name = "solana_cost_model" [dev-dependencies] +itertools = { workspace = true } solana-logger = { workspace = true } solana-sdk = { workspace = true, features = ["dev-context-only-utils"] } static_assertions = { workspace = true } diff --git a/cost-model/benches/cost_tracker.rs b/cost-model/benches/cost_tracker.rs new file mode 100644 index 00000000000000..c4ea748f4e5ed7 --- /dev/null +++ b/cost-model/benches/cost_tracker.rs @@ -0,0 +1,81 @@ +#![feature(test)] +extern crate test; +use { + itertools::Itertools, + solana_cost_model::{ + cost_tracker::CostTracker, + transaction_cost::{TransactionCost, UsageCostDetails}, + }, + solana_sdk::pubkey::Pubkey, + test::Bencher, +}; + +struct BenchSetup { + cost_tracker: CostTracker, + tx_costs: Vec, +} + +fn setup(num_transactions: usize, contentious_transactions: bool) -> BenchSetup { + let mut cost_tracker = CostTracker::default(); + // set cost_tracker with max limits to stretch testing + cost_tracker.set_limits(u64::MAX, u64::MAX, u64::MAX); + + let max_accounts_per_tx = 128; + let pubkey = Pubkey::new_unique(); + let tx_costs = (0..num_transactions) + .map(|_| { + let mut usage_cost_details = UsageCostDetails::default(); + (0..max_accounts_per_tx).for_each(|_| { + let writable_account_key = if contentious_transactions { + pubkey.clone() + } else { + Pubkey::new_unique() + }; + usage_cost_details + .writable_accounts + .push(writable_account_key) + }); + usage_cost_details.programs_execution_cost = 9999; + TransactionCost::Transaction(usage_cost_details) + }) + .collect_vec(); + + BenchSetup { + cost_tracker, + tx_costs, + } +} + +#[bench] +fn bench_cost_tracker_non_contentious_transaction(bencher: &mut Bencher) { + let BenchSetup { + mut cost_tracker, + tx_costs, + } = setup(1024, false); + + bencher.iter(|| { + for tx_cost in tx_costs.iter() { + if cost_tracker.try_add(&tx_cost).is_err() { + break; + } // stop when hit limits + cost_tracker.update_execution_cost(&tx_cost, 0); // update execution cost down to zero + } + }); +} + +#[bench] +fn bench_cost_tracker_contentious_transaction(bencher: &mut Bencher) { + let BenchSetup { + mut cost_tracker, + tx_costs, + } = setup(1024, true); + + bencher.iter(|| { + for tx_cost in tx_costs.iter() { + if cost_tracker.try_add(&tx_cost).is_err() { + break; + } // stop when hit limits + cost_tracker.update_execution_cost(&tx_cost, 0); // update execution cost down to zero + } + }); +} From 4d256ed6e7b5bd6b01e4095c20544aad1a706c57 Mon Sep 17 00:00:00 2001 From: Tao Zhu Date: Wed, 8 May 2024 19:39:36 +0000 Subject: [PATCH 2/5] use ahash in cost_tracker --- Cargo.lock | 2 ++ cost-model/Cargo.toml | 6 ++++++ cost-model/src/cost_tracker.rs | 7 +++++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c2a5575a8275d0..75f7e1de534fda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6041,6 +6041,8 @@ dependencies = [ name = "solana-cost-model" version = "2.0.0" dependencies = [ + "ahash 0.8.10", + "itertools", "lazy_static", "log", "rustc_version 0.4.0", diff --git a/cost-model/Cargo.toml b/cost-model/Cargo.toml index 6767b669e29c00..7c690b96848b78 100644 --- a/cost-model/Cargo.toml +++ b/cost-model/Cargo.toml @@ -26,6 +26,9 @@ solana-stake-program = { workspace = true } solana-system-program = { workspace = true } solana-vote-program = { workspace = true } +# try ahash, if its faster? +ahash = { workspace = true } + [lib] crate-type = ["lib"] name = "solana_cost_model" @@ -42,3 +45,6 @@ targets = ["x86_64-unknown-linux-gnu"] [build-dependencies] rustc_version = { workspace = true } + +[[bench]] +name = "cost_tracker" diff --git a/cost-model/src/cost_tracker.rs b/cost-model/src/cost_tracker.rs index a1ccfd10d22380..4e8e9126c1836f 100644 --- a/cost-model/src/cost_tracker.rs +++ b/cost-model/src/cost_tracker.rs @@ -62,7 +62,7 @@ pub struct CostTracker { account_cost_limit: u64, block_cost_limit: u64, vote_cost_limit: u64, - cost_by_writable_accounts: HashMap, + cost_by_writable_accounts: HashMap, block_cost: u64, vote_cost: u64, transaction_count: u64, @@ -88,7 +88,10 @@ impl Default for CostTracker { account_cost_limit: MAX_WRITABLE_ACCOUNT_UNITS, block_cost_limit: MAX_BLOCK_UNITS, vote_cost_limit: MAX_VOTE_UNITS, - cost_by_writable_accounts: HashMap::with_capacity(WRITABLE_ACCOUNTS_PER_BLOCK), + cost_by_writable_accounts: HashMap::with_capacity_and_hasher( + WRITABLE_ACCOUNTS_PER_BLOCK, + ahash::RandomState::new(), + ), block_cost: 0, vote_cost: 0, transaction_count: 0, From ab4d1c97390c40cfa21ef00b813b6690a4028cc1 Mon Sep 17 00:00:00 2001 From: Tao Zhu Date: Wed, 8 May 2024 20:01:11 +0000 Subject: [PATCH 3/5] clippy --- cost-model/benches/cost_tracker.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cost-model/benches/cost_tracker.rs b/cost-model/benches/cost_tracker.rs index c4ea748f4e5ed7..ff1f453643b655 100644 --- a/cost-model/benches/cost_tracker.rs +++ b/cost-model/benches/cost_tracker.rs @@ -27,7 +27,7 @@ fn setup(num_transactions: usize, contentious_transactions: bool) -> BenchSetup let mut usage_cost_details = UsageCostDetails::default(); (0..max_accounts_per_tx).for_each(|_| { let writable_account_key = if contentious_transactions { - pubkey.clone() + pubkey } else { Pubkey::new_unique() }; @@ -55,10 +55,10 @@ fn bench_cost_tracker_non_contentious_transaction(bencher: &mut Bencher) { bencher.iter(|| { for tx_cost in tx_costs.iter() { - if cost_tracker.try_add(&tx_cost).is_err() { + if cost_tracker.try_add(tx_cost).is_err() { break; } // stop when hit limits - cost_tracker.update_execution_cost(&tx_cost, 0); // update execution cost down to zero + cost_tracker.update_execution_cost(tx_cost, 0); // update execution cost down to zero } }); } @@ -72,10 +72,10 @@ fn bench_cost_tracker_contentious_transaction(bencher: &mut Bencher) { bencher.iter(|| { for tx_cost in tx_costs.iter() { - if cost_tracker.try_add(&tx_cost).is_err() { + if cost_tracker.try_add(tx_cost).is_err() { break; } // stop when hit limits - cost_tracker.update_execution_cost(&tx_cost, 0); // update execution cost down to zero + cost_tracker.update_execution_cost(tx_cost, 0); // update execution cost down to zero } }); } From 43bf0c9b69b4376e2605db46d6f293a22eedea81 Mon Sep 17 00:00:00 2001 From: Tao Zhu Date: Wed, 8 May 2024 20:05:49 +0000 Subject: [PATCH 4/5] lock files --- programs/sbf/Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/programs/sbf/Cargo.lock b/programs/sbf/Cargo.lock index 14a6a4709fae1a..deb60e9ef486cf 100644 --- a/programs/sbf/Cargo.lock +++ b/programs/sbf/Cargo.lock @@ -5013,6 +5013,7 @@ dependencies = [ name = "solana-cost-model" version = "2.0.0" dependencies = [ + "ahash 0.8.10", "lazy_static", "log", "rustc_version", From 231e2f559787573d1ed8dc5653da0b410fd5b369 Mon Sep 17 00:00:00 2001 From: Tao Zhu Date: Wed, 8 May 2024 21:10:53 +0000 Subject: [PATCH 5/5] sort depdencies --- cost-model/Cargo.toml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cost-model/Cargo.toml b/cost-model/Cargo.toml index 7c690b96848b78..cf13a3377376ed 100644 --- a/cost-model/Cargo.toml +++ b/cost-model/Cargo.toml @@ -10,6 +10,7 @@ license = { workspace = true } edition = { workspace = true } [dependencies] +ahash = { workspace = true } lazy_static = { workspace = true } log = { workspace = true } solana-address-lookup-table-program = { workspace = true } @@ -26,9 +27,6 @@ solana-stake-program = { workspace = true } solana-system-program = { workspace = true } solana-vote-program = { workspace = true } -# try ahash, if its faster? -ahash = { workspace = true } - [lib] crate-type = ["lib"] name = "solana_cost_model"