lang: Update dispatch
function to support dynamic discriminators
#3104
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.
Problem
The generated
dispatch
function gets the instruction discriminator by taking the first 8 bytes of the instructiondata
:anchor/lang/syn/src/codegen/program/dispatch.rs
Lines 57 to 65 in e5bed20
This obviously doesn't work with discriminators that are not exactly 8 bytes.
Summary of changes
Dispatch instructions based on whether the given
data
starts with an instruction'sDISCRIMINATOR
constant.Bench
We save a little bit of CU initially due to not initializing an intermediary
sighash
variable. However, comparing viastarts_with
as opposed to matching exact bytes has a slight overhead of ~3 CU per instruction. The result is:See the compute units benchmarks to compare with real values as instruction count increases (
bench
program has 87 instructions).The difference shouldn't be noticable in almost all cases. However, if people notice the regression with higher instruction count programs, we can consider adding a feature flag that uses the old
dispatch
codegen.Note: This PR is part of a greater effort explained in #3097.