Skip to content

Commit 7511655

Browse files
authored
p-token: Improve code generation (#90)
* Make amount optional * Update import * Use compiler hints * More hints * Update hint import * Revert dependencies * Clean up
1 parent ae4630e commit 7511655

File tree

11 files changed

+55
-26
lines changed

11 files changed

+55
-26
lines changed

p-interface/src/native_mint.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ pub const ID: Pubkey = pinocchio_pubkey::pubkey!("So1111111111111111111111111111
1010

1111
#[inline(always)]
1212
pub fn is_native_mint(mint: &Pubkey) -> bool {
13+
// Avoid using `pubkey_eq` since it increased CU consumption.
1314
mint == &ID
1415
}

p-interface/src/state/account.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use {
22
super::{account_state::AccountState, COption, Initializable, Transmutable},
3-
pinocchio::{hint::likely, program_error::ProgramError, pubkey::Pubkey},
3+
pinocchio::{
4+
hint::likely,
5+
program_error::ProgramError,
6+
pubkey::{pubkey_eq, Pubkey},
7+
},
48
};
59

610
/// Incinerator address.
@@ -147,7 +151,7 @@ impl Account {
147151

148152
#[inline(always)]
149153
pub fn is_owned_by_system_program_or_incinerator(&self) -> bool {
150-
SYSTEM_PROGRAM_ID == self.owner || INCINERATOR_ID == self.owner
154+
pubkey_eq(&SYSTEM_PROGRAM_ID, &self.owner) || pubkey_eq(&INCINERATOR_ID, &self.owner)
151155
}
152156
}
153157

p-token/src/processor/mod.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
use {
22
core::{slice::from_raw_parts, str::from_utf8_unchecked},
33
pinocchio::{
4-
account_info::AccountInfo, hint::unlikely, program_error::ProgramError, pubkey::Pubkey,
5-
syscalls::sol_memcpy_, ProgramResult,
4+
account_info::AccountInfo,
5+
hint::{likely, unlikely},
6+
program_error::ProgramError,
7+
pubkey::{pubkey_eq, Pubkey},
8+
syscalls::sol_memcpy_,
9+
ProgramResult,
610
},
711
pinocchio_token_interface::{
812
error::TokenError,
@@ -79,7 +83,7 @@ const MAX_FORMATTED_DIGITS: usize = u8::MAX as usize + 2;
7983
/// Checks that the account is owned by the expected program.
8084
#[inline(always)]
8185
fn check_account_owner(account_info: &AccountInfo) -> ProgramResult {
82-
if account_info.is_owned_by(&TOKEN_PROGRAM_ID) {
86+
if likely(account_info.is_owned_by(&TOKEN_PROGRAM_ID)) {
8387
Ok(())
8488
} else {
8589
Err(ProgramError::IncorrectProgramId)
@@ -101,7 +105,7 @@ unsafe fn validate_owner(
101105
owner_account_info: &AccountInfo,
102106
signers: &[AccountInfo],
103107
) -> ProgramResult {
104-
if expected_owner != owner_account_info.key() {
108+
if unlikely(!pubkey_eq(expected_owner, owner_account_info.key())) {
105109
return Err(TokenError::OwnerMismatch.into());
106110
}
107111

@@ -121,7 +125,7 @@ unsafe fn validate_owner(
121125

122126
for signer in signers.iter() {
123127
for (position, key) in multisig.signers[0..multisig.n as usize].iter().enumerate() {
124-
if key == signer.key() && !matched[position] {
128+
if pubkey_eq(key, signer.key()) && !matched[position] {
125129
if !signer.is_signer() {
126130
return Err(ProgramError::MissingRequiredSignature);
127131
}

p-token/src/processor/set_authority.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use {
22
super::validate_owner,
33
pinocchio::{
4-
account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey, ProgramResult,
4+
account_info::AccountInfo, hint::likely, program_error::ProgramError, pubkey::Pubkey,
5+
ProgramResult,
56
},
67
pinocchio_token_interface::{
78
error::TokenError,
@@ -22,7 +23,9 @@ pub fn process_set_authority(accounts: &[AccountInfo], instruction_data: &[u8])
2223
let authority_type = AuthorityType::try_from(*instruction_data.get_unchecked(0))?;
2324
let new_authority = if *instruction_data.get_unchecked(1) == 0 {
2425
None
25-
} else if *instruction_data.get_unchecked(1) == 1 && instruction_data.len() >= 34 {
26+
} else if likely(*instruction_data.get_unchecked(1) == 1)
27+
&& instruction_data.len() >= 34
28+
{
2629
Some(&*(instruction_data.as_ptr().add(2) as *const Pubkey))
2730
} else {
2831
return Err(TokenError::InvalidInstruction.into());

p-token/src/processor/shared/approve.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use {
22
crate::processor::validate_owner,
3-
pinocchio::{account_info::AccountInfo, program_error::ProgramError, ProgramResult},
3+
pinocchio::{
4+
account_info::AccountInfo, hint::unlikely, program_error::ProgramError, pubkey::pubkey_eq,
5+
ProgramResult,
6+
},
47
pinocchio_token_interface::{
58
error::TokenError,
69
state::{account::Account, load, load_mut, mint::Mint},
@@ -56,15 +59,15 @@ pub fn process_approve(
5659
}
5760

5861
if let Some((mint_info, expected_decimals)) = expected_mint_info {
59-
if mint_info.key() != &source_account.mint {
62+
if unlikely(!pubkey_eq(mint_info.key(), &source_account.mint)) {
6063
return Err(TokenError::MintMismatch.into());
6164
}
6265

6366
// SAFETY: single immutable borrow of `mint_info` account data and
6467
// `load` validates that the mint is initialized.
6568
let mint = unsafe { load::<Mint>(mint_info.borrow_data_unchecked())? };
6669

67-
if expected_decimals != mint.decimals {
70+
if unlikely(expected_decimals != mint.decimals) {
6871
return Err(TokenError::MintDecimalsMismatch.into());
6972
}
7073
}

p-token/src/processor/shared/burn.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use {
22
crate::processor::{check_account_owner, validate_owner},
3-
pinocchio::{account_info::AccountInfo, program_error::ProgramError, ProgramResult},
3+
pinocchio::{
4+
account_info::AccountInfo, hint::likely, program_error::ProgramError, pubkey::pubkey_eq,
5+
ProgramResult,
6+
},
47
pinocchio_token_interface::{
58
error::TokenError,
69
state::{account::Account, load_mut, mint::Mint},
@@ -42,7 +45,7 @@ pub fn process_burn(
4245
.checked_sub(amount)
4346
.ok_or(TokenError::InsufficientFunds)?;
4447

45-
if mint_info.key() != &source_account.mint {
48+
if !pubkey_eq(mint_info.key(), &source_account.mint) {
4649
return Err(TokenError::MintMismatch.into());
4750
}
4851

@@ -52,9 +55,9 @@ pub fn process_burn(
5255
}
5356
}
5457

55-
if !source_account.is_owned_by_system_program_or_incinerator() {
58+
if likely(!source_account.is_owned_by_system_program_or_incinerator()) {
5659
match source_account.delegate() {
57-
Some(delegate) if authority_info.key() == delegate => {
60+
Some(delegate) if pubkey_eq(authority_info.key(), delegate) => {
5861
// SAFETY: `authority_info` is not currently borrowed.
5962
unsafe { validate_owner(delegate, authority_info, remaining)? };
6063

p-token/src/processor/shared/initialize_mint.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use {
22
pinocchio::{
33
account_info::AccountInfo,
4+
hint::likely,
45
program_error::ProgramError,
56
pubkey::Pubkey,
67
sysvars::{rent::Rent, Sysvar},
@@ -30,7 +31,9 @@ pub fn process_initialize_mint(
3031
let mint_authority = &*(instruction_data.as_ptr().add(1) as *const Pubkey);
3132
let freeze_authority = if *instruction_data.get_unchecked(33) == 0 {
3233
None
33-
} else if *instruction_data.get_unchecked(33) == 1 && instruction_data.len() >= 66 {
34+
} else if likely(*instruction_data.get_unchecked(33) == 1)
35+
&& instruction_data.len() >= 66
36+
{
3437
Some(&*(instruction_data.as_ptr().add(34) as *const Pubkey))
3538
} else {
3639
return Err(TokenError::InvalidInstruction.into());

p-token/src/processor/shared/mint_to.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use {
22
crate::processor::{check_account_owner, validate_owner},
3-
pinocchio::{account_info::AccountInfo, program_error::ProgramError, ProgramResult},
3+
pinocchio::{
4+
account_info::AccountInfo, program_error::ProgramError, pubkey::pubkey_eq, ProgramResult,
5+
},
46
pinocchio_token_interface::{
57
error::TokenError,
68
state::{account::Account, load_mut, mint::Mint},
@@ -33,7 +35,7 @@ pub fn process_mint_to(
3335
return Err(TokenError::NativeNotSupported.into());
3436
}
3537

36-
if mint_info.key() != &destination_account.mint {
38+
if !pubkey_eq(mint_info.key(), &destination_account.mint) {
3739
return Err(TokenError::MintMismatch.into());
3840
}
3941

p-token/src/processor/shared/toggle_account_state.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use {
22
crate::processor::validate_owner,
3-
pinocchio::{account_info::AccountInfo, program_error::ProgramError, ProgramResult},
3+
pinocchio::{
4+
account_info::AccountInfo, program_error::ProgramError, pubkey::pubkey_eq, ProgramResult,
5+
},
46
pinocchio_token_interface::{
57
error::TokenError,
68
state::{account::Account, account_state::AccountState, load, load_mut, mint::Mint},
@@ -24,7 +26,7 @@ pub fn process_toggle_account_state(accounts: &[AccountInfo], freeze: bool) -> P
2426
if source_account.is_native() {
2527
return Err(TokenError::NativeNotSupported.into());
2628
}
27-
if mint_info.key() != &source_account.mint {
29+
if !pubkey_eq(mint_info.key(), &source_account.mint) {
2830
return Err(TokenError::MintMismatch.into());
2931
}
3032

p-token/src/processor/shared/transfer.rs

Lines changed: 4 additions & 3 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, hint::unlikely, program_error::ProgramError, ProgramResult,
4+
account_info::AccountInfo, hint::unlikely, program_error::ProgramError, pubkey::pubkey_eq,
5+
ProgramResult,
56
},
67
pinocchio_token_interface::{
78
error::TokenError,
@@ -103,7 +104,7 @@ pub fn process_transfer(
103104
.checked_sub(amount)
104105
.ok_or(TokenError::InsufficientFunds)?;
105106

106-
if source_account.mint != destination_account.mint {
107+
if !pubkey_eq(&source_account.mint, &destination_account.mint) {
107108
return Err(TokenError::MintMismatch.into());
108109
}
109110

@@ -113,7 +114,7 @@ pub fn process_transfer(
113114
// Validates the mint information.
114115

115116
if let Some((mint_info, decimals)) = expected_mint_info {
116-
if mint_info.key() != &source_account.mint {
117+
if !pubkey_eq(mint_info.key(), &source_account.mint) {
117118
return Err(TokenError::MintMismatch.into());
118119
}
119120

0 commit comments

Comments
 (0)