@@ -529,6 +529,8 @@ pub const fn must_use<T>(value: T) -> T {
529529/// likely(a || b) => a || likely(b)
530530/// ```
531531///
532+ /// See also the function `cold_path()` which may be more appropriate for idiomatic Rust code.
533+ ///
532534/// # Examples
533535///
534536/// ```
@@ -581,6 +583,8 @@ pub const fn likely(b: bool) -> bool {
581583/// unlikely(a || b) => unlikely(a) || unlikely(b)
582584/// ```
583585///
586+ /// See also the function `cold_path()` which may be more appropriate for idiomatic Rust code.
587+ ///
584588/// # Examples
585589///
586590/// ```
@@ -613,3 +617,37 @@ pub const fn likely(b: bool) -> bool {
613617pub const fn unlikely ( b : bool ) -> bool {
614618 crate :: intrinsics:: unlikely ( b)
615619}
620+
621+ /// Hints to the compiler that given path is cold, i.e., unlikely to be taken. The compiler may
622+ /// choose to optimize paths that are not cold at the expense of paths that are cold.
623+ ///
624+ /// # Examples
625+ ///
626+ /// ```
627+ /// #![feature(cold_path)]
628+ /// use core::hint::cold_path;
629+ ///
630+ /// fn foo(x: &[i32]) {
631+ /// if let Some(first) = x.get(0) {
632+ /// // this is the fast path
633+ /// } else {
634+ /// // this path is unlikely
635+ /// cold_path();
636+ /// }
637+ /// }
638+ ///
639+ /// fn bar(x: i32) -> i32 {
640+ /// match x {
641+ /// 1 => 10,
642+ /// 2 => 100,
643+ /// 3 => { cold_path(); 1000 }, // this branch is unlikely
644+ /// _ => { cold_path(); 10000 }, // this is also unlikely
645+ /// }
646+ /// }
647+ /// ```
648+ #[ unstable( feature = "cold_path" , issue = "none" ) ]
649+ #[ rustc_nounwind]
650+ #[ inline( always) ]
651+ pub fn cold_path ( ) {
652+ crate :: intrinsics:: cold_path ( )
653+ }
0 commit comments