Skip to content

Commit 443c5b0

Browse files
authored
Rollup merge of rust-lang#151612 - tgross35:cold-path-doc, r=scottmcm
Update documentation for `cold_path`, `likely`, and `unlikely` * Add a note recommending benchmarks to `cold_path`, as other hints have * Note that `cold_path` can be used to implement `likely` and `unlikely` * Update the tracking issue for the `likely_unlikely` feature Tracking issue: rust-lang#136873 Tracking issue: rust-lang#151619
2 parents fb292b7 + 6c0ae93 commit 443c5b0

1 file changed

Lines changed: 38 additions & 2 deletions

File tree

library/core/src/hint.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ pub const fn must_use<T>(value: T) -> T {
658658
/// }
659659
/// }
660660
/// ```
661-
#[unstable(feature = "likely_unlikely", issue = "136873")]
661+
#[unstable(feature = "likely_unlikely", issue = "151619")]
662662
#[inline(always)]
663663
pub const fn likely(b: bool) -> bool {
664664
crate::intrinsics::likely(b)
@@ -708,7 +708,7 @@ pub const fn likely(b: bool) -> bool {
708708
/// }
709709
/// }
710710
/// ```
711-
#[unstable(feature = "likely_unlikely", issue = "136873")]
711+
#[unstable(feature = "likely_unlikely", issue = "151619")]
712712
#[inline(always)]
713713
pub const fn unlikely(b: bool) -> bool {
714714
crate::intrinsics::unlikely(b)
@@ -717,6 +717,10 @@ pub const fn unlikely(b: bool) -> bool {
717717
/// Hints to the compiler that given path is cold, i.e., unlikely to be taken. The compiler may
718718
/// choose to optimize paths that are not cold at the expense of paths that are cold.
719719
///
720+
/// Note that like all hints, the exact effect to codegen is not guaranteed. Using `cold_path`
721+
/// can actually *decrease* performance if the branch is called more than expected. It is advisable
722+
/// to perform benchmarks to tell if this function is useful.
723+
///
720724
/// # Examples
721725
///
722726
/// ```
@@ -741,6 +745,38 @@ pub const fn unlikely(b: bool) -> bool {
741745
/// }
742746
/// }
743747
/// ```
748+
///
749+
/// This can also be used to implement `likely` and `unlikely` helpers to hint the condition rather
750+
/// than the branch:
751+
///
752+
/// ```
753+
/// #![feature(cold_path)]
754+
/// use core::hint::cold_path;
755+
///
756+
/// #[inline(always)]
757+
/// pub const fn likely(b: bool) -> bool {
758+
/// if !b {
759+
/// cold_path();
760+
/// }
761+
/// b
762+
/// }
763+
///
764+
/// #[inline(always)]
765+
/// pub const fn unlikely(b: bool) -> bool {
766+
/// if b {
767+
/// cold_path();
768+
/// }
769+
/// b
770+
/// }
771+
///
772+
/// fn foo(x: i32) {
773+
/// if likely(x > 0) {
774+
/// println!("this branch is likely to be taken");
775+
/// } else {
776+
/// println!("this branch is unlikely to be taken");
777+
/// }
778+
/// }
779+
/// ```
744780
#[unstable(feature = "cold_path", issue = "136873")]
745781
#[inline(always)]
746782
pub const fn cold_path() {

0 commit comments

Comments
 (0)