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

lang: Update dispatch function to support dynamic discriminators #3104

Conversation

acheroncrypto
Copy link
Collaborator

@acheroncrypto acheroncrypto commented Jul 22, 2024

Problem

The generated dispatch function gets the instruction discriminator by taking the first 8 bytes of the instruction data:

// Split the instruction data into the first 8 byte method
// identifier (sighash) and the serialized instruction data.
let mut ix_data: &[u8] = data;
let sighash: [u8; 8] = {
let mut sighash: [u8; 8] = [0; 8];
sighash.copy_from_slice(&ix_data[..8]);
ix_data = &ix_data[8..];
sighash
};

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's DISCRIMINATOR constant.

Bench

We save a little bit of CU initially due to not initializing an intermediary sighash variable. However, comparing via starts_with as opposed to matching exact bytes has a slight overhead of ~3 CU per instruction. The result is:

  • If the program has less than 20 ixs, this results in a slight performance gain
  • Otherwise, this results in extra ~3 CU per each instruction, e.g. if a program has 50 instructions, the last instruction will use ~100 more CU compared to before this PR

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.

Copy link

vercel bot commented Jul 22, 2024

@acheroncrypto is attempting to deploy a commit to the coral-xyz Team on Vercel.

A member of the Team first needs to authorize it.

@acheroncrypto acheroncrypto force-pushed the lang-update-dispatch-function-to-support-dynamic-discriminators branch from 7c0ed34 to bcd1e2c Compare July 23, 2024 05:25
@acheroncrypto acheroncrypto merged commit ccac42d into coral-xyz:master Jul 23, 2024
48 of 49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant