Skip to content

Commit 35b79ea

Browse files
authored
p-token: Add compiler hints (#136)
Add compiler hints
1 parent e7fe2e3 commit 35b79ea

5 files changed

Lines changed: 29 additions & 19 deletions

File tree

pinocchio/interface/src/state/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use pinocchio::program_error::ProgramError;
1+
use pinocchio::{
2+
hint::{likely, unlikely},
3+
program_error::ProgramError,
4+
};
25

36
pub mod account;
47
pub mod account_state;
@@ -37,7 +40,7 @@ pub trait Initializable {
3740
pub unsafe fn load<T: Initializable + Transmutable>(bytes: &[u8]) -> Result<&T, ProgramError> {
3841
load_unchecked(bytes).and_then(|t: &T| {
3942
// checks if the data is initialized
40-
if t.is_initialized()? {
43+
if likely(t.is_initialized()?) {
4144
Ok(t)
4245
} else {
4346
Err(ProgramError::UninitializedAccount)
@@ -54,7 +57,7 @@ pub unsafe fn load<T: Initializable + Transmutable>(bytes: &[u8]) -> Result<&T,
5457
/// The caller must ensure that `bytes` contains a valid representation of `T`.
5558
#[inline(always)]
5659
pub unsafe fn load_unchecked<T: Transmutable>(bytes: &[u8]) -> Result<&T, ProgramError> {
57-
if bytes.len() != T::LEN {
60+
if unlikely(bytes.len() != T::LEN) {
5861
return Err(ProgramError::InvalidAccountData);
5962
}
6063
Ok(&*(bytes.as_ptr() as *const T))
@@ -71,7 +74,7 @@ pub unsafe fn load_mut<T: Initializable + Transmutable>(
7174
) -> Result<&mut T, ProgramError> {
7275
load_mut_unchecked(bytes).and_then(|t: &mut T| {
7376
// checks if the data is initialized
74-
if t.is_initialized()? {
77+
if likely(t.is_initialized()?) {
7578
Ok(t)
7679
} else {
7780
Err(ProgramError::UninitializedAccount)
@@ -90,7 +93,7 @@ pub unsafe fn load_mut<T: Initializable + Transmutable>(
9093
pub unsafe fn load_mut_unchecked<T: Transmutable>(
9194
bytes: &mut [u8],
9295
) -> Result<&mut T, ProgramError> {
93-
if bytes.len() != T::LEN {
96+
if unlikely(bytes.len() != T::LEN) {
9497
return Err(ProgramError::InvalidAccountData);
9598
}
9699
Ok(&mut *(bytes.as_mut_ptr() as *mut T))

pinocchio/program/src/processor/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ fn try_ui_amount_into_amount(ui_amount: &str, decimals: u8) -> Result<u64, Progr
204204
#[inline(always)]
205205
const fn unpack_amount(instruction_data: &[u8]) -> Result<u64, TokenError> {
206206
// expected u64 (8)
207-
if instruction_data.len() >= U64_BYTES {
207+
if likely(instruction_data.len() >= U64_BYTES) {
208208
// SAFETY: The minimum size of the instruction data is `U64_BYTES` bytes.
209209
Ok(unsafe { u64::from_le_bytes(*(instruction_data.as_ptr() as *const [u8; U64_BYTES])) })
210210
} else {
@@ -216,7 +216,7 @@ const fn unpack_amount(instruction_data: &[u8]) -> Result<u64, TokenError> {
216216
#[inline(always)]
217217
const fn unpack_amount_and_decimals(instruction_data: &[u8]) -> Result<(u64, u8), TokenError> {
218218
// expected u64 (8) + u8 (1)
219-
if instruction_data.len() >= 9 {
219+
if likely(instruction_data.len() >= 9) {
220220
let (amount, decimals) = instruction_data.split_at(U64_BYTES);
221221
Ok((
222222
// SAFETY: The size of `amount` is `U64_BYTES` bytes.

pinocchio/program/src/processor/revoke.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use {
22
super::validate_owner,
3-
pinocchio::{account_info::AccountInfo, program_error::ProgramError, ProgramResult},
3+
pinocchio::{
4+
account_info::AccountInfo, hint::unlikely, program_error::ProgramError, ProgramResult,
5+
},
46
pinocchio_token_interface::{
57
error::TokenError,
68
state::{account::Account, load_mut},
@@ -24,7 +26,7 @@ pub fn process_revoke(accounts: &[AccountInfo]) -> ProgramResult {
2426
return Err(ProgramError::NotEnoughAccountKeys);
2527
};
2628

27-
if source_account.is_frozen()? {
29+
if unlikely(source_account.is_frozen()?) {
2830
return Err(TokenError::AccountFrozen.into());
2931
}
3032

pinocchio/program/src/processor/shared/burn.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use {
22
crate::processor::{check_account_owner, validate_owner},
33
pinocchio::{
4-
account_info::AccountInfo, hint::likely, program_error::ProgramError, pubkey::pubkey_eq,
4+
account_info::AccountInfo,
5+
hint::{likely, unlikely},
6+
program_error::ProgramError,
7+
pubkey::pubkey_eq,
58
ProgramResult,
69
},
710
pinocchio_token_interface::{
@@ -31,10 +34,11 @@ pub fn process_burn(
3134
// passed in, one of them will fail the `load_mut` check.
3235
let mint = unsafe { load_mut::<Mint>(mint_info.borrow_mut_data_unchecked())? };
3336

34-
if source_account.is_frozen()? {
37+
if unlikely(source_account.is_frozen()?) {
3538
return Err(TokenError::AccountFrozen.into());
3639
}
37-
if source_account.is_native() {
40+
41+
if unlikely(source_account.is_native()) {
3842
return Err(TokenError::NativeNotSupported.into());
3943
}
4044

@@ -45,12 +49,12 @@ pub fn process_burn(
4549
.checked_sub(amount)
4650
.ok_or(TokenError::InsufficientFunds)?;
4751

48-
if !pubkey_eq(mint_info.key(), &source_account.mint) {
52+
if unlikely(!pubkey_eq(mint_info.key(), &source_account.mint)) {
4953
return Err(TokenError::MintMismatch.into());
5054
}
5155

5256
if let Some(expected_decimals) = expected_decimals {
53-
if expected_decimals != mint.decimals {
57+
if unlikely(expected_decimals != mint.decimals) {
5458
return Err(TokenError::MintDecimalsMismatch.into());
5559
}
5660
}
@@ -80,7 +84,7 @@ pub fn process_burn(
8084

8185
// Updates the source account and mint supply.
8286

83-
if amount == 0 {
87+
if unlikely(amount == 0) {
8488
check_account_owner(source_account_info)?;
8589
check_account_owner(mint_info)?;
8690
} else {

pinocchio/program/src/processor/shared/mint_to.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use {
22
crate::processor::{check_account_owner, validate_owner},
33
pinocchio::{
4-
account_info::AccountInfo, program_error::ProgramError, pubkey::pubkey_eq, ProgramResult,
4+
account_info::AccountInfo, hint::unlikely, program_error::ProgramError, pubkey::pubkey_eq,
5+
ProgramResult,
56
},
67
pinocchio_token_interface::{
78
error::TokenError,
@@ -27,15 +28,15 @@ pub fn process_mint_to(
2728
let destination_account =
2829
unsafe { load_mut::<Account>(destination_account_info.borrow_mut_data_unchecked())? };
2930

30-
if destination_account.is_frozen()? {
31+
if unlikely(destination_account.is_frozen()?) {
3132
return Err(TokenError::AccountFrozen.into());
3233
}
3334

34-
if destination_account.is_native() {
35+
if unlikely(destination_account.is_native()) {
3536
return Err(TokenError::NativeNotSupported.into());
3637
}
3738

38-
if !pubkey_eq(mint_info.key(), &destination_account.mint) {
39+
if unlikely(!pubkey_eq(mint_info.key(), &destination_account.mint)) {
3940
return Err(TokenError::MintMismatch.into());
4041
}
4142

0 commit comments

Comments
 (0)