Skip to content
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

feat: collect and cache builtin instructions cost and count per transaction #2692

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions runtime-transaction/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ name = "solana_runtime_transaction"
bincode = { workspace = true }
criterion = { workspace = true }
rand = { workspace = true }
solana-compute-budget-program = { workspace = true }
solana-loader-v4-program = { workspace = true }
solana-program = { workspace = true }

[package.metadata.docs.rs]
Expand Down
33 changes: 33 additions & 0 deletions runtime-transaction/benches/process_compute_budget_instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,45 @@ fn bench_process_compute_budget_instructions_mixed(c: &mut Criterion) {
);
}

fn bench_process_compute_budget_and_transfer_only(c: &mut Criterion) {
let num_instructions = 355;
let pubkey = Pubkey::new_unique();
c.benchmark_group("bench_process_compute_budget_and_transfer_only")
.throughput(Throughput::Elements(NUM_TRANSACTIONS_PER_ITER as u64))
.bench_function(
format!("{num_instructions} transfer instructions and compute budget ixs"),
|bencher| {
let payer_keypair = Keypair::new();
let mut ixs: Vec<_> = (4..num_instructions)
.map(|i| system_instruction::transfer(&payer_keypair.pubkey(), &pubkey, i))
.collect();
ixs.extend(vec![
ComputeBudgetInstruction::request_heap_frame(40 * 1024),
ComputeBudgetInstruction::set_compute_unit_limit(u32::MAX),
ComputeBudgetInstruction::set_compute_unit_price(u64::MAX),
ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(u32::MAX),
]);
apfitzge marked this conversation as resolved.
Show resolved Hide resolved
let tx = build_sanitized_transaction(&payer_keypair, &ixs);

bencher.iter(|| {
(0..NUM_TRANSACTIONS_PER_ITER).for_each(|_| {
assert!(process_compute_budget_instructions(black_box(
SVMMessage::program_instructions_iter(&tx)
))
.is_ok())
})
});
},
);
}

criterion_group!(
benches,
bench_process_compute_budget_instructions_empty,
bench_process_compute_budget_instructions_no_builtins,
bench_process_compute_budget_instructions_compute_budgets,
bench_process_compute_budget_instructions_builtins,
bench_process_compute_budget_instructions_mixed,
bench_process_compute_budget_and_transfer_only,
);
criterion_main!(benches);
104 changes: 104 additions & 0 deletions runtime-transaction/src/builtin_auxiliary_data_store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// static account keys has max
use {
agave_transaction_view::static_account_keys_meta::MAX_STATIC_ACCOUNTS_PER_PACKET as FILTER_SIZE,
solana_builtins_default_costs::{BUILTIN_INSTRUCTION_COSTS, MAYBE_BUILTIN_KEY},
solana_sdk::pubkey::Pubkey,
};

pub(crate) struct BuiltinAuxiliaryDataStore {
// Array of auxiliary data for all possible static and sanitized program_id_index,
// Each possible value of data indicates:
// None - un-checked
// Some<None> - checked, not builtin
// Some<Some<(bool, u32)>> - checked, is builtin and (is-compute-budget, default-cost)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me this would better represented by a new enum:

    #[derive(Default)]
    enum BuiltinCheckStatus {
		#[default]
        Unchecked,
        NotBuiltin,
        Builtin{
            is_compute_budget: bool,
            default_cost: u32,
        }
    }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think this saves a byte as well since we don't need 2 option discriminants

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum is a way to go

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d76291d

savings cross all benches, due to smaller memory footprint
     Running benches/process_compute_budget_instructions.rs (target/release/deps/process_compute_budget_instructions-b53a7f97abe58117)
bench_process_compute_budget_instructions_empty/0 instructions
                        time:   [11.957 µs 11.975 µs 11.995 µs]
                        thrpt:  [85.368 Melem/s 85.513 Melem/s 85.641 Melem/s]
                 change:
                        time:   [-29.911% -29.703% -29.488%] (p = 0.00 < 0.05)
                        thrpt:  [+41.820% +42.253% +42.676%]
                        Performance has improved.
Found 7 outliers among 100 measurements (7.00%)
  5 (5.00%) high mild
  2 (2.00%) high severe

bench_process_compute_budget_instructions_no_builtins/4 dummy Instructions
                        time:   [18.924 µs 18.941 µs 18.959 µs]
                        thrpt:  [54.011 Melem/s 54.062 Melem/s 54.112 Melem/s]
                 change:
                        time:   [-15.416% -15.210% -15.005%] (p = 0.00 < 0.05)
                        thrpt:  [+17.654% +17.938% +18.225%]
                        Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
  1 (1.00%) low mild
  1 (1.00%) high mild
  4 (4.00%) high severe

bench_process_compute_budget_instructions_compute_budgets/4 compute-budget instructions
                        time:   [37.252 µs 37.332 µs 37.424 µs]
                        thrpt:  [27.362 Melem/s 27.430 Melem/s 27.488 Melem/s]
                 change:
                        time:   [-21.462% -21.177% -20.898%] (p = 0.00 < 0.05)
                        thrpt:  [+26.419% +26.867% +27.328%]
                        Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
  2 (2.00%) high mild

bench_process_compute_budget_instructions_builtins/4 dummy builtins
                        time:   [41.314 µs 41.521 µs 41.730 µs]
                        thrpt:  [24.539 Melem/s 24.662 Melem/s 24.786 Melem/s]
                 change:
                        time:   [-8.4225% -8.0835% -7.7755%] (p = 0.00 < 0.05)
                        thrpt:  [+8.4311% +8.7944% +9.1972%]
                        Performance has improved.
Found 18 outliers among 100 measurements (18.00%)
  18 (18.00%) high mild

bench_process_compute_budget_instructions_mixed/355 mixed instructions
                        time:   [539.98 µs 540.43 µs 540.90 µs]
                        thrpt:  [1.8931 Melem/s 1.8948 Melem/s 1.8964 Melem/s]
                 change:
                        time:   [-2.9610% -2.8023% -2.6445%] (p = 0.00 < 0.05)
                        thrpt:  [+2.7164% +2.8831% +3.0513%]
                        Performance has improved.
Found 3 outliers among 100 measurements (3.00%)
  1 (1.00%) high mild
  2 (2.00%) high severe

bench_process_compute_budget_and_transfer_only/355 transfer instructions and compute budget ixs
                        time:   [607.07 µs 607.59 µs 608.19 µs]
                        thrpt:  [1.6837 Melem/s 1.6853 Melem/s 1.6868 Melem/s]
                 change:
                        time:   [-4.9817% -4.8051% -4.6671%] (p = 0.00 < 0.05)
                        thrpt:  [+4.8956% +5.0477% +5.2429%]
                        Performance has improved.
Found 7 outliers among 100 measurements (7.00%)
  5 (5.00%) high mild
  2 (2.00%) high severe

auxiliary_data: [Option<Option<(bool, u32)>>; FILTER_SIZE as usize],
}

impl BuiltinAuxiliaryDataStore {
pub(crate) fn new() -> Self {
BuiltinAuxiliaryDataStore {
auxiliary_data: [None; FILTER_SIZE as usize],
}
}

#[inline]
pub(crate) fn get_auxiliary_data(
&mut self,
index: usize,
program_id: &Pubkey,
) -> Option<(bool, u32)> {
*self
.auxiliary_data
.get_mut(index)
.expect("program id index is sanitized")
.get_or_insert_with(|| {
if !MAYBE_BUILTIN_KEY[program_id.as_ref()[0] as usize] {
return None;
}

BUILTIN_INSTRUCTION_COSTS.get(program_id).map(|cost| {
(
solana_sdk::compute_budget::check_id(program_id),
*cost as u32,
)
})
})
}
}

#[cfg(test)]
mod test {
use super::*;

const DUMMY_PROGRAM_ID: &str = "dummmy1111111111111111111111111111111111111";

#[test]
fn test_get_auxiliary_data() {
let mut test_store = BuiltinAuxiliaryDataStore::new();
let mut index = 9;

// initial state is Unchecked (eg, None)
assert!(test_store.auxiliary_data[index].is_none());

// non builtin returns None
assert!(test_store
.get_auxiliary_data(index, &DUMMY_PROGRAM_ID.parse().unwrap())
.is_none());
// but its state is now checked (eg, Some(...))
assert_eq!(test_store.auxiliary_data[index], Some(None));
// lookup same `index` will return cached auxiliary data, will *not* lookup `program_id`
// again
assert!(test_store
.get_auxiliary_data(index, &solana_sdk::loader_v4::id())
.is_none());

// builtin return default cost
index += 1;
assert_eq!(
test_store.get_auxiliary_data(index, &solana_sdk::loader_v4::id()),
Some((
false,
solana_loader_v4_program::DEFAULT_COMPUTE_UNITS as u32
))
);

// compute-budget return default cost, and true flag
index += 1;
assert_eq!(
test_store.get_auxiliary_data(index, &solana_sdk::compute_budget::id()),
Some((
true,
solana_compute_budget_program::DEFAULT_COMPUTE_UNITS as u32
))
);
}

#[test]
#[should_panic(expected = "program id index is sanitized")]
fn test_get_auxiliary_data_out_of_bound_index() {
let mut test_store = BuiltinAuxiliaryDataStore::new();
assert!(test_store
.get_auxiliary_data(FILTER_SIZE as usize + 1, &DUMMY_PROGRAM_ID.parse().unwrap())
.is_none());
}
}
37 changes: 0 additions & 37 deletions runtime-transaction/src/compute_budget_program_id_filter.rs

This file was deleted.

Loading