Skip to content

Commit

Permalink
Declare Non/migrating buiiltins in const array, eleminates heap alloc…
Browse files Browse the repository at this point in the history
…ation (Vec<>) per transaction
  • Loading branch information
tao-stones committed Dec 9, 2024
1 parent 8a7aa87 commit 45dfd71
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
64 changes: 36 additions & 28 deletions builtins-default-costs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ use {
};

/// DEVELOPER: when a builtin is migrated to sbpf, please add its corresponding
/// migration feature ID to BUILTIN_INSTRUCTION_COSTS, so the builtin's default
/// cost can be determined properly based on feature status.
/// migration feature ID to BUILTIN_INSTRUCTION_COSTS, and move it from
/// NON_MIGRATING_BUILTINS_COSTS to MIGRATING_BUILTINS_COSTS, so the builtin's
/// default cost can be determined properly based on feature status.
/// When migration completed, eg the feature gate is enabled everywhere, please
/// remove that builtin entry from BUILTIN_INSTRUCTION_COSTS.
/// remove that builtin entry from MIGRATING_BUILTINS_COSTS.
#[derive(Clone)]
struct BuiltinCost {
pub struct BuiltinCost {
native_cost: u64,
core_bpf_migration_feature: Option<Pubkey>,
}

lazy_static! {
/// Number of compute units for each built-in programs
///
/// DEVELOPER WARNING: This map CANNOT be modified without causing a
/// consensus failure because this map is used to calculate the compute
/// limit for transactions that don't specify a compute limit themselves as
/// of https://github.com/anza-xyz/agave/issues/2212. It's also used to
/// calculate the cost of a transaction which is used in replay to enforce
/// block cost limits as of
/// https://github.com/solana-labs/solana/issues/29595.
static ref BUILTIN_INSTRUCTION_COSTS: AHashMap<Pubkey, BuiltinCost> = [
/// Number of compute units for each built-in programs
///
/// DEVELOPER WARNING: This map CANNOT be modified without causing a
/// consensus failure because this map is used to calculate the compute
/// limit for transactions that don't specify a compute limit themselves as
/// of https://github.com/anza-xyz/agave/issues/2212. It's also used to
/// calculate the cost of a transaction which is used in replay to enforce
/// block cost limits as of
/// https://github.com/solana-labs/solana/issues/29595.
pub const MIGRATING_BUILTINS_COSTS: &[(Pubkey, BuiltinCost)] = &[
(
stake::id(),
BuiltinCost {
Expand All @@ -48,6 +48,18 @@ lazy_static! {
core_bpf_migration_feature: Some(feature_set::migrate_config_program_to_core_bpf::id()),
},
),
(
address_lookup_table::id(),
BuiltinCost {
native_cost: solana_address_lookup_table_program::processor::DEFAULT_COMPUTE_UNITS,
core_bpf_migration_feature: Some(
feature_set::migrate_address_lookup_table_program_to_core_bpf::id(),
),
},
),
];

pub const NON_MIGRATING_BUILTINS_COSTS: &[(Pubkey, BuiltinCost)] = &[
(
vote::id(),
BuiltinCost {
Expand All @@ -69,15 +81,6 @@ lazy_static! {
core_bpf_migration_feature: None,
},
),
(
address_lookup_table::id(),
BuiltinCost {
native_cost: solana_address_lookup_table_program::processor::DEFAULT_COMPUTE_UNITS,
core_bpf_migration_feature: Some(
feature_set::migrate_address_lookup_table_program_to_core_bpf::id(),
),
},
),
(
bpf_loader_upgradeable::id(),
BuiltinCost {
Expand Down Expand Up @@ -121,11 +124,16 @@ lazy_static! {
core_bpf_migration_feature: None,
},
),
];

lazy_static! {
static ref BUILTIN_INSTRUCTION_COSTS: AHashMap<Pubkey, BuiltinCost> =
MIGRATING_BUILTINS_COSTS
.iter()
.chain(NON_MIGRATING_BUILTINS_COSTS.iter())
.cloned()
.collect();
// DO NOT ADD MORE ENTRIES TO THIS MAP
]
.iter()
.cloned()
.collect();
}

lazy_static! {
Expand Down
2 changes: 1 addition & 1 deletion runtime-transaction/src/builtin_programs_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(crate) enum ProgramKind {
Builtin,
// Builtin program maybe in process of being migrated to core bpf,
// if core_bpf_migration_feature is activated, then the migration has
// completed and it should not longer be considered as builtin
// completed and it should no longer be considered as builtin
MigratingBuiltin {
core_bpf_migration_feature_index: usize,
},
Expand Down
4 changes: 2 additions & 2 deletions runtime-transaction/src/compute_budget_instruction_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ struct MigrationBuiltinFeatureCounter {
// The vector of counters, matching the size of the static vector MIGRATION_FEATURE_IDS,
// each counter representing the number of times its corresponding feature ID is
// referenced in this transaction.
migrating_builtin: Vec<u16>,
migrating_builtin: [u16; solana_builtins_default_costs::MIGRATING_BUILTINS_COSTS.len()],
}

impl Default for MigrationBuiltinFeatureCounter {
fn default() -> Self {
Self {
migrating_builtin: vec![0; MIGRATION_FEATURES_ID.len()],
migrating_builtin: [0; solana_builtins_default_costs::MIGRATING_BUILTINS_COSTS.len()],
}
}
}
Expand Down

0 comments on commit 45dfd71

Please sign in to comment.