From f136afe4b5b32772294450c621929b72962dd60c Mon Sep 17 00:00:00 2001 From: febo Date: Thu, 28 Aug 2025 14:29:19 +0100 Subject: [PATCH] Add hint module --- sdk/pinocchio/src/lib.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/sdk/pinocchio/src/lib.rs b/sdk/pinocchio/src/lib.rs index 6225cd39..4693036c 100644 --- a/sdk/pinocchio/src/lib.rs +++ b/sdk/pinocchio/src/lib.rs @@ -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() {} + + /// 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 + } + } +}