Likely/unlikely revisited

How does one get likely() and unlikely() on stable?

This post has a snippet from hashbrown, but looking in the latest hashbrown sources this no longer seems to be the case -- instead there's this:

// Branch prediction hint. This is currently only available on nightly but it
// consistently improves performance by 10-15%.
#[cfg(not(feature = "nightly"))]
use core::convert::identity as likely;
#[cfg(not(feature = "nightly"))]
use core::convert::identity as unlikely;
#[cfg(feature = "nightly")]
use core::intrinsics::{likely, unlikely};

How should this be interpreted? There's currently no way to get useful hinting in stable?

Well as of recently they didn't work on nightly either, actually.

Someone was looking at making attributes for this, though, which is the better way to do this.

(After all, we want likely/unlikely match arms as well.)

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.