Compiler hint for unlikely/likely for if branches

Here is how hashbrown does to implement likely and unlikely on stable:

#[inline]
#[cold]
fn cold() {}

#[inline]
fn likely(b: bool) -> bool {
    if !b { cold() }
    b
}

#[inline]
fn unlikely(b: bool) -> bool {
    if b { cold() }
    b
}
19 Likes