Skip to content
Merged
Changes from all commits
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
36 changes: 36 additions & 0 deletions sdk/pinocchio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,39 @@ pub const SUCCESS: u64 = 0;

/// The result of a program execution.
pub type ProgramResult = Result<(), program_error::ProgramError>;

/// Module with functions to provide hints to the compiler about how code
/// should be optimized.
pub mod hint {
/// A "dummy" function with a hint to the compiler that it is unlikely to be
/// called.
///
/// This function is used as a hint to the compiler to optimize other code paths
/// instead of the one where the function is used.
#[cold]
pub const fn cold_path() {}
Comment on lines +276 to +277
Copy link
Contributor

@rustopian rustopian Aug 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[inline(never)] on cold_path doesn't seem to make a difference - perhaps there are some cases where it would - I'll try a bit more and if so will propose separately.


/// Return the given `bool` value with a hint to the compiler that `true` is the
/// likely case.
#[inline(always)]
pub const fn likely(b: bool) -> bool {
if b {
true
} else {
cold_path();
false
}
}

/// Return a given `bool` value with a hint to the compiler that `false` is the
/// likely case.
#[inline(always)]
pub const fn unlikely(b: bool) -> bool {
if b {
cold_path();
true
} else {
false
}
}
}