Skip to content

Commit

Permalink
Add bench tests for ahash vs hash
Browse files Browse the repository at this point in the history
  • Loading branch information
ksolana committed Jul 30, 2024
1 parent 5fa2fb3 commit a1efd3e
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions cost-model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ solana-sdk = { workspace = true }
solana-stake-program = { workspace = true }
solana-system-program = { workspace = true }
solana-vote-program = { workspace = true }
rand = "0.8.5"

[lib]
crate-type = ["lib"]
Expand Down
136 changes: 136 additions & 0 deletions cost-model/benches/block_cost_limit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#![feature(test)]
extern crate test;
use {
ahash::AHashMap,
std::collections::HashMap,
lazy_static::lazy_static,
solana_sdk::{

signer::Signer,
},
solana_sdk::{
address_lookup_table, bpf_loader, bpf_loader_deprecated, bpf_loader_upgradeable,
compute_budget, ed25519_program, loader_v4, pubkey::Pubkey, secp256k1_program,
},
test::Bencher,
};

struct BenchSetup {
pubkeys: [Pubkey; 12],
ahash_map: AHashMap<Pubkey, u64>,
hash_map : HashMap<Pubkey, u64>,
}

const NUM_TRANSACTIONS_PER_ITER: usize = 1024;
const DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT: u32 = 200_000;

fn setup() -> BenchSetup {
let pubkeys : [Pubkey; 12] = [
solana_stake_program::id(),
solana_config_program::id(),
solana_vote_program::id(),
solana_system_program::id(),
compute_budget::id(),
address_lookup_table::program::id(),
bpf_loader_upgradeable::id(),
bpf_loader_deprecated::id(),
bpf_loader::id(),
loader_v4::id(),
secp256k1_program::id(),
ed25519_program::id(),
];

lazy_static! {
/// Number of compute units for each built-in programs
pub static ref ahash_map: AHashMap<Pubkey, u64> = [
(solana_stake_program::id(), solana_stake_program::stake_instruction::DEFAULT_COMPUTE_UNITS),
(solana_config_program::id(), solana_config_program::config_processor::DEFAULT_COMPUTE_UNITS),
(solana_vote_program::id(), solana_vote_program::vote_processor::DEFAULT_COMPUTE_UNITS),
(solana_system_program::id(), solana_system_program::system_processor::DEFAULT_COMPUTE_UNITS),
(compute_budget::id(), solana_compute_budget_program::DEFAULT_COMPUTE_UNITS),
(address_lookup_table::program::id(), solana_address_lookup_table_program::processor::DEFAULT_COMPUTE_UNITS),
(bpf_loader_upgradeable::id(), solana_bpf_loader_program::UPGRADEABLE_LOADER_COMPUTE_UNITS),
(bpf_loader_deprecated::id(), solana_bpf_loader_program::DEPRECATED_LOADER_COMPUTE_UNITS),
(bpf_loader::id(), solana_bpf_loader_program::DEFAULT_LOADER_COMPUTE_UNITS),
(loader_v4::id(), solana_loader_v4_program::DEFAULT_COMPUTE_UNITS),
// Note: These are precompile, run directly in bank during sanitizing;
(secp256k1_program::id(), 0),
(ed25519_program::id(), 0),
]
.iter()
.cloned()
.collect();
}

lazy_static! {
/// Number of compute units for each built-in programs
pub static ref hash_map: AHashMap<Pubkey, u64> = [
(solana_stake_program::id(), solana_stake_program::stake_instruction::DEFAULT_COMPUTE_UNITS),
(solana_config_program::id(), solana_config_program::config_processor::DEFAULT_COMPUTE_UNITS),
(solana_vote_program::id(), solana_vote_program::vote_processor::DEFAULT_COMPUTE_UNITS),
(solana_system_program::id(), solana_system_program::system_processor::DEFAULT_COMPUTE_UNITS),
(compute_budget::id(), solana_compute_budget_program::DEFAULT_COMPUTE_UNITS),
(address_lookup_table::program::id(), solana_address_lookup_table_program::processor::DEFAULT_COMPUTE_UNITS),
(bpf_loader_upgradeable::id(), solana_bpf_loader_program::UPGRADEABLE_LOADER_COMPUTE_UNITS),
(bpf_loader_deprecated::id(), solana_bpf_loader_program::DEPRECATED_LOADER_COMPUTE_UNITS),
(bpf_loader::id(), solana_bpf_loader_program::DEFAULT_LOADER_COMPUTE_UNITS),
(loader_v4::id(), solana_loader_v4_program::DEFAULT_COMPUTE_UNITS),
// Note: These are precompile, run directly in bank during sanitizing;
(secp256k1_program::id(), 0),
(ed25519_program::id(), 0),
]
.iter()
.cloned()
.collect();
}

BenchSetup {
pubkeys,
ahash_map,
hash_map
}
}

#[bench]
fn bench_ahash_find(bencher: &mut Bencher) {
let BenchSetup {
pubkeys,
ahash_map,
hash_map,
} = setup();

bencher.iter(|| {
for t in (0..NUM_TRANSACTIONS_PER_ITER) {
let idx = rand::thread_rng().gen_range(0..pubkeys.len());
let ix_execution_cost =
if let Some(builtin_cost) = ahash_map.get(idx) {
*builtin_cost
} else {
u64::from(DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT)
};
assert!(ix_execution_cost != DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT);
}
});
}

#[bench]
fn bench_hash_find(bencher: &mut Bencher) {
let BenchSetup {
pubkeys,
ahash_map,
hash_map,
} = setup();

bencher.iter(|| {
for t in (0..NUM_TRANSACTIONS_PER_ITER) {
let idx = rand::thread_rng().gen_range(0..pubkeys.len());
let ix_execution_cost =
if let Some(builtin_cost) = hash_map.get(idx) {
*builtin_cost
} else {
u64::from(DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT)
};
assert!(ix_execution_cost != DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT);
}
});
}
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 a1efd3e

Please sign in to comment.