Skip to content

Commit 2d3c09b

Browse files
committed
Use compiler hints
1 parent adfc55e commit 2d3c09b

File tree

14 files changed

+55
-33
lines changed

14 files changed

+55
-33
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ consolidate-commits = false
3636
mollusk-svm = "0.4.0"
3737
mollusk-svm-fuzz-fixture = "0.4.0"
3838
num-traits = "0.2"
39-
pinocchio = "0.9"
39+
pinocchio = { version = "0.9", git = "https://github.com/anza-xyz/pinocchio.git", branch = "febo/pubkey-eq" }
4040
solana-instruction = "2.3.0"
4141
solana-program-error = "2.2.2"
4242
solana-program-option = "2.2.1"

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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use {
22
super::{account_state::AccountState, COption, Initializable, Transmutable},
33
crate::likely,
4-
pinocchio::{program_error::ProgramError, pubkey::Pubkey},
4+
pinocchio::{
5+
program_error::ProgramError,
6+
pubkey::{pubkey_eq, Pubkey},
7+
},
58
};
69

710
/// Incinerator address.
@@ -148,7 +151,7 @@ impl Account {
148151

149152
#[inline(always)]
150153
pub fn is_owned_by_system_program_or_incinerator(&self) -> bool {
151-
SYSTEM_PROGRAM_ID == self.owner || INCINERATOR_ID == self.owner
154+
pubkey_eq(&SYSTEM_PROGRAM_ID, &self.owner) || pubkey_eq(&INCINERATOR_ID, &self.owner)
152155
}
153156
}
154157

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: 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, program_error::ProgramError, pubkey::Pubkey,
5-
syscalls::sol_memcpy_, ProgramResult,
4+
account_info::AccountInfo,
5+
hint::likely,
6+
program_error::ProgramError,
7+
pubkey::{pubkey_eq, Pubkey},
8+
syscalls::sol_memcpy_,
9+
ProgramResult,
610
},
711
pinocchio_token_interface::{
812
error::TokenError,
@@ -78,7 +82,7 @@ const MAX_FORMATTED_DIGITS: usize = u8::MAX as usize + 2;
7882
/// Checks that the account is owned by the expected program.
7983
#[inline(always)]
8084
fn check_account_owner(account_info: &AccountInfo) -> ProgramResult {
81-
if account_info.is_owned_by(&TOKEN_PROGRAM_ID) {
85+
if likely(account_info.is_owned_by(&TOKEN_PROGRAM_ID)) {
8286
Ok(())
8387
} else {
8488
Err(ProgramError::IncorrectProgramId)
@@ -100,7 +104,7 @@ unsafe fn validate_owner(
100104
owner_account_info: &AccountInfo,
101105
signers: &[AccountInfo],
102106
) -> ProgramResult {
103-
if expected_owner != owner_account_info.key() {
107+
if unlikely(!pubkey_eq(expected_owner, owner_account_info.key())) {
104108
return Err(TokenError::OwnerMismatch.into());
105109
}
106110

@@ -120,7 +124,7 @@ unsafe fn validate_owner(
120124

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

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

0 commit comments

Comments
 (0)