forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 445
Benchmarks for address-lookup-table program instructions #4406
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
Merged
tao-stones
merged 1 commit into
anza-xyz:master
from
tao-stones:test-add-address-lookup-table-program-benches
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
267 changes: 267 additions & 0 deletions
267
programs/address-lookup-table-tests/benches/address_lookup_table.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,267 @@ | ||
use { | ||
criterion::{black_box, criterion_group, criterion_main, Criterion}, | ||
solana_account::{create_account_shared_data_for_test, AccountSharedData}, | ||
solana_instruction::AccountMeta, | ||
solana_program_runtime::{ | ||
invoke_context::mock_process_instruction, loaded_programs::ProgramCacheEntry, | ||
}, | ||
solana_pubkey::Pubkey, | ||
solana_sdk::{ | ||
address_lookup_table::instruction::{derive_lookup_table_address, ProgramInstruction}, | ||
clock::{Clock, Slot}, | ||
hash::Hash, | ||
rent::Rent, | ||
sysvar::{clock, slot_hashes::SlotHashes}, | ||
}, | ||
solana_sdk_ids::{ | ||
address_lookup_table, system_program, | ||
sysvar::{rent, slot_hashes}, | ||
}, | ||
std::sync::Arc, | ||
}; | ||
|
||
const ACCOUNT_BALANCE: u64 = u64::MAX / 4; | ||
|
||
#[derive(Default)] | ||
struct TestSetup { | ||
authority_address: Pubkey, | ||
payer_address: Pubkey, | ||
lookup_table_address: Pubkey, | ||
bump_seed: u8, | ||
recent_slot: Slot, | ||
transaction_accounts: Vec<(Pubkey, AccountSharedData)>, | ||
instruction_accounts: Vec<AccountMeta>, | ||
instruction_data: Vec<u8>, | ||
} | ||
|
||
impl TestSetup { | ||
fn new() -> Self { | ||
let authority_address = Pubkey::new_unique(); | ||
let payer_address = Pubkey::new_unique(); | ||
let recent_slot = 1; | ||
let mut slot_hashes = SlotHashes::default(); | ||
slot_hashes.add(recent_slot, Hash::new_unique()); | ||
|
||
let (lookup_table_address, bump_seed) = | ||
derive_lookup_table_address(&authority_address, recent_slot); | ||
|
||
let transaction_accounts: Vec<(Pubkey, AccountSharedData)> = vec![ | ||
// lookup table account is initially owned by system program, it later | ||
// allocate() by native_invoke System program during create_lookup_table | ||
( | ||
lookup_table_address, | ||
AccountSharedData::new(0, 0, &system_program::id()), | ||
), | ||
( | ||
authority_address, | ||
AccountSharedData::new(ACCOUNT_BALANCE, 0, &Pubkey::new_unique()), | ||
), | ||
( | ||
payer_address, | ||
AccountSharedData::new(ACCOUNT_BALANCE, 0, &system_program::id()), | ||
), | ||
( | ||
system_program::id(), | ||
AccountSharedData::new(0, 0, &system_program::id()), | ||
), | ||
( | ||
slot_hashes::id(), | ||
create_account_shared_data_for_test(&slot_hashes), | ||
), | ||
( | ||
rent::id(), | ||
create_account_shared_data_for_test(&Rent::default()), | ||
), | ||
( | ||
clock::id(), | ||
create_account_shared_data_for_test(&Clock::default()), | ||
), | ||
]; | ||
|
||
Self { | ||
authority_address, | ||
payer_address, | ||
lookup_table_address, | ||
bump_seed, | ||
recent_slot, | ||
transaction_accounts, | ||
..TestSetup::default() | ||
} | ||
} | ||
|
||
fn prep_create_lookup_table(&mut self) { | ||
self.instruction_accounts = vec![ | ||
AccountMeta::new(self.lookup_table_address, false), | ||
AccountMeta::new_readonly(self.authority_address, false), | ||
AccountMeta::new(self.payer_address, true), | ||
AccountMeta::new_readonly(system_program::id(), false), | ||
]; | ||
|
||
self.instruction_data = bincode::serialize(&ProgramInstruction::CreateLookupTable { | ||
recent_slot: self.recent_slot, | ||
bump_seed: self.bump_seed, | ||
}) | ||
.unwrap(); | ||
} | ||
|
||
fn exec_create_lookup_table(&mut self) { | ||
self.prep_create_lookup_table(); | ||
let accounts = self.run(); | ||
|
||
let lookup_table_account = accounts[0].clone(); | ||
self.transaction_accounts[0] = (self.lookup_table_address, lookup_table_account); | ||
} | ||
|
||
fn prep_extend_lookup_table(&mut self) { | ||
self.instruction_accounts = vec![ | ||
AccountMeta::new(self.lookup_table_address, false), | ||
AccountMeta::new_readonly(self.authority_address, true), | ||
AccountMeta::new(self.payer_address, true), | ||
AccountMeta::new_readonly(system_program::id(), false), | ||
]; | ||
|
||
// extend reasonable number of addresses at a time, so bench loop won't | ||
// add more than LOOKUP_TABLE_MAX_ADDRESSES addresses to lookup_table | ||
let new_addresses: Vec<_> = (0..16).map(|_| Pubkey::new_unique()).collect(); | ||
self.instruction_data = | ||
bincode::serialize(&ProgramInstruction::ExtendLookupTable { new_addresses }).unwrap(); | ||
} | ||
|
||
fn exec_extend_lookup_table(&mut self) { | ||
self.prep_extend_lookup_table(); | ||
let accounts = self.run(); | ||
|
||
let lookup_table_account = accounts[0].clone(); | ||
self.transaction_accounts[0] = (self.lookup_table_address, lookup_table_account); | ||
} | ||
|
||
fn prep_freeze_lookup_table(&mut self) { | ||
self.instruction_accounts = vec![ | ||
AccountMeta::new(self.lookup_table_address, false), | ||
AccountMeta::new(self.authority_address, true), | ||
]; | ||
|
||
self.instruction_data = bincode::serialize(&ProgramInstruction::FreezeLookupTable).unwrap(); | ||
} | ||
|
||
fn prep_deactivate_lookup_table(&mut self) { | ||
self.instruction_accounts = vec![ | ||
AccountMeta::new(self.lookup_table_address, false), | ||
AccountMeta::new(self.authority_address, true), | ||
]; | ||
|
||
self.instruction_data = | ||
bincode::serialize(&ProgramInstruction::DeactivateLookupTable).unwrap(); | ||
} | ||
|
||
fn exec_deactivate_lookup_table(&mut self) { | ||
self.prep_deactivate_lookup_table(); | ||
let accounts = self.run(); | ||
|
||
let lookup_table_account = accounts[0].clone(); | ||
self.transaction_accounts[0] = (self.lookup_table_address, lookup_table_account); | ||
// advance clock after deactivating | ||
self.transaction_accounts[6] = ( | ||
clock::id(), | ||
create_account_shared_data_for_test(&Clock { | ||
slot: self.recent_slot.wrapping_add(1), | ||
..Clock::default() | ||
}), | ||
); | ||
} | ||
|
||
fn prep_close_lookup_table(&mut self) { | ||
self.instruction_accounts = vec![ | ||
AccountMeta::new(self.lookup_table_address, false), | ||
AccountMeta::new(self.authority_address, true), | ||
AccountMeta::new(self.payer_address, false), | ||
]; | ||
|
||
self.instruction_data = bincode::serialize(&ProgramInstruction::CloseLookupTable).unwrap(); | ||
} | ||
|
||
fn run(&self) -> Vec<AccountSharedData> { | ||
mock_process_instruction( | ||
&address_lookup_table::id(), | ||
Vec::new(), | ||
&self.instruction_data, | ||
self.transaction_accounts.clone(), | ||
self.instruction_accounts.clone(), | ||
Ok(()), //expected_result, | ||
solana_address_lookup_table_program::processor::Entrypoint::vm, | ||
|invoke_context| { | ||
// add System to program_cache_for_tx_batch, so it can be native_invoke() | ||
invoke_context.program_cache_for_tx_batch.replenish( | ||
system_program::id(), | ||
Arc::new(ProgramCacheEntry::new_builtin( | ||
0, | ||
0, | ||
solana_system_program::system_processor::Entrypoint::vm, | ||
)), | ||
); | ||
}, | ||
|_invoke_context| {}, | ||
) | ||
} | ||
} | ||
|
||
fn bench_create_lookup_table(c: &mut Criterion) { | ||
let mut test_setup = TestSetup::new(); | ||
test_setup.prep_create_lookup_table(); | ||
|
||
c.bench_function("create_lookup_table", |bencher| { | ||
bencher.iter(|| black_box(test_setup.run())) | ||
}); | ||
} | ||
|
||
fn bench_extend_lookup_table(c: &mut Criterion) { | ||
let mut test_setup = TestSetup::new(); | ||
test_setup.exec_create_lookup_table(); | ||
test_setup.prep_extend_lookup_table(); | ||
|
||
c.bench_function("extend_lookup_table", |bencher| { | ||
bencher.iter(|| black_box(test_setup.run())) | ||
}); | ||
} | ||
|
||
fn bench_freeze_lookup_table(c: &mut Criterion) { | ||
let mut test_setup = TestSetup::new(); | ||
test_setup.exec_create_lookup_table(); | ||
test_setup.exec_extend_lookup_table(); | ||
test_setup.prep_freeze_lookup_table(); | ||
|
||
c.bench_function("freeze_lookup_table", |bencher| { | ||
bencher.iter(|| black_box(test_setup.run())) | ||
}); | ||
} | ||
|
||
fn bench_deactivate_lookup_table(c: &mut Criterion) { | ||
let mut test_setup = TestSetup::new(); | ||
test_setup.exec_create_lookup_table(); | ||
test_setup.prep_deactivate_lookup_table(); | ||
|
||
c.bench_function("deactivate_lookup_table", |bencher| { | ||
bencher.iter(|| black_box(test_setup.run())) | ||
}); | ||
} | ||
|
||
fn bench_close_lookup_table(c: &mut Criterion) { | ||
let mut test_setup = TestSetup::new(); | ||
test_setup.exec_create_lookup_table(); | ||
test_setup.exec_deactivate_lookup_table(); | ||
test_setup.prep_close_lookup_table(); | ||
|
||
c.bench_function("close_lookup_table", |bencher| { | ||
bencher.iter(|| black_box(test_setup.run())) | ||
}); | ||
} | ||
|
||
criterion_group!( | ||
benches, | ||
bench_create_lookup_table, | ||
bench_extend_lookup_table, | ||
bench_freeze_lookup_table, | ||
bench_deactivate_lookup_table, | ||
bench_close_lookup_table, | ||
); | ||
criterion_main!(benches); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using numbers to index accounts rather than at least named constant makes it fragile in case of code modifications.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea agree, thanks for good suggestion! can refactor it separately.