Skip to content

Commit e0bbb63

Browse files
committed
entrypoint: hint case with 2 accounts as unlikely
1 parent f136afe commit e0bbb63

File tree

1 file changed

+29
-24
lines changed
  • sdk/pinocchio/src/entrypoint

1 file changed

+29
-24
lines changed

sdk/pinocchio/src/entrypoint/mod.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use core::{
1818

1919
use crate::{
2020
account_info::{Account, AccountInfo, MAX_PERMITTED_DATA_INCREASE},
21+
hint::unlikely,
2122
pubkey::Pubkey,
2223
BPF_ALIGN_OF_U128, MAX_TX_ACCOUNTS, NON_DUP_MARKER,
2324
};
@@ -353,31 +354,35 @@ pub unsafe fn deserialize<const MAX_ACCOUNTS: usize>(
353354
// This is an optimization to reduce the number of jumps required to process the
354355
// accounts. The macro `process_accounts` will generate inline code to process the
355356
// specified number of accounts.
356-
while to_process_plus_one > 5 {
357-
// Process 5 accounts at a time.
358-
process_accounts!(5 => (input, accounts, accounts_slice));
359-
to_process_plus_one -= 5;
360-
}
361-
362-
// There might be remaining accounts to process.
363-
match to_process_plus_one {
364-
5 => {
365-
process_accounts!(4 => (input, accounts, accounts_slice));
366-
}
367-
4 => {
368-
process_accounts!(3 => (input, accounts, accounts_slice));
369-
}
370-
3 => {
371-
process_accounts!(2 => (input, accounts, accounts_slice));
372-
}
373-
2 => {
374-
process_accounts!(1 => (input, accounts, accounts_slice));
357+
if unlikely(to_process_plus_one == 2) {
358+
process_accounts!(1 => (input, accounts, accounts_slice));
359+
} else {
360+
while to_process_plus_one > 5 {
361+
// Process 5 accounts at a time.
362+
process_accounts!(5 => (input, accounts, accounts_slice));
363+
to_process_plus_one -= 5;
375364
}
376-
1 => (),
377-
_ => {
378-
// SAFETY: `while` loop above makes sure that `to_process_plus_one`
379-
// has 1 to 5 entries left.
380-
unsafe { core::hint::unreachable_unchecked() }
365+
366+
// There might be remaining accounts to process.
367+
match to_process_plus_one {
368+
5 => {
369+
process_accounts!(4 => (input, accounts, accounts_slice));
370+
}
371+
4 => {
372+
process_accounts!(3 => (input, accounts, accounts_slice));
373+
}
374+
3 => {
375+
process_accounts!(2 => (input, accounts, accounts_slice));
376+
}
377+
2 => {
378+
process_accounts!(1 => (input, accounts, accounts_slice));
379+
}
380+
1 => (),
381+
_ => {
382+
// SAFETY: `while` loop above makes sure that `to_process_plus_one`
383+
// has 1 to 5 entries left.
384+
unsafe { core::hint::unreachable_unchecked() }
385+
}
381386
}
382387
}
383388

0 commit comments

Comments
 (0)