From 14952590bde0c62b2173f302f0e33c1059af0af1 Mon Sep 17 00:00:00 2001 From: Alessandro Decina Date: Wed, 21 Sep 2022 20:47:36 +0100 Subject: [PATCH] Improve serialization benches (#27718) * serialization benches: fix index_in_callee * serialize benches: bench the whole serialize_parameters() * Add serialization benches for max num of instruction accounts --- programs/bpf_loader/benches/serialization.rs | 84 +++++++++++++------- 1 file changed, 57 insertions(+), 27 deletions(-) diff --git a/programs/bpf_loader/benches/serialization.rs b/programs/bpf_loader/benches/serialization.rs index d1b2d8689c96e7..e14f55476ca542 100644 --- a/programs/bpf_loader/benches/serialization.rs +++ b/programs/bpf_loader/benches/serialization.rs @@ -3,19 +3,18 @@ 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![ ( @@ -23,7 +22,7 @@ fn create_inputs() -> TransactionContext { AccountSharedData::from(Account { lamports: 0, data: vec![], - owner: bpf_loader::id(), + owner, executable: true, rent_epoch: 0, }), @@ -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, }), @@ -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, }), @@ -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, }), @@ -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, }), @@ -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, }), @@ -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 = 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::>(); + { + 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]; @@ -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(); }); }