2018-04-12 21:47:38 +08:00
|
|
|
#![stable(feature = "core_hint", since = "1.27.0")]
|
|
|
|
|
|
|
|
|
|
//! Hints to compiler that affects how code should be emitted or optimized.
|
2024-08-30 16:01:22 -04:00
|
|
|
//!
|
2020-08-30 10:35:50 +08:00
|
|
|
//! Hints may be compile time or runtime.
|
2018-04-12 21:47:38 +08:00
|
|
|
|
2019-04-15 11:23:21 +09:00
|
|
|
use crate::{intrinsics, ub_checks};
|
2018-04-12 21:47:38 +08:00
|
|
|
|
2022-04-17 20:58:36 +02:00
|
|
|
/// Informs the compiler that the site which is calling this function is not
|
|
|
|
|
/// reachable, possibly enabling further optimizations.
|
2018-04-12 21:47:38 +08:00
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
2022-05-06 09:34:41 +02:00
|
|
|
/// Reaching this function is *Undefined Behavior*.
|
|
|
|
|
///
|
|
|
|
|
/// As the compiler assumes that all forms of Undefined Behavior can never
|
|
|
|
|
/// happen, it will eliminate all branches in the surrounding code that it can
|
|
|
|
|
/// determine will invariably lead to a call to `unreachable_unchecked()`.
|
2018-04-12 21:47:38 +08:00
|
|
|
///
|
2022-04-17 20:58:36 +02:00
|
|
|
/// If the assumptions embedded in using this function turn out to be wrong -
|
|
|
|
|
/// that is, if the site which is calling `unreachable_unchecked()` is actually
|
|
|
|
|
/// reachable at runtime - the compiler may have generated nonsensical machine
|
|
|
|
|
/// instructions for this situation, including in seemingly unrelated code,
|
|
|
|
|
/// causing difficult-to-debug problems.
|
2018-04-12 21:47:38 +08:00
|
|
|
///
|
2022-04-17 20:58:36 +02:00
|
|
|
/// Use this function sparingly. Consider using the [`unreachable!`] macro,
|
|
|
|
|
/// which may prevent some optimizations but will safely panic in case it is
|
|
|
|
|
/// actually reached at runtime. Benchmark your code to find out if using
|
|
|
|
|
/// `unreachable_unchecked()` comes with a performance benefit.
|
2018-04-12 21:47:38 +08:00
|
|
|
///
|
2022-04-17 20:58:36 +02:00
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// `unreachable_unchecked()` can be used in situations where the compiler
|
|
|
|
|
/// can't prove invariants that were previously established. Such situations
|
2022-08-18 10:13:37 +08:00
|
|
|
/// have a higher chance of occurring if those invariants are upheld by
|
2022-04-17 20:58:36 +02:00
|
|
|
/// external code that the compiler can't analyze.
|
|
|
|
|
/// ```
|
|
|
|
|
/// fn prepare_inputs(divisors: &mut Vec<u32>) {
|
|
|
|
|
/// // Note to future-self when making changes: The invariant established
|
|
|
|
|
/// // here is NOT checked in `do_computation()`; if this changes, you HAVE
|
|
|
|
|
/// // to change `do_computation()`.
|
|
|
|
|
/// divisors.retain(|divisor| *divisor != 0)
|
|
|
|
|
/// }
|
|
|
|
|
///
|
2022-05-06 09:34:41 +02:00
|
|
|
/// /// # Safety
|
|
|
|
|
/// /// All elements of `divisor` must be non-zero.
|
|
|
|
|
/// unsafe fn do_computation(i: u32, divisors: &[u32]) -> u32 {
|
2022-04-17 20:58:36 +02:00
|
|
|
/// divisors.iter().fold(i, |acc, divisor| {
|
|
|
|
|
/// // Convince the compiler that a division by zero can't happen here
|
|
|
|
|
/// // and a check is not needed below.
|
|
|
|
|
/// if *divisor == 0 {
|
2022-05-06 09:34:41 +02:00
|
|
|
/// // Safety: `divisor` can't be zero because of `prepare_inputs`,
|
2022-04-17 20:58:36 +02:00
|
|
|
/// // but the compiler does not know about this. We *promise*
|
|
|
|
|
/// // that we always call `prepare_inputs`.
|
2022-05-06 09:34:41 +02:00
|
|
|
/// std::hint::unreachable_unchecked()
|
2022-04-17 20:58:36 +02:00
|
|
|
/// }
|
|
|
|
|
/// // The compiler would normally introduce a check here that prevents
|
|
|
|
|
/// // a division by zero. However, if `divisor` was zero, the branch
|
|
|
|
|
/// // above would reach what we explicitly marked as unreachable.
|
|
|
|
|
/// // The compiler concludes that `divisor` can't be zero at this point
|
|
|
|
|
/// // and removes the - now proven useless - check.
|
|
|
|
|
/// acc / divisor
|
|
|
|
|
/// })
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// let mut divisors = vec![2, 0, 4];
|
|
|
|
|
/// prepare_inputs(&mut divisors);
|
2022-05-06 09:34:41 +02:00
|
|
|
/// let result = unsafe {
|
|
|
|
|
/// // Safety: prepare_inputs() guarantees that divisors is non-zero
|
|
|
|
|
/// do_computation(100, &divisors)
|
|
|
|
|
/// };
|
|
|
|
|
/// assert_eq!(result, 12);
|
2022-04-17 20:58:36 +02:00
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
2022-05-06 09:34:41 +02:00
|
|
|
/// While using `unreachable_unchecked()` is perfectly sound in the following
|
2023-04-22 15:58:44 +01:00
|
|
|
/// example, as the compiler is able to prove that a division by zero is not
|
|
|
|
|
/// possible, benchmarking reveals that `unreachable_unchecked()` provides
|
2022-04-17 20:58:36 +02:00
|
|
|
/// no benefit over using [`unreachable!`], while the latter does not introduce
|
|
|
|
|
/// the possibility of Undefined Behavior.
|
2018-04-12 21:47:38 +08:00
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// fn div_1(a: u32, b: u32) -> u32 {
|
|
|
|
|
/// use std::hint::unreachable_unchecked;
|
|
|
|
|
///
|
|
|
|
|
/// // `b.saturating_add(1)` is always positive (not zero),
|
2019-02-09 22:16:58 +00:00
|
|
|
/// // hence `checked_div` will never return `None`.
|
2018-04-12 21:47:38 +08:00
|
|
|
/// // Therefore, the else branch is unreachable.
|
|
|
|
|
/// a.checked_div(b.saturating_add(1))
|
|
|
|
|
/// .unwrap_or_else(|| unsafe { unreachable_unchecked() })
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(div_1(7, 0), 7);
|
|
|
|
|
/// assert_eq!(div_1(9, 1), 4);
|
2020-03-27 22:43:28 +01:00
|
|
|
/// assert_eq!(div_1(11, u32::MAX), 0);
|
2018-04-12 21:47:38 +08:00
|
|
|
/// ```
|
|
|
|
|
#[inline]
|
|
|
|
|
#[stable(feature = "unreachable", since = "1.27.0")]
|
2021-10-04 01:04:17 -04:00
|
|
|
#[rustc_const_stable(feature = "const_unreachable_unchecked", since = "1.57.0")]
|
2022-07-24 14:28:30 -04:00
|
|
|
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
|
2020-07-17 21:57:13 +02:00
|
|
|
pub const unsafe fn unreachable_unchecked() -> ! {
|
2024-03-17 10:12:25 +01:00
|
|
|
ub_checks::assert_unsafe_precondition!(
|
2024-02-26 21:25:27 -05:00
|
|
|
check_language_ub,
|
|
|
|
|
"hint::unreachable_unchecked must never be reached",
|
|
|
|
|
() => false
|
|
|
|
|
);
|
2020-06-22 00:54:46 +02:00
|
|
|
// SAFETY: the safety contract for `intrinsics::unreachable` must
|
|
|
|
|
// be upheld by the caller.
|
2024-02-26 21:25:27 -05:00
|
|
|
unsafe { intrinsics::unreachable() }
|
2018-04-12 21:47:38 +08:00
|
|
|
}
|
2018-12-19 16:43:29 -05:00
|
|
|
|
2023-12-19 11:00:26 -08:00
|
|
|
/// Makes a *soundness* promise to the compiler that `cond` holds.
|
|
|
|
|
///
|
2024-04-07 04:08:25 -04:00
|
|
|
/// This may allow the optimizer to simplify things, but it might also make the generated code
|
|
|
|
|
/// slower. Either way, calling it will most likely make compilation take longer.
|
2023-12-19 11:00:26 -08:00
|
|
|
///
|
2024-04-07 04:08:25 -04:00
|
|
|
/// You may know this from other places as
|
|
|
|
|
/// [`llvm.assume`](https://llvm.org/docs/LangRef.html#llvm-assume-intrinsic) or, in C,
|
|
|
|
|
/// [`__builtin_assume`](https://clang.llvm.org/docs/LanguageExtensions.html#builtin-assume).
|
2023-12-19 11:00:26 -08:00
|
|
|
///
|
2024-04-07 04:08:25 -04:00
|
|
|
/// This promotes a correctness requirement to a soundness requirement. Don't do that without
|
|
|
|
|
/// very good reason.
|
2023-12-19 11:00:26 -08:00
|
|
|
///
|
2024-04-07 04:08:25 -04:00
|
|
|
/// # Usage
|
2023-12-19 11:00:26 -08:00
|
|
|
///
|
2024-04-07 04:08:25 -04:00
|
|
|
/// This is a situational tool for micro-optimization, and is allowed to do nothing. Any use
|
|
|
|
|
/// should come with a repeatable benchmark to show the value, with the expectation to drop it
|
|
|
|
|
/// later should the optimizer get smarter and no longer need it.
|
2023-12-19 11:00:26 -08:00
|
|
|
///
|
2024-04-07 04:08:25 -04:00
|
|
|
/// The more complicated the condition, the less likely this is to be useful. For example,
|
|
|
|
|
/// `assert_unchecked(foo.is_sorted())` is a complex enough value that the compiler is unlikely
|
|
|
|
|
/// to be able to take advantage of it.
|
2023-12-19 11:00:26 -08:00
|
|
|
///
|
2024-04-07 04:08:25 -04:00
|
|
|
/// There's also no need to `assert_unchecked` basic properties of things. For example, the
|
|
|
|
|
/// compiler already knows the range of `count_ones`, so there is no benefit to
|
|
|
|
|
/// `let n = u32::count_ones(x); assert_unchecked(n <= u32::BITS);`.
|
|
|
|
|
///
|
|
|
|
|
/// `assert_unchecked` is logically equivalent to `if !cond { unreachable_unchecked(); }`. If
|
|
|
|
|
/// ever you are tempted to write `assert_unchecked(false)`, you should instead use
|
|
|
|
|
/// [`unreachable_unchecked()`] directly.
|
2023-12-19 11:00:26 -08:00
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
2024-04-07 04:08:25 -04:00
|
|
|
/// `cond` must be `true`. It is immediate UB to call this with `false`.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use core::hint;
|
|
|
|
|
///
|
|
|
|
|
/// /// # Safety
|
|
|
|
|
/// ///
|
|
|
|
|
/// /// `p` must be nonnull and valid
|
|
|
|
|
/// pub unsafe fn next_value(p: *const i32) -> i32 {
|
|
|
|
|
/// // SAFETY: caller invariants guarantee that `p` is not null
|
|
|
|
|
/// unsafe { hint::assert_unchecked(!p.is_null()) }
|
|
|
|
|
///
|
|
|
|
|
/// if p.is_null() {
|
|
|
|
|
/// return -1;
|
|
|
|
|
/// } else {
|
|
|
|
|
/// // SAFETY: caller invariants guarantee that `p` is valid
|
|
|
|
|
/// unsafe { *p + 1 }
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2023-12-19 11:00:26 -08:00
|
|
|
///
|
2024-04-07 04:08:25 -04:00
|
|
|
/// Without the `assert_unchecked`, the above function produces the following with optimizations
|
|
|
|
|
/// enabled:
|
|
|
|
|
///
|
|
|
|
|
/// ```asm
|
|
|
|
|
/// next_value:
|
|
|
|
|
/// test rdi, rdi
|
|
|
|
|
/// je .LBB0_1
|
|
|
|
|
/// mov eax, dword ptr [rdi]
|
|
|
|
|
/// inc eax
|
|
|
|
|
/// ret
|
|
|
|
|
/// .LBB0_1:
|
|
|
|
|
/// mov eax, -1
|
|
|
|
|
/// ret
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// Adding the assertion allows the optimizer to remove the extra check:
|
|
|
|
|
///
|
|
|
|
|
/// ```asm
|
|
|
|
|
/// next_value:
|
|
|
|
|
/// mov eax, dword ptr [rdi]
|
|
|
|
|
/// inc eax
|
|
|
|
|
/// ret
|
|
|
|
|
/// ```
|
2023-12-19 11:00:26 -08:00
|
|
|
///
|
2024-04-07 04:08:25 -04:00
|
|
|
/// This example is quite unlike anything that would be used in the real world: it is redundant
|
2024-07-04 14:56:08 +08:00
|
|
|
/// to put an assertion right next to code that checks the same thing, and dereferencing a
|
2024-04-07 04:08:25 -04:00
|
|
|
/// pointer already has the builtin assumption that it is nonnull. However, it illustrates the
|
|
|
|
|
/// kind of changes the optimizer can make even when the behavior is less obviously related.
|
2024-04-07 04:30:49 -04:00
|
|
|
#[track_caller]
|
2023-12-19 11:00:26 -08:00
|
|
|
#[inline(always)]
|
|
|
|
|
#[doc(alias = "assume")]
|
2024-07-21 08:55:06 -04:00
|
|
|
#[stable(feature = "hint_assert_unchecked", since = "1.81.0")]
|
|
|
|
|
#[rustc_const_stable(feature = "hint_assert_unchecked", since = "1.81.0")]
|
2023-12-19 11:00:26 -08:00
|
|
|
pub const unsafe fn assert_unchecked(cond: bool) {
|
|
|
|
|
// SAFETY: The caller promised `cond` is true.
|
|
|
|
|
unsafe {
|
2024-03-17 10:12:25 +01:00
|
|
|
ub_checks::assert_unsafe_precondition!(
|
2024-02-26 21:25:27 -05:00
|
|
|
check_language_ub,
|
2023-12-19 11:00:26 -08:00
|
|
|
"hint::assert_unchecked must never be called when the condition is false",
|
2024-02-07 10:27:57 -05:00
|
|
|
(cond: bool = cond) => cond,
|
2023-12-19 11:00:26 -08:00
|
|
|
);
|
|
|
|
|
crate::intrinsics::assume(cond);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 10:35:50 +08:00
|
|
|
/// Emits a machine instruction to signal the processor that it is running in
|
|
|
|
|
/// a busy-wait spin-loop ("spin lock").
|
|
|
|
|
///
|
|
|
|
|
/// Upon receiving the spin-loop signal the processor can optimize its behavior by,
|
|
|
|
|
/// for example, saving power or switching hyper-threads.
|
|
|
|
|
///
|
|
|
|
|
/// This function is different from [`thread::yield_now`] which directly
|
|
|
|
|
/// yields to the system's scheduler, whereas `spin_loop` does not interact
|
|
|
|
|
/// with the operating system.
|
|
|
|
|
///
|
|
|
|
|
/// A common use case for `spin_loop` is implementing bounded optimistic
|
|
|
|
|
/// spinning in a CAS loop in synchronization primitives. To avoid problems
|
|
|
|
|
/// like priority inversion, it is strongly recommended that the spin loop is
|
|
|
|
|
/// terminated after a finite amount of iterations and an appropriate blocking
|
|
|
|
|
/// syscall is made.
|
|
|
|
|
///
|
|
|
|
|
/// **Note**: On platforms that do not support receiving spin-loop hints this
|
|
|
|
|
/// function does not do anything at all.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
2018-12-19 16:43:29 -05:00
|
|
|
///
|
2020-08-30 10:35:50 +08:00
|
|
|
/// ```
|
|
|
|
|
/// use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
|
/// use std::sync::Arc;
|
|
|
|
|
/// use std::{hint, thread};
|
|
|
|
|
///
|
|
|
|
|
/// // A shared atomic value that threads will use to coordinate
|
|
|
|
|
/// let live = Arc::new(AtomicBool::new(false));
|
|
|
|
|
///
|
|
|
|
|
/// // In a background thread we'll eventually set the value
|
|
|
|
|
/// let bg_work = {
|
|
|
|
|
/// let live = live.clone();
|
|
|
|
|
/// thread::spawn(move || {
|
|
|
|
|
/// // Do some work, then make the value live
|
|
|
|
|
/// do_some_work();
|
|
|
|
|
/// live.store(true, Ordering::Release);
|
|
|
|
|
/// })
|
|
|
|
|
/// };
|
2019-04-03 10:54:07 +02:00
|
|
|
///
|
2020-08-30 10:35:50 +08:00
|
|
|
/// // Back on our current thread, we wait for the value to be set
|
2021-01-03 19:54:54 +02:00
|
|
|
/// while !live.load(Ordering::Acquire) {
|
2020-08-30 10:35:50 +08:00
|
|
|
/// // The spin loop is a hint to the CPU that we're waiting, but probably
|
|
|
|
|
/// // not for very long
|
|
|
|
|
/// hint::spin_loop();
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// // The value is now set
|
|
|
|
|
/// # fn do_some_work() {}
|
|
|
|
|
/// do_some_work();
|
|
|
|
|
/// bg_work.join()?;
|
|
|
|
|
/// # Ok::<(), Box<dyn core::any::Any + Send + 'static>>(())
|
|
|
|
|
/// ```
|
2019-04-03 10:54:07 +02:00
|
|
|
///
|
2020-08-30 10:35:50 +08:00
|
|
|
/// [`thread::yield_now`]: ../../std/thread/fn.yield_now.html
|
2022-12-07 17:11:17 +01:00
|
|
|
#[inline(always)]
|
2020-08-30 10:35:50 +08:00
|
|
|
#[stable(feature = "renamed_spin_loop", since = "1.49.0")]
|
2018-12-19 16:43:29 -05:00
|
|
|
pub fn spin_loop() {
|
2022-09-06 20:08:04 +02:00
|
|
|
#[cfg(target_arch = "x86")]
|
2019-12-06 20:18:12 -08:00
|
|
|
{
|
2022-09-06 20:08:04 +02:00
|
|
|
// SAFETY: the `cfg` attr ensures that we only execute this on x86 targets.
|
|
|
|
|
unsafe { crate::arch::x86::_mm_pause() };
|
|
|
|
|
}
|
2019-03-16 18:07:58 +01:00
|
|
|
|
2022-09-06 20:08:04 +02:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
|
{
|
|
|
|
|
// SAFETY: the `cfg` attr ensures that we only execute this on x86_64 targets.
|
|
|
|
|
unsafe { crate::arch::x86_64::_mm_pause() };
|
2018-12-19 16:43:29 -05:00
|
|
|
}
|
|
|
|
|
|
2023-09-04 19:53:40 +00:00
|
|
|
#[cfg(target_arch = "riscv32")]
|
2022-01-05 15:28:03 +08:00
|
|
|
{
|
2023-09-04 19:53:40 +00:00
|
|
|
crate::arch::riscv32::pause();
|
2022-01-05 15:28:03 +08:00
|
|
|
}
|
|
|
|
|
|
2023-09-04 19:53:40 +00:00
|
|
|
#[cfg(target_arch = "riscv64")]
|
2019-12-06 20:18:12 -08:00
|
|
|
{
|
2023-09-04 19:53:40 +00:00
|
|
|
crate::arch::riscv64::pause();
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-27 10:49:21 -07:00
|
|
|
#[cfg(any(target_arch = "aarch64", target_arch = "arm64ec"))]
|
2023-09-04 19:53:40 +00:00
|
|
|
{
|
|
|
|
|
// SAFETY: the `cfg` attr ensures that we only execute this on aarch64 targets.
|
|
|
|
|
unsafe { crate::arch::aarch64::__isb(crate::arch::aarch64::SY) };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(all(target_arch = "arm", target_feature = "v6"))]
|
|
|
|
|
{
|
|
|
|
|
// SAFETY: the `cfg` attr ensures that we only execute this on arm targets
|
|
|
|
|
// with support for the v6 feature.
|
|
|
|
|
unsafe { crate::arch::arm::__yield() };
|
2018-12-19 16:43:29 -05:00
|
|
|
}
|
|
|
|
|
}
|
Moves test::black_box to core::hint
This changes removes a cyclic dependency between the "test" and "libtest"
crates, where "libtest" depends on "test" for "black_box", but "test" depends on
"libtest" for everything else.
I've chosen the "hint" module because there seems to be enough consensus in the
discussion of RFC2360 that this module is where such an intrinsic would belong,
but this PR does not implement that RFC! (note: if that RFC ever gets merged,
the API, docs, etc. of this API will need to change).
For backwards compatibility reasons I've chosen to also keep the "test" feature
gate for these instead of adding a new feature gate. If we change the feature
gate, we'll potentially all benchmarks, and while that's something that we could
do, it seems unnecessary to do that now - if RFC2360 gets merged, we'll need to
do that anyways.
2019-03-21 09:15:52 +01:00
|
|
|
|
2019-07-23 10:30:13 +01:00
|
|
|
/// An identity function that *__hints__* to the compiler to be maximally pessimistic about what
|
|
|
|
|
/// `black_box` could do.
|
Moves test::black_box to core::hint
This changes removes a cyclic dependency between the "test" and "libtest"
crates, where "libtest" depends on "test" for "black_box", but "test" depends on
"libtest" for everything else.
I've chosen the "hint" module because there seems to be enough consensus in the
discussion of RFC2360 that this module is where such an intrinsic would belong,
but this PR does not implement that RFC! (note: if that RFC ever gets merged,
the API, docs, etc. of this API will need to change).
For backwards compatibility reasons I've chosen to also keep the "test" feature
gate for these instead of adding a new feature gate. If we change the feature
gate, we'll potentially all benchmarks, and while that's something that we could
do, it seems unnecessary to do that now - if RFC2360 gets merged, we'll need to
do that anyways.
2019-03-21 09:15:52 +01:00
|
|
|
///
|
2019-07-23 10:30:13 +01:00
|
|
|
/// Unlike [`std::convert::identity`], a Rust compiler is encouraged to assume that `black_box` can
|
2020-08-27 01:16:18 +02:00
|
|
|
/// use `dummy` in any possible valid way that Rust code is allowed to without introducing undefined
|
2019-07-23 10:30:13 +01:00
|
|
|
/// behavior in the calling code. This property makes `black_box` useful for writing code in which
|
|
|
|
|
/// certain optimizations are not desired, such as benchmarks.
|
|
|
|
|
///
|
2024-12-05 14:47:41 -05:00
|
|
|
/// <div class="warning">
|
|
|
|
|
///
|
2019-07-23 10:30:13 +01:00
|
|
|
/// Note however, that `black_box` is only (and can only be) provided on a "best-effort" basis. The
|
|
|
|
|
/// extent to which it can block optimisations may vary depending upon the platform and code-gen
|
2023-02-15 16:22:08 +00:00
|
|
|
/// backend used. Programs cannot rely on `black_box` for *correctness*, beyond it behaving as the
|
2023-03-07 18:32:58 -05:00
|
|
|
/// identity function. As such, it **must not be relied upon to control critical program behavior.**
|
2024-06-19 20:01:32 +02:00
|
|
|
/// This also means that this function does not offer any guarantees for cryptographic or security
|
2023-03-07 18:32:58 -05:00
|
|
|
/// purposes.
|
2020-10-05 22:47:52 +08:00
|
|
|
///
|
2024-12-05 14:47:41 -05:00
|
|
|
/// </div>
|
|
|
|
|
///
|
2020-10-05 23:29:43 +08:00
|
|
|
/// [`std::convert::identity`]: crate::convert::identity
|
2022-12-25 18:10:11 -05:00
|
|
|
///
|
|
|
|
|
/// # When is this useful?
|
|
|
|
|
///
|
2023-03-25 19:57:46 -04:00
|
|
|
/// While not suitable in those mission-critical cases, `black_box`'s functionality can generally be
|
2022-12-25 18:10:11 -05:00
|
|
|
/// relied upon for benchmarking, and should be used there. It will try to ensure that the
|
|
|
|
|
/// compiler doesn't optimize away part of the intended test code based on context. For
|
|
|
|
|
/// example:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// fn contains(haystack: &[&str], needle: &str) -> bool {
|
|
|
|
|
/// haystack.iter().any(|x| x == &needle)
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// pub fn benchmark() {
|
|
|
|
|
/// let haystack = vec!["abc", "def", "ghi", "jkl", "mno"];
|
|
|
|
|
/// let needle = "ghi";
|
|
|
|
|
/// for _ in 0..10 {
|
|
|
|
|
/// contains(&haystack, needle);
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// The compiler could theoretically make optimizations like the following:
|
|
|
|
|
///
|
2024-01-04 15:48:22 -08:00
|
|
|
/// - The `needle` and `haystack` do not change, move the call to `contains` outside the loop and
|
2022-12-25 18:10:11 -05:00
|
|
|
/// delete the loop
|
|
|
|
|
/// - Inline `contains`
|
|
|
|
|
/// - `needle` and `haystack` have values known at compile time, `contains` is always true. Remove
|
|
|
|
|
/// the call and replace with `true`
|
|
|
|
|
/// - Nothing is done with the result of `contains`: delete this function call entirely
|
|
|
|
|
/// - `benchmark` now has no purpose: delete this function
|
|
|
|
|
///
|
|
|
|
|
/// It is not likely that all of the above happens, but the compiler is definitely able to make some
|
|
|
|
|
/// optimizations that could result in a very inaccurate benchmark. This is where `black_box` comes
|
|
|
|
|
/// in:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::hint::black_box;
|
|
|
|
|
///
|
2024-12-05 14:47:41 -05:00
|
|
|
/// // Same `contains` function.
|
2022-12-25 18:10:11 -05:00
|
|
|
/// fn contains(haystack: &[&str], needle: &str) -> bool {
|
|
|
|
|
/// haystack.iter().any(|x| x == &needle)
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// pub fn benchmark() {
|
|
|
|
|
/// let haystack = vec!["abc", "def", "ghi", "jkl", "mno"];
|
|
|
|
|
/// let needle = "ghi";
|
|
|
|
|
/// for _ in 0..10 {
|
2024-12-05 14:47:41 -05:00
|
|
|
/// // Force the compiler to run `contains`, even though it is a pure function whose
|
|
|
|
|
/// // results are unused.
|
|
|
|
|
/// black_box(contains(
|
|
|
|
|
/// // Prevent the compiler from making assumptions about the input.
|
|
|
|
|
/// black_box(&haystack),
|
|
|
|
|
/// black_box(needle),
|
|
|
|
|
/// ));
|
2022-12-25 18:10:11 -05:00
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// This essentially tells the compiler to block optimizations across any calls to `black_box`. So,
|
|
|
|
|
/// it now:
|
|
|
|
|
///
|
|
|
|
|
/// - Treats both arguments to `contains` as unpredictable: the body of `contains` can no longer be
|
|
|
|
|
/// optimized based on argument values
|
|
|
|
|
/// - Treats the call to `contains` and its result as volatile: the body of `benchmark` cannot
|
|
|
|
|
/// optimize this away
|
|
|
|
|
///
|
2023-03-26 12:48:01 -04:00
|
|
|
/// This makes our benchmark much more realistic to how the function would actually be used, where
|
2022-12-25 18:10:11 -05:00
|
|
|
/// arguments are usually not known at compile time and the result is used in some way.
|
2024-12-05 14:47:41 -05:00
|
|
|
///
|
|
|
|
|
/// # How to use this
|
|
|
|
|
///
|
|
|
|
|
/// In practice, `black_box` serves two purposes:
|
|
|
|
|
///
|
|
|
|
|
/// 1. It prevents the compiler from making optimizations related to the value returned by `black_box`
|
|
|
|
|
/// 2. It forces the value passed to `black_box` to be calculated, even if the return value of `black_box` is unused
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::hint::black_box;
|
|
|
|
|
///
|
|
|
|
|
/// let zero = 0;
|
|
|
|
|
/// let five = 5;
|
|
|
|
|
///
|
|
|
|
|
/// // The compiler will see this and remove the `* five` call, because it knows that multiplying
|
|
|
|
|
/// // any integer by 0 will result in 0.
|
|
|
|
|
/// let c = zero * five;
|
|
|
|
|
///
|
|
|
|
|
/// // Adding `black_box` here disables the compiler's ability to reason about the first operand in the multiplication.
|
|
|
|
|
/// // It is forced to assume that it can be any possible number, so it cannot remove the `* five`
|
|
|
|
|
/// // operation.
|
|
|
|
|
/// let c = black_box(zero) * five;
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// While most cases will not be as clear-cut as the above example, it still illustrates how
|
|
|
|
|
/// `black_box` can be used. When benchmarking a function, you usually want to wrap its inputs in
|
|
|
|
|
/// `black_box` so the compiler cannot make optimizations that would be unrealistic in real-life
|
|
|
|
|
/// use.
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::hint::black_box;
|
|
|
|
|
///
|
|
|
|
|
/// // This is a simple function that increments its input by 1. Note that it is pure, meaning it
|
|
|
|
|
/// // has no side-effects. This function has no effect if its result is unused. (An example of a
|
|
|
|
|
/// // function *with* side-effects is `println!()`.)
|
|
|
|
|
/// fn increment(x: u8) -> u8 {
|
|
|
|
|
/// x + 1
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// // Here, we call `increment` but discard its result. The compiler, seeing this and knowing that
|
|
|
|
|
/// // `increment` is pure, will eliminate this function call entirely. This may not be desired,
|
|
|
|
|
/// // though, especially if we're trying to track how much time `increment` takes to execute.
|
|
|
|
|
/// let _ = increment(black_box(5));
|
|
|
|
|
///
|
|
|
|
|
/// // Here, we force `increment` to be executed. This is because the compiler treats `black_box`
|
|
|
|
|
/// // as if it has side-effects, and thus must compute its input.
|
|
|
|
|
/// let _ = black_box(increment(black_box(5)));
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// There may be additional situations where you want to wrap the result of a function in
|
|
|
|
|
/// `black_box` to force its execution. This is situational though, and may not have any effect
|
|
|
|
|
/// (such as when the function returns a zero-sized type such as [`()` unit][unit]).
|
|
|
|
|
///
|
|
|
|
|
/// Note that `black_box` has no effect on how its input is treated, only its output. As such,
|
|
|
|
|
/// expressions passed to `black_box` may still be optimized:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::hint::black_box;
|
|
|
|
|
///
|
|
|
|
|
/// // The compiler sees this...
|
|
|
|
|
/// let y = black_box(5 * 10);
|
|
|
|
|
///
|
|
|
|
|
/// // ...as this. As such, it will likely simplify `5 * 10` to just `50`.
|
|
|
|
|
/// let _0 = 5 * 10;
|
|
|
|
|
/// let y = black_box(_0);
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// In the above example, the `5 * 10` expression is considered distinct from the `black_box` call,
|
|
|
|
|
/// and thus is still optimized by the compiler. You can prevent this by moving the multiplication
|
|
|
|
|
/// operation outside of `black_box`:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::hint::black_box;
|
|
|
|
|
///
|
|
|
|
|
/// // No assumptions can be made about either operand, so the multiplication is not optimized out.
|
|
|
|
|
/// let y = black_box(5) * black_box(10);
|
|
|
|
|
/// ```
|
2021-08-10 11:50:33 +01:00
|
|
|
#[inline]
|
2022-10-31 10:43:15 -04:00
|
|
|
#[stable(feature = "bench_black_box", since = "1.66.0")]
|
2021-12-23 20:07:41 +09:00
|
|
|
#[rustc_const_unstable(feature = "const_black_box", issue = "none")]
|
|
|
|
|
pub const fn black_box<T>(dummy: T) -> T {
|
2021-09-08 13:13:47 -04:00
|
|
|
crate::intrinsics::black_box(dummy)
|
Moves test::black_box to core::hint
This changes removes a cyclic dependency between the "test" and "libtest"
crates, where "libtest" depends on "test" for "black_box", but "test" depends on
"libtest" for everything else.
I've chosen the "hint" module because there seems to be enough consensus in the
discussion of RFC2360 that this module is where such an intrinsic would belong,
but this PR does not implement that RFC! (note: if that RFC ever gets merged,
the API, docs, etc. of this API will need to change).
For backwards compatibility reasons I've chosen to also keep the "test" feature
gate for these instead of adding a new feature gate. If we change the feature
gate, we'll potentially all benchmarks, and while that's something that we could
do, it seems unnecessary to do that now - if RFC2360 gets merged, we'll need to
do that anyways.
2019-03-21 09:15:52 +01:00
|
|
|
}
|
2022-03-07 16:37:59 -08:00
|
|
|
|
|
|
|
|
/// An identity function that causes an `unused_must_use` warning to be
|
|
|
|
|
/// triggered if the given value is not used (returned, stored in a variable,
|
|
|
|
|
/// etc) by the caller.
|
|
|
|
|
///
|
|
|
|
|
/// This is primarily intended for use in macro-generated code, in which a
|
|
|
|
|
/// [`#[must_use]` attribute][must_use] either on a type or a function would not
|
|
|
|
|
/// be convenient.
|
|
|
|
|
///
|
|
|
|
|
/// [must_use]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(hint_must_use)]
|
|
|
|
|
///
|
|
|
|
|
/// use core::fmt;
|
|
|
|
|
///
|
|
|
|
|
/// pub struct Error(/* ... */);
|
|
|
|
|
///
|
|
|
|
|
/// #[macro_export]
|
|
|
|
|
/// macro_rules! make_error {
|
|
|
|
|
/// ($($args:expr),*) => {
|
|
|
|
|
/// core::hint::must_use({
|
|
|
|
|
/// let error = $crate::make_error(core::format_args!($($args),*));
|
|
|
|
|
/// error
|
|
|
|
|
/// })
|
|
|
|
|
/// };
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// // Implementation detail of make_error! macro.
|
|
|
|
|
/// #[doc(hidden)]
|
|
|
|
|
/// pub fn make_error(args: fmt::Arguments<'_>) -> Error {
|
|
|
|
|
/// Error(/* ... */)
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// fn demo() -> Option<Error> {
|
|
|
|
|
/// if true {
|
|
|
|
|
/// // Oops, meant to write `return Some(make_error!("..."));`
|
|
|
|
|
/// Some(make_error!("..."));
|
|
|
|
|
/// }
|
|
|
|
|
/// None
|
|
|
|
|
/// }
|
|
|
|
|
/// #
|
|
|
|
|
/// # // Make rustdoc not wrap the whole snippet in fn main, so that $crate::make_error works
|
|
|
|
|
/// # fn main() {}
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// In the above example, we'd like an `unused_must_use` lint to apply to the
|
|
|
|
|
/// value created by `make_error!`. However, neither `#[must_use]` on a struct
|
|
|
|
|
/// nor `#[must_use]` on a function is appropriate here, so the macro expands
|
|
|
|
|
/// using `core::hint::must_use` instead.
|
|
|
|
|
///
|
|
|
|
|
/// - We wouldn't want `#[must_use]` on the `struct Error` because that would
|
|
|
|
|
/// make the following unproblematic code trigger a warning:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// # struct Error;
|
|
|
|
|
/// #
|
|
|
|
|
/// fn f(arg: &str) -> Result<(), Error>
|
|
|
|
|
/// # { Ok(()) }
|
|
|
|
|
///
|
|
|
|
|
/// #[test]
|
|
|
|
|
/// fn t() {
|
|
|
|
|
/// // Assert that `f` returns error if passed an empty string.
|
|
|
|
|
/// // A value of type `Error` is unused here but that's not a problem.
|
|
|
|
|
/// f("").unwrap_err();
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// - Using `#[must_use]` on `fn make_error` can't help because the return value
|
|
|
|
|
/// *is* used, as the right-hand side of a `let` statement. The `let`
|
|
|
|
|
/// statement looks useless but is in fact necessary for ensuring that
|
|
|
|
|
/// temporaries within the `format_args` expansion are not kept alive past the
|
|
|
|
|
/// creation of the `Error`, as keeping them alive past that point can cause
|
|
|
|
|
/// autotrait issues in async code:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// # #![feature(hint_must_use)]
|
|
|
|
|
/// #
|
|
|
|
|
/// # struct Error;
|
|
|
|
|
/// #
|
|
|
|
|
/// # macro_rules! make_error {
|
|
|
|
|
/// # ($($args:expr),*) => {
|
|
|
|
|
/// # core::hint::must_use({
|
|
|
|
|
/// # // If `let` isn't used, then `f()` produces a non-Send future.
|
|
|
|
|
/// # let error = make_error(core::format_args!($($args),*));
|
|
|
|
|
/// # error
|
|
|
|
|
/// # })
|
|
|
|
|
/// # };
|
|
|
|
|
/// # }
|
|
|
|
|
/// #
|
|
|
|
|
/// # fn make_error(args: core::fmt::Arguments<'_>) -> Error {
|
|
|
|
|
/// # Error
|
|
|
|
|
/// # }
|
|
|
|
|
/// #
|
|
|
|
|
/// async fn f() {
|
|
|
|
|
/// // Using `let` inside the make_error expansion causes temporaries like
|
|
|
|
|
/// // `unsync()` to drop at the semicolon of that `let` statement, which
|
|
|
|
|
/// // is prior to the await point. They would otherwise stay around until
|
|
|
|
|
/// // the semicolon on *this* statement, which is after the await point,
|
|
|
|
|
/// // and the enclosing Future would not implement Send.
|
|
|
|
|
/// log(make_error!("look: {:p}", unsync())).await;
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// async fn log(error: Error) {/* ... */}
|
|
|
|
|
///
|
|
|
|
|
/// // Returns something without a Sync impl.
|
|
|
|
|
/// fn unsync() -> *const () {
|
|
|
|
|
/// 0 as *const ()
|
|
|
|
|
/// }
|
|
|
|
|
/// #
|
|
|
|
|
/// # fn test() {
|
|
|
|
|
/// # fn assert_send(_: impl Send) {}
|
|
|
|
|
/// # assert_send(f());
|
|
|
|
|
/// # }
|
|
|
|
|
/// ```
|
|
|
|
|
#[unstable(feature = "hint_must_use", issue = "94745")]
|
|
|
|
|
#[must_use] // <-- :)
|
2022-12-07 17:11:17 +01:00
|
|
|
#[inline(always)]
|
2022-03-07 16:37:59 -08:00
|
|
|
pub const fn must_use<T>(value: T) -> T {
|
|
|
|
|
value
|
|
|
|
|
}
|
2024-12-01 08:27:19 +01:00
|
|
|
|
|
|
|
|
/// Hints to the compiler that a branch condition is likely to be true.
|
|
|
|
|
/// Returns the value passed to it.
|
|
|
|
|
///
|
|
|
|
|
/// It can be used with `if` or boolean `match` expressions.
|
|
|
|
|
///
|
2025-01-20 16:16:46 +01:00
|
|
|
/// When used outside of a branch condition, it may still influence a nearby branch, but
|
|
|
|
|
/// probably will not have any effect.
|
2024-12-01 08:27:19 +01:00
|
|
|
///
|
|
|
|
|
/// It can also be applied to parts of expressions, such as `likely(a) && unlikely(b)`, or to
|
|
|
|
|
/// compound expressions, such as `likely(a && b)`. When applied to compound expressions, it has
|
|
|
|
|
/// the following effect:
|
|
|
|
|
/// ```text
|
|
|
|
|
/// likely(!a) => !unlikely(a)
|
|
|
|
|
/// likely(a && b) => likely(a) && likely(b)
|
|
|
|
|
/// likely(a || b) => a || likely(b)
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// See also the function [`cold_path()`] which may be more appropriate for idiomatic Rust code.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(likely_unlikely)]
|
|
|
|
|
/// use core::hint::likely;
|
|
|
|
|
///
|
|
|
|
|
/// fn foo(x: i32) {
|
|
|
|
|
/// if likely(x > 0) {
|
|
|
|
|
/// println!("this branch is likely to be taken");
|
|
|
|
|
/// } else {
|
|
|
|
|
/// println!("this branch is unlikely to be taken");
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// match likely(x > 0) {
|
|
|
|
|
/// true => println!("this branch is likely to be taken"),
|
|
|
|
|
/// false => println!("this branch is unlikely to be taken"),
|
|
|
|
|
/// }
|
|
|
|
|
///
|
2025-01-20 16:16:46 +01:00
|
|
|
/// // Use outside of a branch condition may still influence a nearby branch
|
2024-12-01 08:27:19 +01:00
|
|
|
/// let cond = likely(x != 0);
|
|
|
|
|
/// if cond {
|
|
|
|
|
/// println!("this branch is likely to be taken");
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
///
|
|
|
|
|
#[unstable(feature = "likely_unlikely", issue = "26179")]
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub const fn likely(b: bool) -> bool {
|
|
|
|
|
crate::intrinsics::likely(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Hints to the compiler that a branch condition is unlikely to be true.
|
|
|
|
|
/// Returns the value passed to it.
|
|
|
|
|
///
|
|
|
|
|
/// It can be used with `if` or boolean `match` expressions.
|
|
|
|
|
///
|
2025-01-20 16:16:46 +01:00
|
|
|
/// When used outside of a branch condition, it may still influence a nearby branch, but
|
|
|
|
|
/// probably will not have any effect.
|
2024-12-01 08:27:19 +01:00
|
|
|
///
|
|
|
|
|
/// It can also be applied to parts of expressions, such as `likely(a) && unlikely(b)`, or to
|
|
|
|
|
/// compound expressions, such as `unlikely(a && b)`. When applied to compound expressions, it has
|
|
|
|
|
/// the following effect:
|
|
|
|
|
/// ```text
|
|
|
|
|
/// unlikely(!a) => !likely(a)
|
|
|
|
|
/// unlikely(a && b) => a && unlikely(b)
|
|
|
|
|
/// unlikely(a || b) => unlikely(a) || unlikely(b)
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// See also the function [`cold_path()`] which may be more appropriate for idiomatic Rust code.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(likely_unlikely)]
|
|
|
|
|
/// use core::hint::unlikely;
|
|
|
|
|
///
|
|
|
|
|
/// fn foo(x: i32) {
|
|
|
|
|
/// if unlikely(x > 0) {
|
|
|
|
|
/// println!("this branch is unlikely to be taken");
|
|
|
|
|
/// } else {
|
|
|
|
|
/// println!("this branch is likely to be taken");
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// match unlikely(x > 0) {
|
|
|
|
|
/// true => println!("this branch is unlikely to be taken"),
|
|
|
|
|
/// false => println!("this branch is likely to be taken"),
|
|
|
|
|
/// }
|
|
|
|
|
///
|
2025-01-20 16:16:46 +01:00
|
|
|
/// // Use outside of a branch condition may still influence a nearby branch
|
2024-12-01 08:27:19 +01:00
|
|
|
/// let cond = unlikely(x != 0);
|
|
|
|
|
/// if cond {
|
|
|
|
|
/// println!("this branch is likely to be taken");
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
#[unstable(feature = "likely_unlikely", issue = "26179")]
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub const fn unlikely(b: bool) -> bool {
|
|
|
|
|
crate::intrinsics::unlikely(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Hints to the compiler that given path is cold, i.e., unlikely to be taken. The compiler may
|
|
|
|
|
/// choose to optimize paths that are not cold at the expense of paths that are cold.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(cold_path)]
|
|
|
|
|
/// use core::hint::cold_path;
|
|
|
|
|
///
|
|
|
|
|
/// fn foo(x: &[i32]) {
|
|
|
|
|
/// if let Some(first) = x.get(0) {
|
|
|
|
|
/// // this is the fast path
|
|
|
|
|
/// } else {
|
|
|
|
|
/// // this path is unlikely
|
|
|
|
|
/// cold_path();
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// fn bar(x: i32) -> i32 {
|
|
|
|
|
/// match x {
|
|
|
|
|
/// 1 => 10,
|
|
|
|
|
/// 2 => 100,
|
|
|
|
|
/// 3 => { cold_path(); 1000 }, // this branch is unlikely
|
|
|
|
|
/// _ => { cold_path(); 10000 }, // this is also unlikely
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
#[unstable(feature = "cold_path", issue = "26179")]
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub const fn cold_path() {
|
|
|
|
|
crate::intrinsics::cold_path()
|
|
|
|
|
}
|