memkit 0.1.1-beta.1

Deterministic, intent-driven memory allocation for systems requiring predictable performance
Documentation
//! Branch prediction hints and compiler optimization helpers.
//!
//! These are micro-optimizations that help the compiler generate better code
//! for hot paths by providing hints about likely/unlikely branches.

/// Hint that a condition is likely to be true.
/// 
/// Use this on hot paths where the condition is almost always true.
#[inline(always)]
#[cold]
pub fn cold() {}

/// Hint that a branch is unlikely to be taken.
/// 
/// Returns the value unchanged but hints to the compiler.
#[inline(always)]
pub fn unlikely(b: bool) -> bool {
    if b { cold() }
    b
}

/// Hint that a branch is likely to be taken.
/// 
/// Returns the value unchanged but hints to the compiler.
#[inline(always)]
pub fn likely(b: bool) -> bool {
    if !b { cold() }
    b
}

/// Mark a code path as cold (rarely executed).
/// 
/// Use this for error handling paths.
#[inline(always)]
pub fn cold_path<F: FnOnce() -> T, T>(f: F) -> T {
    cold();
    f()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_hints() {
        assert!(likely(true));
        assert!(!likely(false));
        assert!(unlikely(true));
        assert!(!unlikely(false));
    }
}