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

cost model: unify builtin lists for core bpf migration tracking #4039

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

buffalojoec
Copy link

@buffalojoec buffalojoec commented Dec 10, 2024

Building on #3975, this PR combines the tracking of builtin Core BPF migrations in the cost modeling and other areas of the runtime to use a single list - the BUILTINS list - as the source of truth.

This should enable contributors to avoid footguns when updating Core BPF migration configs on the BUILTINS list and accidentally breaking cost modeling.

Copy link

@tao-stones tao-stones left a comment

Choose a reason for hiding this comment

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

Awesome to have the single source of truth here. Can you take a look at #3975, there two BUILTINS arrays were created - one for migrating one for not-migrating. The motivation is each transaction can have a fixed-length array in meta data, avoiding Vec allocation, by

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: [u16; MIGRATING_BUILTINS_COSTS.len()],
}

Do you think if that's a reasonable approach? If so, could this PR rebase on top of that one?

builtins/src/cost_modeling.rs Outdated Show resolved Hide resolved
builtins/src/cost_modeling.rs Show resolved Hide resolved
builtins/src/cost_modeling.rs Outdated Show resolved Hide resolved
@buffalojoec
Copy link
Author

@tao-stones thanks for taking a look! I refactored this a bit and re-wrote my commits (I had to rebase anyway from some other changes that went in).

With this approach, we can eliminate the footgun of having to update two lists with a new migration feature ID. The only "secondary" area that needs to get updated now is the NUM_COST_MODELED_BUILTINS_WITH_MIGRATIONS in the cost_modeling module. However, I've added a test that will barf if someone forgets to adjust the constant. The feature IDs are always sourced from BUILTINS, which should eliminate the footgun.

Copy link

@tao-stones tao-stones left a comment

Choose a reason for hiding this comment

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

Like it very much to have single source for builtins.

builtins/src/cost_modeling.rs Show resolved Hide resolved
lazy_static! {
// This lazy-static list is designed to panic if any builtin migrations
// are changed without updating `NUM_COST_MODELED_BUILTINS_WITH_MIGRATIONS`.
static ref COST_MODELED_BUILTINS_WITH_MIGRATIONS: [BuiltinWithMigration; NUM_COST_MODELED_BUILTINS_WITH_MIGRATIONS] = {

Choose a reason for hiding this comment

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

nit: it validates migrating builtins not exceeds NUM_COST_MODELED_BUILTINS_WITH_MIGRATIONS, but not check if number of migrating builtins reduced (eg. migration completed).

Copy link
Author

Choose a reason for hiding this comment

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

Yes, true, it only checks if a feature gate was added or removed, but it doesn't know about feature status for this list. That's what the querying API is for below this.

pub fn get_migration_feature_index(program_id: &Pubkey) -> Option<usize> {
COST_MODELED_BUILTINS_WITH_MIGRATIONS
.iter()
.position(|builtin| builtin.program_id == *program_id)

Choose a reason for hiding this comment

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

this iter, even small, can/want be avoided -- #3975 addresses by explicitly adding position to BuiltinCost. It is probably not easy to do when COST_MODELED_BUILTINS_WITH_MIGRATIONS is generated unrelated to BUILTIN_INSTRUCTION_COSTS (where lookup happens).

I appreciate the simplicity tho, also think once we have customized lookup and retires BUILTIN_INSTRUCTION_COSTS, this iter can be eliminated.

compute-budget-instruction/src/builtin_programs_filter.rs Outdated Show resolved Hide resolved
}

lazy_static! {
static ref BUILTIN_INSTRUCTION_COSTS: AHashMap<Pubkey, BuiltinCost> = BUILTINS

Choose a reason for hiding this comment

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

a note for future refactors to remove BUILTIN_INSTRUCTION_COSTS: this AHashMap has two functions:

  1. CostModel uses it to determine if program is builtin. It can be done by checking if NotCostModeled
  2. Provides lookup by program_id. Since the list of builtins is small and static, could implement customized lookup instead.

Choose a reason for hiding this comment

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

Another note, would like to profile perf between AHashmap::get() and binary_search if BUILTINS is sorted,

Copy link
Author

Choose a reason for hiding this comment

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

Okay, I can profile this. Afaict BUILTINS being sorted shouldn't break anything in the runtime.

@buffalojoec buffalojoec force-pushed the unify-builtin-lists-wip branch from 1c5c3d7 to 8f0c37a Compare December 18, 2024 05:51
@buffalojoec buffalojoec changed the title Unify builtin lists wip cost model: unify builtin lists for core bpf migration tracking Dec 18, 2024
@buffalojoec
Copy link
Author

Hey @tao-stones thanks for the review! I've rebased and updated this PR based on #3975 now that it was merged. Some of the commits have changed to absorb the changes from #3975, so you might have to give this another once-over.

Copy link

@tao-stones tao-stones left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks for refreshing it. Fort transparency, @apfitzge initially pointed out optimization opportunity wrt https://github.com/anza-xyz/agave/pull/4039/files#r1880590905 in Master version, which was easier to implement then. I'm good the way it is implemented in this PR, would like @apfitzge weight in.

builtins/src/cost_modeling.rs Outdated Show resolved Hide resolved
}

lazy_static! {
static ref BUILTIN_INSTRUCTION_COSTS: AHashMap<Pubkey, BuiltinCost> = BUILTINS

Choose a reason for hiding this comment

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

Another note, would like to profile perf between AHashmap::get() and binary_search if BUILTINS is sorted,

@buffalojoec buffalojoec marked this pull request as ready for review December 19, 2024 08:05
@buffalojoec buffalojoec requested a review from a team as a code owner December 19, 2024 08:05
@@ -369,6 +403,7 @@ mod tests {
// Since a macro is used to initialize the test IDs from the `test_only`
// module, best to ensure the lists have the expected values within a test
// context.
#[cfg(feature = "mock-builtin-migrations")]
Copy link

@LucasSte LucasSte Dec 19, 2024

Choose a reason for hiding this comment

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

As this test is behind a CFG feature, it won't run in the CI. Do we want it to run?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants