3

When declaring a PDA in anchor, what is the difference between

#[account(
    seeds = [SomeSeed],
    bump
)]
pub my_acc: Account<'info, MyAccount>,

and

#[account(
    seeds = [SomeSeed],
    bump = my_acc.bump
)]
pub my_acc: Account<'info, MyAccount>,

I assumed that both of these tell anchor to use the canonical bump, but does one of them just store it in the account/pass it as extra instruction data? Or is the difference something else?

1 Answer 1

4

Depending on the seed you can save a lot of CU saving the bumb in an account.

This is because to find a PDA that is off curve internally anchor calls create_program_address repedadly with decreasing bumbs until it finds a PDA thats not on the curve.

Here is one example:

    // 12,136 CUs
    compute_fn! { "Find PDA" =>
        Pubkey::find_program_address(&[b"counter"], ctx.program_id);
    }

    // 1,651 CUs
    compute_fn! { "Find PDA" =>
        Pubkey::create_program_address(&[b"counter", &[248_u8]], &program_id).unwrap();
    }

In anchor:

    // 12,136 CUs when not defining the bump
    #[account(
        seeds = [b"counter"],
        bump
    )]

    // only 1600 if using the bump that is saved in the counter_checked account
    #[account(
        seeds = [b"counter"],
        bump = counter_checked.bump
    )]

So here you would have a u8 field in the counter_checked account and set it when you initialize it. Then you can just always use that cached bump when you call an instruction.

Here is a guide explaining the optimization: https://solana.com/de/developers/guides/advanced/how-to-optimize-compute and here a test you can run to try it yourself: https://github.com/solana-developers/cu_optimizations/blob/main/anchor/counter/anchor/programs/counter/src/lib.rs#L18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.