Skip to content
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

Use ahash in cost tracker #1252

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading