Skip to content

Commit

Permalink
Use ahash in cost tracker (#1252)
Browse files Browse the repository at this point in the history
* add bench for cost-tracker

* use `ahash` in cost_tracker
  • Loading branch information
tao-stones authored May 9, 2024
1 parent 5a9906e commit 8a55087
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions cost-model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -31,6 +32,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 }
Expand All @@ -41,3 +43,6 @@ targets = ["x86_64-unknown-linux-gnu"]

[build-dependencies]
rustc_version = { workspace = true }

[[bench]]
name = "cost_tracker"
81 changes: 81 additions & 0 deletions cost-model/benches/cost_tracker.rs
Original file line number Diff line number Diff line change
@@ -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<TransactionCost>,
}

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
} 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
}
});
}
7 changes: 5 additions & 2 deletions cost-model/src/cost_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub struct CostTracker {
account_cost_limit: u64,
block_cost_limit: u64,
vote_cost_limit: u64,
cost_by_writable_accounts: HashMap<Pubkey, u64>,
cost_by_writable_accounts: HashMap<Pubkey, u64, ahash::RandomState>,
block_cost: u64,
vote_cost: u64,
transaction_count: u64,
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions programs/sbf/Cargo.lock

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

0 comments on commit 8a55087

Please sign in to comment.