What are the consequences of passing public keys as references or as values inside programs? Which approach should I use as a default?

As I understand now passing public keys on the stack might blow it up, as its size is quite small for programs, yet using it through a pointer will cost more compute.

Is my general understanding correct?

Also what is about the case when Pubkey is used in general programs. Which approach should be applied then?

1 Answer 1


In general I suggest passing references rather than values for pubkeys. It goes against usual logic but bpf compute cost doesn't care about cache locality. There are some cases where it might be better to pass by value (will need to be experimentally determined) but most of the time I've seen refs use less compute.

Keep in mind though that we're talking about 4 saved instructions per copy so you'd have to be copying a lot of public keys to make it worth refactoring, there are usually better places to optimize your code. Make it work then optimize.

Also refs can run into lifetime issues so may not be the best for junior rust developers.

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.