Crate likely_stable

Source
Expand description

This crates brings likely and unlikely branch prediction hints to stable rust

Likelyness hint only affects static branch prediction. So a good likelyness indication is the one that indicates the likelyness a branch will be taken when the condition has not been executed since a relatively long time. Profile measurments are very bad indicator on weither to use likely hint or unlikely hint for a given branching statement.

The fact it only affects the static branch predictor makes likely-hood optimizations extremely difficult to benchmark. Indeed, all micro-benchmark needs to loop over the benchmarked code in order to have an accurate timing. But since we loop, the branch predictor use its dynamic prediction ability and the likely-hood ness hint is not used.

use likely_stable::{likely,unlikely};
use rand::random;

if likely(random::<i32>() > 10) {
    println!("likely!")
} else {
    println!("unlikely!")
}

It also provides if_likely and if_unlikely for branch prediction for if let statements.

use likely_stable::if_likely;
use rand::random;

let v = Some(random()).filter(|v:&i32| *v > 10);

if_likely!{let Some(v) = v => {
    println!("likely!")
} else {
    println!("unlikely!")
}};

Moreover traits LikelyBool, LikelyOption and LikelyResult provides likely and unlikely versions of the methods commonly used for types bool, Option and Result

use likely_stable::LikelyOption;
use rand::random;

let v = Some(random()).filter(|v:&i32| *v > 10);

v.map_or_else_likely(
    || println!("unlikely"),
    |v| println!("likely {}",v));

Macros§

if_likely
If statement which inform the compiler that the first branch will be the most taken branch
if_unlikely
If statement which inform the compiler that the first branch will be the most taken branch

Traits§

LikelyBool
Likely trait for bool
LikelyOption
Likely trait for Options
LikelyResult
Likely trait for Result

Functions§

likely
Brings likely to stable rust.
unlikely
Brings unlikely to stable rust.