Skip to content

Commit

Permalink
accounts: use slice instead of vec (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
cavemanloverboy authored Jun 23, 2024
1 parent 29ebb8c commit 1f0b1a6
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 56 deletions.
14 changes: 6 additions & 8 deletions bencher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ use {
std::path::PathBuf,
};

pub type Bench = (String, Instruction, Vec<(Pubkey, AccountSharedData)>);
pub type Bench<'a> = (String, Instruction, &'a [(Pubkey, AccountSharedData)]);

pub struct MolluskComputeUnitBencher {
benches: Vec<Bench>,
pub struct MolluskComputeUnitBencher<'a> {
benches: Vec<Bench<'a>>,
iterations: u64,
mollusk: Mollusk,
must_pass: bool,
out_dir: PathBuf,
}

impl MolluskComputeUnitBencher {
impl<'a> MolluskComputeUnitBencher<'a> {
pub fn new(mollusk: Mollusk) -> Self {
let mut out_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
out_dir.push("benches");
Expand All @@ -32,7 +32,7 @@ impl MolluskComputeUnitBencher {
}
}

pub fn bench(mut self, bench: Bench) -> Self {
pub fn bench(mut self, bench: Bench<'a>) -> Self {
self.benches.push(bench);
self
}
Expand All @@ -59,9 +59,7 @@ impl MolluskComputeUnitBencher {
let mut results = vec![];

for _ in 0..self.iterations {
let result = self
.mollusk
.process_instruction(&instruction, accounts.clone());
let result = self.mollusk.process_instruction(&instruction, accounts);

match result.program_result {
ProgramResult::Success => (),
Expand Down
38 changes: 7 additions & 31 deletions bencher/tests/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,13 @@ fn test_markdown() {
let mollusk = Mollusk::new(&program_id, "test_program");

MolluskComputeUnitBencher::new(mollusk)
.bench((
String::from("bench0"),
instruction.clone(),
accounts.clone(),
))
.bench((
String::from("bench1"),
instruction.clone(),
accounts.clone(),
))
.bench((
String::from("bench2"),
instruction.clone(),
accounts.clone(),
))
.bench((
String::from("bench3"),
instruction.clone(),
accounts.clone(),
))
.bench((
String::from("bench4"),
instruction.clone(),
accounts.clone(),
))
.bench((
String::from("bench5"),
instruction.clone(),
accounts.clone(),
))
.bench((String::from("bench6"), instruction, accounts))
.bench((String::from("bench0"), instruction.clone(), &accounts))
.bench((String::from("bench1"), instruction.clone(), &accounts))
.bench((String::from("bench2"), instruction.clone(), &accounts))
.bench((String::from("bench3"), instruction.clone(), &accounts))
.bench((String::from("bench4"), instruction.clone(), &accounts))
.bench((String::from("bench5"), instruction.clone(), &accounts))
.bench((String::from("bench6"), instruction, &accounts))
.iterations(100)
.must_pass(true)
.out_dir("../target/benches")
Expand Down
4 changes: 2 additions & 2 deletions harness/benches/ips.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ fn transfer_checked_unchecked(c: &mut Criterion) {
// Bench transfer with post-execution checks
g.bench_function("transfer_checked", |b| {
b.iter(|| {
mollusk.process_and_validate_instruction(&instruction, accounts.clone(), &checks);
mollusk.process_and_validate_instruction(&instruction, &accounts, &checks);
})
});

// Bench transfer without post-execution checks
g.bench_function("transfer_unchecked", |b| {
b.iter(|| {
mollusk.process_instruction(&instruction, accounts.clone());
mollusk.process_instruction(&instruction, &accounts);
})
});

Expand Down
7 changes: 4 additions & 3 deletions harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl Mollusk {
pub fn process_instruction(
&self,
instruction: &Instruction,
accounts: Vec<(Pubkey, AccountSharedData)>,
accounts: &[(Pubkey, AccountSharedData)],
) -> InstructionResult {
let mut compute_units_consumed = 0;
let mut timings = ExecuteTimings::default();
Expand All @@ -177,8 +177,9 @@ impl Mollusk {
.collect::<Vec<_>>();

let transaction_accounts = [(self.program_id, self.program_account.clone())]
.into_iter()
.iter()
.chain(accounts)
.cloned()
.collect::<Vec<_>>();

let mut transaction_context = TransactionContext::new(
Expand Down Expand Up @@ -237,7 +238,7 @@ impl Mollusk {
pub fn process_and_validate_instruction(
&self,
instruction: &Instruction,
accounts: Vec<(Pubkey, AccountSharedData)>,
accounts: &[(Pubkey, AccountSharedData)],
checks: &[Check],
) -> InstructionResult {
let result = self.process_instruction(instruction, accounts);
Expand Down
16 changes: 8 additions & 8 deletions harness/tests/bpf_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn test_write_data() {

mollusk.process_and_validate_instruction(
&account_not_signer_ix,
vec![(key, account.clone())],
&[(key, account.clone())],
&[
Check::err(ProgramError::MissingRequiredSignature),
Check::compute_units(272),
Expand All @@ -58,7 +58,7 @@ fn test_write_data() {

mollusk.process_and_validate_instruction(
&data_too_large_ix,
vec![(key, account.clone())],
&[(key, account.clone())],
&[
Check::err(ProgramError::AccountDataTooSmall),
Check::compute_units(281),
Expand All @@ -69,7 +69,7 @@ fn test_write_data() {
// Success.
mollusk.process_and_validate_instruction(
&instruction,
vec![(key, account.clone())],
&[(key, account.clone())],
&[
Check::success(),
Check::compute_units(350),
Expand Down Expand Up @@ -121,7 +121,7 @@ fn test_transfer() {

mollusk.process_and_validate_instruction(
&payer_not_signer_ix,
vec![
&[
(payer, payer_account.clone()),
(recipient, recipient_account.clone()),
(system_program::id(), system_program_account()),
Expand All @@ -137,7 +137,7 @@ fn test_transfer() {
{
mollusk.process_and_validate_instruction(
&instruction,
vec![
&[
(payer, AccountSharedData::default()),
(recipient, recipient_account.clone()),
(system_program::id(), system_program_account()),
Expand All @@ -154,7 +154,7 @@ fn test_transfer() {
// Success.
mollusk.process_and_validate_instruction(
&instruction,
vec![
&[
(payer, payer_account.clone()),
(recipient, recipient_account.clone()),
(system_program::id(), system_program_account()),
Expand Down Expand Up @@ -200,7 +200,7 @@ fn test_close_account() {

mollusk.process_and_validate_instruction(
&account_not_signer_ix,
vec![
&[
(key, account.clone()),
(incinerator::id(), AccountSharedData::default()),
(system_program::id(), system_program_account()),
Expand All @@ -215,7 +215,7 @@ fn test_close_account() {
// Success.
mollusk.process_and_validate_instruction(
&instruction,
vec![
&[
(key, account.clone()),
(incinerator::id(), AccountSharedData::default()),
(system_program::id(), system_program_account()),
Expand Down
8 changes: 4 additions & 4 deletions harness/tests/system_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn test_transfer() {
let transfer_amount = 42_000u64;

let instruction = system_instruction::transfer(&sender, &recipient, transfer_amount);
let accounts = vec![
let accounts = [
(
sender,
AccountSharedData::new(base_lamports, 0, &system_program::id()),
Expand All @@ -37,7 +37,7 @@ fn test_transfer() {
.build(),
];

Mollusk::default().process_and_validate_instruction(&instruction, accounts, &checks);
Mollusk::default().process_and_validate_instruction(&instruction, &accounts, &checks);
}

#[test]
Expand All @@ -49,7 +49,7 @@ fn test_transfer_bad_owner() {
let transfer_amount = 42_000u64;

let instruction = system_instruction::transfer(&sender, &recipient, transfer_amount);
let accounts = vec![
let accounts = [
(
sender,
AccountSharedData::new(base_lamports, 0, &Pubkey::new_unique()), // <-- Bad owner.
Expand All @@ -64,5 +64,5 @@ fn test_transfer_bad_owner() {
Check::compute_units(DEFAULT_COMPUTE_UNITS),
];

Mollusk::default().process_and_validate_instruction(&instruction, accounts, &checks);
Mollusk::default().process_and_validate_instruction(&instruction, &accounts, &checks);
}

0 comments on commit 1f0b1a6

Please sign in to comment.