From fc2503dbdbf0140757e9a99acbf2c24c13518f04 Mon Sep 17 00:00:00 2001 From: aumetra Date: Fri, 10 Nov 2023 10:41:28 +0100 Subject: [PATCH] Better stable branch hints --- src/macros.rs | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 48cf2755..b9a00823 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1209,21 +1209,45 @@ macro_rules! unlikely { } /// possible compiler hint that a branch is likely +/// +/// Technique borrowed from here: #[cfg(not(feature = "hints"))] #[macro_export] macro_rules! likely { - ($e:expr) => { - $e - }; + ($e:expr) => {{ + #[inline] + #[cold] + fn cold() {} + + let cond = $e; + + if !cond { + cold(); + } + + cond + }}; } /// possible compiler hint that a branch is unlikely +/// +/// Technique borrowed from here: #[cfg(not(feature = "hints"))] #[macro_export] macro_rules! unlikely { - ($e:expr) => { - $e - }; + ($e:expr) => {{ + #[inline] + #[cold] + fn cold() {} + + let cond = $e; + + if cond { + cold(); + } + + cond + }}; } /// static cast to an i8