Skip to content

Conversation

@LucasSte
Copy link
Collaborator

@LucasSte LucasSte commented Oct 30, 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                      1229  1195   -34 ( -3%)
  solana_sbf_rust_membuiltins               277    69  -208 (-75%)
  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                  13578 13576    -2 ( -0%)
  solana_sbf_rust_secp256k1_recover       88376 88376    +0 ( +0%)
  solana_sbf_rust_sha                     21984 21984    +0 ( +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 using p-token unmodified and this PR using p-token without pubkey_eq.

Instruction Master This PR Diff
initialize_mint 106 108 +2
initialize_account 159 157 -2
initialize_multisig 211 221 +10
transfer 76 76 0
approve 129 136 +5
revoke 102 102 0
set_authority 131 130 -1
mint_to 124 123 -1
burn 129 129 0
close_account 121 121 0
freeze_account 150 150 0
thaw_account 146 146 0
transfer_checked 105 105 0
approve_checked 169 183 +14
mint_to_checked 173 177 +4
burn_checked 141 147 +6
initialize_account2 176 179 +3
initialize_account3 246 247 +1
initialize_multisig2 342 342 0
initialize_mint2 228 229 +1
amount_to_ui_amount 423 424 +1
ui_amount_to_amount 703 706 +3
initialize_immutable_owner 38 38 0
sync_native 62 62 0
batch 1499 1496 -3

@LucasSte LucasSte changed the title [SOL] Fix pubkey comparison [SOL] Expand memcmp earlier (take 2) Oct 30, 2025
@LucasSte
Copy link
Collaborator Author

LucasSte commented Oct 31, 2025

Relative to #170, the numbers for programs/sbf seem better and for p-token only slightly better (@febo agreed with that) . Since P-token numbers fluctuate a lot, I ran an Anchor tic-tac-toe game and compared the numbers to ensure this PR is not a regression.

PS: I am not repeating all the numbers for 'play' since it is a call to the same function with a different board configuration. The whole list for master is here and for this PR is here.

Instruction Master This PR Diff
SetupGame 5966 5937 -29
Play - 1 2480 2436 -44
Play - 2 2539 2493 -46
Play - Last 3105 3044 -61

@LucasSte LucasSte requested a review from Lichtso October 31, 2025 15:12
@LucasSte LucasSte marked this pull request as ready for review October 31, 2025 15:12
@LucasSte LucasSte merged commit 642e6ba into anza-xyz:solana-rustc/20.1-2025-02-13 Oct 31, 2025
14 checks passed
@LucasSte LucasSte deleted the fix-cmp-2 branch October 31, 2025 21:53
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