Likely/unlikely hint on refutable patterns


#1

The unstable likely/unlikely intrinsics, which can be used to hint the compiler and CPU branch prediction heuristics, may currently only be easily used on “raw” if statements that use a boolean condition.

However, refutable patterns such as match arms or if let are effectively branches too, and I cannot think of a fundamental reason why the developer shouldn’t be able to let rustc and LLVM know that some of these branches are more likely than others, so that they can optimize accordingly.

Has this matter been discussed in the past?


#2

IIRC the likely/unlikely intrinsics are intended to be deprecated precisely because they can’t really work in the cases that you mention (see the tracking issue).

An attribute-based approach has higher chances of being successful, but nobody has bothered pursuing that yet. In particular, because we would need most likely to be able to apply attributes to expressions, which is a feature that isn’t stable yet.


#3

Hmm, interestingly it seems that attributes on match arms are stable, as this compiles on stable without even a warning:

fn main() {
    match 0 {
        #[inline] _x => ()
    }
}

#4

@scottmcm nice, I did not knew that.

Maybe such an RFC can already be written then.