Reexport likely/unlikely in std::hint#133695
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 |
library/core/src/hint.rs
Outdated
| value | ||
| } | ||
|
|
||
| /// Hints to the compiler that branch condition is likely to be true. |
There was a problem hiding this comment.
| /// 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.
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.
Also it would be good to point people to cold_path (for which we just approved the ACP) for general match and if let.
There was a problem hiding this comment.
Should I also export cold_path() in this PR, or create a new PR for that?
There was a problem hiding this comment.
I also added a test for cold_path(), but not with idiomatic Rust such as if let ... because this will only work after #133852
|
@rustbot author |
This comment has been minimized.
This comment has been minimized.
library/core/src/hint.rs
Outdated
| /// ``` | ||
| /// | ||
| /// | ||
|
|
This comment has been minimized.
This comment has been minimized.
library/core/src/hint.rs
Outdated
| /// When used outside of a branch condition, it may still work if there is a branch close by, but | ||
| /// it is not guaranteed to have any effect. |
library/core/src/hint.rs
Outdated
| /// | ||
| /// | ||
| #[unstable(feature = "likely_unlikely", issue = "26179")] | ||
| #[rustc_nounwind] |
There was a problem hiding this comment.
I don't believe this attribute is appropriate for public functions.
| #[rustc_nounwind] |
library/core/src/hint.rs
Outdated
| /// } | ||
| /// ``` | ||
| #[unstable(feature = "likely_unlikely", issue = "26179")] | ||
| #[rustc_nounwind] |
There was a problem hiding this comment.
I don't believe this attribute is appropriate for public functions.
| #[rustc_nounwind] |
library/core/src/hint.rs
Outdated
| /// } | ||
| /// ``` | ||
| #[unstable(feature = "cold_path", issue = "26179")] | ||
| #[rustc_nounwind] |
There was a problem hiding this comment.
I don't believe this attribute is appropriate for public functions.
| #[rustc_nounwind] |
|
@bors r+ |
Since
likely/unlikelyshould 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