Skip to content

Conversation

@LucasSte
Copy link
Collaborator

@LucasSte LucasSte commented Oct 23, 2025

Problem

It has been pointed out that comparing pub keys with the equal operator consumes more CUs than it should. I found the problem to be that LLVM lowers memcmp too late in the pipeline stage, so that it still contains suboptimal code when going to machine code generation.

Solution

Lower memcmp earlier.

Benchamarks

Programs/sbf

  SBF program                          expected actual  diff
  solana_sbf_rust_128bit                    770   770    +0 ( +0%)
  solana_sbf_rust_alloc                    4767  4767    +0 ( +0%)
  solana_sbf_rust_custom_heap               324   324    +0 ( +0%)
  solana_sbf_rust_dep_crate                  20    20    +0 ( +0%)
  solana_sbf_rust_iter                     1513  1513    +0 ( +0%)
  solana_sbf_rust_many_args                1281  1281    +0 ( +0%)
  solana_sbf_rust_mem                      1231  1229    -2 ( -0%)
  solana_sbf_rust_membuiltins               294   277   -17 ( -6%)
  solana_sbf_rust_noop                      342   342    +0 ( +0%)
  solana_sbf_rust_param_passing             108   108    +0 ( +0%)
  solana_sbf_rust_rand                      301   301    +0 ( +0%)
  solana_sbf_rust_sanity                  13570 13578    +8 ( +0%)
  solana_sbf_rust_secp256k1_recover       88376 88376    +0 ( +0%)
  solana_sbf_rust_sha                     21990 21984    -6 ( -0%)

No change to solana or Pinocchio entrypoint

Pubkey comparison

Name CUs Delta
Different 10 -2
Same 17 -3

P-token (after reverting solana-program/token#90 and removing pubkey_eq from Pinocchio).
The comparison is against LLVM master with p-token unmodified and this PR with the p-token without pubkey_eq.

Function Master This PR Diff
initialize_mint 106 107 +1
initialize_account 159 156 -3
transfer 76 77 +1
transfer_checked 105 106 +1
batch 1499 1477 -22
mint_to 124 123 -1
approve 129 133 +4
burn_checked 141 145 +4
revoke 102 102 0
set_authority 131 130 -1

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.

1 participant