-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Reexport likely/unlikely in std::hint #133695
base: master
Are you sure you want to change the base?
Conversation
rustbot has assigned @workingjubilee. Use |
This would need lang and libs-api to look over. I'd note that the discussion in the tracking issue seems to be saying that this is the wrong API. In particular a |
This comment has been minimized.
This comment has been minimized.
Yes, I agree with #26179 (comment) that I think the next step would be to create an ACP for |
Basically the current implementation gathers information about which code paths are cold, but this information is only used in select with 2 options. This was all I needed to fix likely and unlikely and I didn't want to make the PR bigger than needed. However common patterns in Rust generate select with more than 2 options. For example this code:
May generate something equivalent to:
|
This comment has been minimized.
This comment has been minimized.
"the compiler currently doesn't support the better option" sounds fixable. |
@workingjubilee I'm all for improving But And since there is RFC I thought they may already be approved |
If we had In any case you should file an ACP at https://github.com/rust-lang/libs-team/issues (it's just an issue template) for adding something new in
Given it was accepted about a decade ago, there is a good chance it would have to be revisited anyway :) |
@tgross35 I'd like to have both likely/unlikely and cold_path, because cold_path can sometimes be awkward. For example, I think:
is more readable than:
I'll look into the ACP and try to create one for all of these functions. |
I agree it's not the most convenient when you have one branch that you want to prioritize rather than deprioritizing all others, for either |
it might be useful to review https://blog.aaronballman.com/2020/08/dont-use-the-likely-or-unlikely-attributes/ which raises some relevant concerns. |
@tgross35 We could implement The current implementation leaves the |
@workingjubilee Most of the points in the article seem to be about the specification for For example when you write: if likely(unlikely(x)) {
true_branch
} else {
false_branch
} It will get expanded to: if x {
cold_path();
true_branch
} else {
cold_path();
false_branch
} And since both paths are cold, they have the same weight and so |
Is there any objection to having |
@hanna-kruppe No objections to |
One concern I have with the current implementation is that it doesn't support nesting properly: it just splits the function into a normal half and a cold half. Consider this example: if A {
} else {
cold_path();
if B {
} else {
cold_path();
}
} At the moment rustc will correctly mark condition |
@Amanieu I need to verify, but this situation should work ok. The code detecting cold blocks goes from bottom to top. Edit: I just tested the example and branch weights are emited for both branches |
No objections from me, for sure. It's very hard to use this particular category of optimization hint correctly, so I am strongly in favor of simply, on nightly, directly exposing all the details while we discuss if it can be used correctly at all. If it seems that |
r? @Amanieu |
@@ -511,3 +511,65 @@ pub const fn black_box<T>(dummy: T) -> T { | |||
pub const fn must_use<T>(value: T) -> T { | |||
value | |||
} | |||
|
|||
/// Hints to the compiler that branch condition is likely to be true. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Hints to the compiler that branch condition is likely to be true. | |
/// Hints to the compiler that a branch condition is likely to be true. |
/// Hints to the compiler that branch condition is likely to be true. | ||
/// Returns the value passed to it. | ||
/// | ||
/// It can be used with `if` or boolean `match` expressions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be expanded a bit? What happens if you use it outside (presumably, nothing)? Can this be used in a &&
or ||
expression?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also it would be good to point people to cold_path
(for which we just approved the ACP) for general match
and if let
.
Since
likely
/unlikely
should be working now, we could reexport them instd::hint
. I'm not sure if this is already approved or if it requires approvalTracking issue: #26179