-
Notifications
You must be signed in to change notification settings - Fork 252
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
base: master
Are you sure you want to change the base?
Conversation
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.
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?
62aefd8
to
1c5c3d7
Compare
@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 |
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.
Like it very much to have single source for builtins.
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] = { |
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.
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).
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.
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) |
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.
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.
} | ||
|
||
lazy_static! { | ||
static ref BUILTIN_INSTRUCTION_COSTS: AHashMap<Pubkey, BuiltinCost> = BUILTINS |
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.
a note for future refactors to remove BUILTIN_INSTRUCTION_COSTS
: this AHashMap
has two functions:
- CostModel uses it to determine if program is builtin. It can be done by checking if
NotCostModeled
- Provides lookup by
program_id
. Since the list of builtins is small and static, could implement customized lookup instead.
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.
Another note, would like to profile perf between AHashmap::get()
and binary_search
if BUILTINS
is sorted,
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.
Okay, I can profile this. Afaict BUILTINS
being sorted shouldn't break anything in the runtime.
1c5c3d7
to
8f0c37a
Compare
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. |
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.
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.
} | ||
|
||
lazy_static! { | ||
static ref BUILTIN_INSTRUCTION_COSTS: AHashMap<Pubkey, BuiltinCost> = BUILTINS |
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.
Another note, would like to profile perf between AHashmap::get()
and binary_search
if BUILTINS
is sorted,
Co-authored-by: Tao Zhu <[email protected]>
@@ -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")] |
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.
As this test is behind a CFG feature, it won't run in the CI. Do we want it to run?
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.