Skip to content

tlv-account-resolution: reduce SBF PDA derivation cost - #212

Open
beriktassuly wants to merge 2 commits into
solana-program:mainfrom
beriktassuly:optimize-sbf-pda-resolution
Open

beriktassuly wants to merge 2 commits into
solana-program:mainfrom
beriktassuly:optimize-sbf-pda-resolution

Conversation

@beriktassuly

@beriktassuly beriktassuly commented Nov 11, 2026 •

Copy link
Copy Markdown

Problem

Resolving PDA metas on SBF costs 1,500 CU per bump attempt via try_find_program_address.

Summary of changes

Resolve PDA metas with Address::derive_program_address (solana-address 2.3.0) instead of find_program_address. Seed lists outside the valid range still go through find_program_address.

In token-2022's success_transfer test (two PDA metas), the Token-2022 transfer drops from 22,080 to 20,296 CU (−8.1%).

@joncinque joncinque left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have the functions to do this more cheaply with Address::derive_program_address https://github.com/anza-xyz/solana-sdk/blob/8f48de5ed28208239775a90eba77f6fd5a95885c/address/src/derive.rs#L169

Let's use that instead of inlining all of the logic here

@beriktassuly

beriktassuly commented Nov 24, 2026 •

Copy link
Copy Markdown
Author

We have the functions to do this more cheaply with Address::derive_program_address. Let's use that instead of inlining all of the logic here

Switched to Address::derive_program_address and removed the inlined derivation, along with the solana-sha256-hasher dependency and the cfg gates, so the same path is used on all targets.

It needs solana-address 2.3.0, so I added it as a direct dependency with curve25519. The lockfile change is limited to solana-address and its own dependencies. Since it takes a fixed-size array, the seeds are padded with empty slices, which add no bytes to the hash input. Seed lists outside the valid range still go through find_program_address as before, and the test compares against it for every seed count.

Measured with success_transfer in token-2022's transfer_hook.rs (two PDA metas, fixed keys, same prebuilt hook in every run), Token-2022 TransferChecked:

CU
main 22,080
inline (d3ce00e) 20,000
derive_program_address (11f98e3) 20,296

The extra 296 CU over the inline version most likely comes from the padding: sol_sha256 charges a minimum per slice, so the empty slices aren't free. Matching on the seed count to call it with the exact N would avoid that, at the cost of one instantiation per count. Happy to do that if you think it's worth it, otherwise I'll leave it as is.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants