Skip to content

Commit f136afe

Browse files
committed
Add hint module
1 parent b9eb2d4 commit f136afe

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

sdk/pinocchio/src/lib.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,39 @@ pub const SUCCESS: u64 = 0;
264264

265265
/// The result of a program execution.
266266
pub type ProgramResult = Result<(), program_error::ProgramError>;
267+
268+
/// Module with functions to provide hints to the compiler about how code
269+
/// should be optimized.
270+
pub mod hint {
271+
/// A "dummy" function with a hint to the compiler that it is unlikely to be
272+
/// called.
273+
///
274+
/// This function is used as a hint to the compiler to optimize other code paths
275+
/// instead of the one where the function is used.
276+
#[cold]
277+
pub const fn cold_path() {}
278+
279+
/// Return the given `bool` value with a hint to the compiler that `true` is the
280+
/// likely case.
281+
#[inline(always)]
282+
pub const fn likely(b: bool) -> bool {
283+
if b {
284+
true
285+
} else {
286+
cold_path();
287+
false
288+
}
289+
}
290+
291+
/// Return a given `bool` value with a hint to the compiler that `false` is the
292+
/// likely case.
293+
#[inline(always)]
294+
pub const fn unlikely(b: bool) -> bool {
295+
if b {
296+
cold_path();
297+
true
298+
} else {
299+
false
300+
}
301+
}
302+
}

0 commit comments

Comments
 (0)