Crate branch_hints

Source
Expand description

This crate provides likely and unlikely functions that work as compiler hints for branching in stable rust. They are taken directly from the hashbrown crate, all credit belongs to them. These functions enable constructions like

if likely(condition) {
    // main code
} else {
    // error handling code
}

and they should be optimized away by the compiler.

§Notes

This is a very minimal crate. If you want more comprehensive functionality, take a look at the likely_stable crate.

Verify for yourself with benchmarks if these functions do anything for you, as even the unstable intrinsics sometimes don’t do anything: https://github.com/rust-lang/rust/issues/88767. This implementation of them was removed from hashbrown for this reason: https://github.com/rust-lang/hashbrown/issues/482.

Functions§

likely
Returns the boolean passed to it while hinting to the compiler that it is likely to be true. This function brings likely to stable Rust.
unlikely
Returns the boolean passed to it while hinting to the compiler that it is unlikely to be true. This function brings unlikely to stable Rust.