Skip to content

Commit

Permalink
Improve serialization benches (solana-labs#27718)
Browse files Browse the repository at this point in the history
* serialization benches: fix index_in_callee

* serialize benches: bench the whole serialize_parameters()

* Add serialization benches for max num of instruction accounts
  • Loading branch information
alessandrod authored Sep 21, 2022
1 parent 8e46077 commit 1495259
Showing 1 changed file with 57 additions and 27 deletions.
84 changes: 57 additions & 27 deletions programs/bpf_loader/benches/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@
extern crate test;

use {
solana_bpf_loader_program::serialization::{
serialize_parameters_aligned, serialize_parameters_unaligned,
},
solana_bpf_loader_program::serialization::serialize_parameters,
solana_sdk::{
account::{Account, AccountSharedData},
bpf_loader,
bpf_loader, bpf_loader_deprecated,
pubkey::Pubkey,
sysvar::rent::Rent,
transaction_context::{IndexOfAccount, InstructionAccount, TransactionContext},
},
test::Bencher,
};

fn create_inputs() -> TransactionContext {
fn create_inputs(owner: Pubkey, num_instruction_accounts: usize) -> TransactionContext {
let program_id = solana_sdk::pubkey::new_rand();
let transaction_accounts = vec![
(
program_id,
AccountSharedData::from(Account {
lamports: 0,
data: vec![],
owner: bpf_loader::id(),
owner,
executable: true,
rent_epoch: 0,
}),
Expand All @@ -33,7 +32,7 @@ fn create_inputs() -> TransactionContext {
AccountSharedData::from(Account {
lamports: 1,
data: vec![1u8; 100000],
owner: bpf_loader::id(),
owner,
executable: false,
rent_epoch: 100,
}),
Expand All @@ -43,7 +42,7 @@ fn create_inputs() -> TransactionContext {
AccountSharedData::from(Account {
lamports: 2,
data: vec![11u8; 100000],
owner: bpf_loader::id(),
owner,
executable: true,
rent_epoch: 200,
}),
Expand All @@ -53,7 +52,7 @@ fn create_inputs() -> TransactionContext {
AccountSharedData::from(Account {
lamports: 3,
data: vec![],
owner: bpf_loader::id(),
owner,
executable: false,
rent_epoch: 3100,
}),
Expand All @@ -63,7 +62,7 @@ fn create_inputs() -> TransactionContext {
AccountSharedData::from(Account {
lamports: 4,
data: vec![1u8; 100000],
owner: bpf_loader::id(),
owner,
executable: false,
rent_epoch: 100,
}),
Expand All @@ -73,7 +72,7 @@ fn create_inputs() -> TransactionContext {
AccountSharedData::from(Account {
lamports: 5,
data: vec![11u8; 10000],
owner: bpf_loader::id(),
owner,
executable: true,
rent_epoch: 200,
}),
Expand All @@ -83,25 +82,32 @@ fn create_inputs() -> TransactionContext {
AccountSharedData::from(Account {
lamports: 6,
data: vec![],
owner: bpf_loader::id(),
owner,
executable: false,
rent_epoch: 3100,
}),
),
];
let instruction_accounts = [1, 1, 2, 3, 4, 4, 5, 6]
let mut instruction_accounts: Vec<InstructionAccount> = Vec::new();
for (instruction_account_index, index_in_transaction) in [1, 1, 2, 3, 4, 4, 5, 6]
.into_iter()
.cycle()
.take(num_instruction_accounts)
.enumerate()
.map(
|(instruction_account_index, index_in_transaction)| InstructionAccount {
index_in_caller: instruction_account_index as IndexOfAccount,
index_in_transaction,
index_in_callee: instruction_account_index as IndexOfAccount,
is_signer: false,
is_writable: instruction_account_index >= 4,
},
)
.collect::<Vec<_>>();
{
let index_in_callee = instruction_accounts
.iter()
.position(|account| account.index_in_transaction == index_in_transaction)
.unwrap_or(instruction_account_index) as IndexOfAccount;
instruction_accounts.push(InstructionAccount {
index_in_caller: instruction_account_index as IndexOfAccount,
index_in_transaction,
index_in_callee,
is_signer: false,
is_writable: instruction_account_index >= 4,
});
}

let mut transaction_context =
TransactionContext::new(transaction_accounts, Some(Rent::default()), 1, 1);
let instruction_data = vec![1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
Expand All @@ -115,22 +121,46 @@ fn create_inputs() -> TransactionContext {

#[bench]
fn bench_serialize_unaligned(bencher: &mut Bencher) {
let transaction_context = create_inputs();
let transaction_context = create_inputs(bpf_loader_deprecated::id(), 7);
let instruction_context = transaction_context
.get_current_instruction_context()
.unwrap();
bencher.iter(|| {
let _ = serialize_parameters_unaligned(&transaction_context, instruction_context).unwrap();
let _ = serialize_parameters(&transaction_context, instruction_context, true).unwrap();
});
}

#[bench]
fn bench_serialize_aligned(bencher: &mut Bencher) {
let transaction_context = create_inputs();
let transaction_context = create_inputs(bpf_loader::id(), 7);
let instruction_context = transaction_context
.get_current_instruction_context()
.unwrap();

bencher.iter(|| {
let _ = serialize_parameters(&transaction_context, instruction_context, true).unwrap();
});
}

#[bench]
fn bench_serialize_unaligned_max_accounts(bencher: &mut Bencher) {
let transaction_context = create_inputs(bpf_loader_deprecated::id(), 255);
let instruction_context = transaction_context
.get_current_instruction_context()
.unwrap();
bencher.iter(|| {
let _ = serialize_parameters(&transaction_context, instruction_context, true).unwrap();
});
}

#[bench]
fn bench_serialize_aligned_max_accounts(bencher: &mut Bencher) {
let transaction_context = create_inputs(bpf_loader::id(), 255);
let instruction_context = transaction_context
.get_current_instruction_context()
.unwrap();

bencher.iter(|| {
let _ = serialize_parameters_aligned(&transaction_context, instruction_context).unwrap();
let _ = serialize_parameters(&transaction_context, instruction_context, true).unwrap();
});
}

0 comments on commit 1495259

Please sign in to comment.