Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why the identity function can be used as unlikely function? #482

Closed
YjyJeff opened this issue Nov 9, 2023 · 3 comments
Closed

Why the identity function can be used as unlikely function? #482

YjyJeff opened this issue Nov 9, 2023 · 3 comments

Comments

@YjyJeff
Copy link

YjyJeff commented Nov 9, 2023

According to the code, both likely and unlikely functions use the identity function. Why this function can be used as unlikely? How it works? How the compiler distinguish likely from unlikely?

Thanks in advance

@exrok
Copy link

exrok commented Nov 9, 2023

The compiler can't distinguish them.

Without the nightly feature the likely,unlikely functions do nothing and are simply the identify function as you observed.

But with the nightly feature...

#[cfg(feature = "nightly")]
use core::intrinsics::{likely, unlikely};

the actual intrinsics are used, which are still nightly only: https://doc.rust-lang.org/stable/core/intrinsics/fn.likely.html

@YjyJeff
Copy link
Author

YjyJeff commented Nov 10, 2023

Ok. I am looking for the implementation of the likely and unlikely in stable. Some other links said that hashbrown has a stable implementation of these two functions. I noticed the implementation has changed recently.

So in the current stable rust, the old implementation does not work and we can not achieve it now? @exrok

@exrok
Copy link

exrok commented Nov 12, 2023

Correct that is the my current understanding. I am not aware of a stable implementation of the likely and unlikely. You could try the olds impls from hashbrown (removed here d677fd4) the logic behind seems sounds it is just a question of if they help LLVM consistently.

Sometimes even the unstable intrinsics don't help consistently: rust-lang/rust#88767

Also note, the current likely/unlikely intrinsics are currently marked "perma-unstable", so don't expect them to stabilize soon: rust-lang/rust#26179 (comment)

In terms of stable solutions I think the closest would be PGO: https://doc.rust-lang.org/rustc/profile-guided-optimization.html
But that only helps if your the one building the application and complicates the build procedure.

@YjyJeff YjyJeff closed this as completed Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants