Skip to content

Commit d12fefa

Browse files
committed
Use compiler hints
1 parent 20e0ffe commit d12fefa

File tree

13 files changed

+54
-29
lines changed

13 files changed

+54
-29
lines changed

Cargo.lock

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

p-interface/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ crate-type = ["rlib"]
1313

1414
[dependencies]
1515
pinocchio = { workspace = true }
16-
pinocchio-pubkey = "0.3"
16+
pinocchio-pubkey = { version = "0.3", git = "https://github.com/anza-xyz/pinocchio.git", branch = "febo/pubkey-eq" }
1717

1818
[dev-dependencies]
1919
strum = "0.27"

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/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ logging = []
1616

1717
[dependencies]
1818
pinocchio = { workspace = true }
19-
pinocchio-log = { version = "0.5", default-features = false }
19+
pinocchio-log = { version = "0.5", default-features = false, git = "https://github.com/anza-xyz/pinocchio.git", branch = "febo/pubkey-eq" }
2020
pinocchio-token-interface = { version = "^0", path = "../p-interface" }
2121

2222
[dev-dependencies]

p-token/src/processor/mod.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
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,
6+
hint::unlikely,
7+
program_error::ProgramError,
8+
pubkey::{pubkey_eq, Pubkey},
9+
syscalls::sol_memcpy_,
10+
ProgramResult,
611
},
712
pinocchio_token_interface::{
813
error::TokenError,
@@ -79,7 +84,7 @@ const MAX_FORMATTED_DIGITS: usize = u8::MAX as usize + 2;
7984
/// Checks that the account is owned by the expected program.
8085
#[inline(always)]
8186
fn check_account_owner(account_info: &AccountInfo) -> ProgramResult {
82-
if account_info.is_owned_by(&TOKEN_PROGRAM_ID) {
87+
if likely(account_info.is_owned_by(&TOKEN_PROGRAM_ID)) {
8388
Ok(())
8489
} else {
8590
Err(ProgramError::IncorrectProgramId)
@@ -101,7 +106,7 @@ unsafe fn validate_owner(
101106
owner_account_info: &AccountInfo,
102107
signers: &[AccountInfo],
103108
) -> ProgramResult {
104-
if expected_owner != owner_account_info.key() {
109+
if unlikely(!pubkey_eq(expected_owner, owner_account_info.key())) {
105110
return Err(TokenError::OwnerMismatch.into());
106111
}
107112

@@ -121,7 +126,7 @@ unsafe fn validate_owner(
121126

122127
for signer in signers.iter() {
123128
for (position, key) in multisig.signers[0..multisig.n as usize].iter().enumerate() {
124-
if key == signer.key() && !matched[position] {
129+
if pubkey_eq(key, signer.key()) && !matched[position] {
125130
if !signer.is_signer() {
126131
return Err(ProgramError::MissingRequiredSignature);
127132
}

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: 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, load, load_mut, mint::Mint},
@@ -56,7 +58,7 @@ pub fn process_approve(
5658
}
5759

5860
if let Some((mint_info, expected_decimals)) = expected_mint_info {
59-
if mint_info.key() != &source_account.mint {
61+
if !pubkey_eq(mint_info.key(), &source_account.mint) {
6062
return Err(TokenError::MintMismatch.into());
6163
}
6264

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());

0 commit comments

Comments
 (0)