#hint #intrinsics #optimization #performance #likely

no-std likely_stable

likely and unlikely compiler hints in stable rust

2 releases

0.1.3 Dec 11, 2024
0.1.2 Mar 9, 2021
0.1.1 Mar 9, 2021
0.1.0 Mar 9, 2021

#1 in #hint

Download history 4595/week @ 2024-11-30 4854/week @ 2024-12-07 4619/week @ 2024-12-14 1385/week @ 2024-12-21 1402/week @ 2024-12-28 4050/week @ 2025-01-04 4691/week @ 2025-01-11 3872/week @ 2025-01-18 4217/week @ 2025-01-25 5144/week @ 2025-02-01 5437/week @ 2025-02-08 5309/week @ 2025-02-15 5765/week @ 2025-02-22 8342/week @ 2025-03-01 10318/week @ 2025-03-08 7519/week @ 2025-03-15

33,008 downloads per month
Used in 9 crates (5 directly)

MIT/Apache

31KB
674 lines

LICENSE LICENSE Documentation Crates.io Version

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

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));

Usage

Add this to your Cargo.toml:

[dependencies]
likely_stable = "0.1"

No runtime deps

~10KB