Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions p-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,28 @@ pub mod state;
pub mod program {
pinocchio_pubkey::declare_id!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
}


#[cold]
pub const fn unlikely_branch() {}


#[inline(always)]
pub const fn likely(b: bool) -> bool {
if b {
true
} else {
unlikely_branch();
false
}
}

#[inline(always)]
pub const fn unlikely(b: bool) -> bool {
if b {
unlikely_branch();
true
} else {
false
}
}
3 changes: 2 additions & 1 deletion p-interface/src/state/account.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {
super::{account_state::AccountState, COption, Initializable, Transmutable},
crate::likely,
pinocchio::{program_error::ProgramError, pubkey::Pubkey},
};

Expand Down Expand Up @@ -158,6 +159,6 @@ unsafe impl Transmutable for Account {
impl Initializable for Account {
#[inline(always)]
fn is_initialized(&self) -> Result<bool, ProgramError> {
AccountState::try_from(self.state).map(|state| state != AccountState::Uninitialized)
AccountState::try_from(self.state).map(|state| likely(state != AccountState::Uninitialized))
}
}
10 changes: 6 additions & 4 deletions p-token/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use {
multisig::{Multisig, MAX_SIGNERS},
Transmutable,
},
unlikely,
},
};

Expand Down Expand Up @@ -103,9 +104,10 @@ unsafe fn validate_owner(
return Err(TokenError::OwnerMismatch.into());
}

if owner_account_info.data_len() == Multisig::LEN
&& owner_account_info.is_owned_by(&TOKEN_PROGRAM_ID)
{
if unlikely(
owner_account_info.data_len() == Multisig::LEN
&& owner_account_info.is_owned_by(&TOKEN_PROGRAM_ID),
) {
// SAFETY: the caller guarantees that there are no mutable borrows of
// `owner_account_info` account data and the `load` validates that the
// account is initialized; additionally, `Multisig` accounts are only
Expand All @@ -130,7 +132,7 @@ unsafe fn validate_owner(
if num_signers < multisig.m {
return Err(ProgramError::MissingRequiredSignature);
}
} else if !owner_account_info.is_signer() {
} else if unlikely(!owner_account_info.is_signer()) {
return Err(ProgramError::MissingRequiredSignature);
}

Expand Down
3 changes: 2 additions & 1 deletion p-token/src/processor/shared/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use {
pinocchio_token_interface::{
error::TokenError,
state::{account::Account, load, load_mut, load_mut_unchecked, mint::Mint},
unlikely,
},
};

Expand Down Expand Up @@ -76,7 +77,7 @@ pub fn process_transfer(
// - transfers to different accounts: we need to check that the source and
// destination accounts are not frozen, have the same mint, and the source
// account has enough tokens.
let remaining_amount = if self_transfer {
let remaining_amount = if unlikely(self_transfer) {
if source_account.is_frozen()? {
return Err(TokenError::AccountFrozen.into());
}
Expand Down
Loading