core/hint.rs
1#![stable(feature = "core_hint", since = "1.27.0")]
2
3//! Hints to compiler that affects how code should be emitted or optimized.
4//!
5//! Hints may be compile time or runtime.
6
7use crate::mem::MaybeUninit;
8use crate::{intrinsics, ub_checks};
9
10/// Informs the compiler that the site which is calling this function is not
11/// reachable, possibly enabling further optimizations.
12///
13/// # Safety
14///
15/// Reaching this function is *Undefined Behavior*.
16///
17/// As the compiler assumes that all forms of Undefined Behavior can never
18/// happen, it will eliminate all branches in the surrounding code that it can
19/// determine will invariably lead to a call to `unreachable_unchecked()`.
20///
21/// If the assumptions embedded in using this function turn out to be wrong -
22/// that is, if the site which is calling `unreachable_unchecked()` is actually
23/// reachable at runtime - the compiler may have generated nonsensical machine
24/// instructions for this situation, including in seemingly unrelated code,
25/// causing difficult-to-debug problems.
26///
27/// Use this function sparingly. Consider using the [`unreachable!`] macro,
28/// which may prevent some optimizations but will safely panic in case it is
29/// actually reached at runtime. Benchmark your code to find out if using
30/// `unreachable_unchecked()` comes with a performance benefit.
31///
32/// # Examples
33///
34/// `unreachable_unchecked()` can be used in situations where the compiler
35/// can't prove invariants that were previously established. Such situations
36/// have a higher chance of occurring if those invariants are upheld by
37/// external code that the compiler can't analyze.
38/// ```
39/// fn prepare_inputs(divisors: &mut Vec<u32>) {
40/// // Note to future-self when making changes: The invariant established
41/// // here is NOT checked in `do_computation()`; if this changes, you HAVE
42/// // to change `do_computation()`.
43/// divisors.retain(|divisor| *divisor != 0)
44/// }
45///
46/// /// # Safety
47/// /// All elements of `divisor` must be non-zero.
48/// unsafe fn do_computation(i: u32, divisors: &[u32]) -> u32 {
49/// divisors.iter().fold(i, |acc, divisor| {
50/// // Convince the compiler that a division by zero can't happen here
51/// // and a check is not needed below.
52/// if *divisor == 0 {
53/// // Safety: `divisor` can't be zero because of `prepare_inputs`,
54/// // but the compiler does not know about this. We *promise*
55/// // that we always call `prepare_inputs`.
56/// unsafe { std::hint::unreachable_unchecked() }
57/// }
58/// // The compiler would normally introduce a check here that prevents
59/// // a division by zero. However, if `divisor` was zero, the branch
60/// // above would reach what we explicitly marked as unreachable.
61/// // The compiler concludes that `divisor` can't be zero at this point
62/// // and removes the - now proven useless - check.
63/// acc / divisor
64/// })
65/// }
66///
67/// let mut divisors = vec![2, 0, 4];
68/// prepare_inputs(&mut divisors);
69/// let result = unsafe {
70/// // Safety: prepare_inputs() guarantees that divisors is non-zero
71/// do_computation(100, &divisors)
72/// };
73/// assert_eq!(result, 12);
74///
75/// ```
76///
77/// While using `unreachable_unchecked()` is perfectly sound in the following
78/// example, as the compiler is able to prove that a division by zero is not
79/// possible, benchmarking reveals that `unreachable_unchecked()` provides
80/// no benefit over using [`unreachable!`], while the latter does not introduce
81/// the possibility of Undefined Behavior.
82///
83/// ```
84/// fn div_1(a: u32, b: u32) -> u32 {
85/// use std::hint::unreachable_unchecked;
86///
87/// // `b.saturating_add(1)` is always positive (not zero),
88/// // hence `checked_div` will never return `None`.
89/// // Therefore, the else branch is unreachable.
90/// a.checked_div(b.saturating_add(1))
91/// .unwrap_or_else(|| unsafe { unreachable_unchecked() })
92/// }
93///
94/// assert_eq!(div_1(7, 0), 7);
95/// assert_eq!(div_1(9, 1), 4);
96/// assert_eq!(div_1(11, u32::MAX), 0);
97/// ```
98#[inline]
99#[stable(feature = "unreachable", since = "1.27.0")]
100#[rustc_const_stable(feature = "const_unreachable_unchecked", since = "1.57.0")]
101#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
102pub const unsafe fn unreachable_unchecked() -> ! {
103 ub_checks::assert_unsafe_precondition!(
104 check_language_ub,
105 "hint::unreachable_unchecked must never be reached",
106 () => false
107 );
108 // SAFETY: the safety contract for `intrinsics::unreachable` must
109 // be upheld by the caller.
110 unsafe { intrinsics::unreachable() }
111}
112
113/// Makes a *soundness* promise to the compiler that `cond` holds.
114///
115/// This may allow the optimizer to simplify things, but it might also make the generated code
116/// slower. Either way, calling it will most likely make compilation take longer.
117///
118/// You may know this from other places as
119/// [`llvm.assume`](http://llvm.org/docs/LangRef.html#llvm-assume-intrinsic) or, in C,
120/// [`__builtin_assume`](http://clang.llvm.org/docs/LanguageExtensions.html#builtin-assume).
121///
122/// This promotes a correctness requirement to a soundness requirement. Don't do that without
123/// very good reason.
124///
125/// # Usage
126///
127/// This is a situational tool for micro-optimization, and is allowed to do nothing. Any use
128/// should come with a repeatable benchmark to show the value, with the expectation to drop it
129/// later should the optimizer get smarter and no longer need it.
130///
131/// The more complicated the condition, the less likely this is to be useful. For example,
132/// `assert_unchecked(foo.is_sorted())` is a complex enough value that the compiler is unlikely
133/// to be able to take advantage of it.
134///
135/// There's also no need to `assert_unchecked` basic properties of things. For example, the
136/// compiler already knows the range of `count_ones`, so there is no benefit to
137/// `let n = u32::count_ones(x); assert_unchecked(n <= u32::BITS);`.
138///
139/// `assert_unchecked` is logically equivalent to `if !cond { unreachable_unchecked(); }`. If
140/// ever you are tempted to write `assert_unchecked(false)`, you should instead use
141/// [`unreachable_unchecked()`] directly.
142///
143/// # Safety
144///
145/// `cond` must be `true`. It is immediate UB to call this with `false`.
146///
147/// # Example
148///
149/// ```
150/// use core::hint;
151///
152/// /// # Safety
153/// ///
154/// /// `p` must be nonnull and valid
155/// pub unsafe fn next_value(p: *const i32) -> i32 {
156/// // SAFETY: caller invariants guarantee that `p` is not null
157/// unsafe { hint::assert_unchecked(!p.is_null()) }
158///
159/// if p.is_null() {
160/// return -1;
161/// } else {
162/// // SAFETY: caller invariants guarantee that `p` is valid
163/// unsafe { *p + 1 }
164/// }
165/// }
166/// ```
167///
168/// Without the `assert_unchecked`, the above function produces the following with optimizations
169/// enabled:
170///
171/// ```asm
172/// next_value:
173/// test rdi, rdi
174/// je .LBB0_1
175/// mov eax, dword ptr [rdi]
176/// inc eax
177/// ret
178/// .LBB0_1:
179/// mov eax, -1
180/// ret
181/// ```
182///
183/// Adding the assertion allows the optimizer to remove the extra check:
184///
185/// ```asm
186/// next_value:
187/// mov eax, dword ptr [rdi]
188/// inc eax
189/// ret
190/// ```
191///
192/// This example is quite unlike anything that would be used in the real world: it is redundant
193/// to put an assertion right next to code that checks the same thing, and dereferencing a
194/// pointer already has the builtin assumption that it is nonnull. However, it illustrates the
195/// kind of changes the optimizer can make even when the behavior is less obviously related.
196#[track_caller]
197#[inline(always)]
198#[doc(alias = "assume")]
199#[stable(feature = "hint_assert_unchecked", since = "1.81.0")]
200#[rustc_const_stable(feature = "hint_assert_unchecked", since = "1.81.0")]
201pub const unsafe fn assert_unchecked(cond: bool) {
202 // SAFETY: The caller promised `cond` is true.
203 unsafe {
204 ub_checks::assert_unsafe_precondition!(
205 check_language_ub,
206 "hint::assert_unchecked must never be called when the condition is false",
207 (cond: bool = cond) => cond,
208 );
209 crate::intrinsics::assume(cond);
210 }
211}
212
213/// Emits a machine instruction to signal the processor that it is running in
214/// a busy-wait spin-loop ("spin lock").
215///
216/// Upon receiving the spin-loop signal the processor can optimize its behavior by,
217/// for example, saving power or switching hyper-threads.
218///
219/// This function is different from [`thread::yield_now`] which directly
220/// yields to the system's scheduler, whereas `spin_loop` does not interact
221/// with the operating system.
222///
223/// A common use case for `spin_loop` is implementing bounded optimistic
224/// spinning in a CAS loop in synchronization primitives. To avoid problems
225/// like priority inversion, it is strongly recommended that the spin loop is
226/// terminated after a finite amount of iterations and an appropriate blocking
227/// syscall is made.
228///
229/// **Note**: On platforms that do not support receiving spin-loop hints this
230/// function does not do anything at all.
231///
232/// # Examples
233///
234/// ```
235/// use std::sync::atomic::{AtomicBool, Ordering};
236/// use std::sync::Arc;
237/// use std::{hint, thread};
238///
239/// // A shared atomic value that threads will use to coordinate
240/// let live = Arc::new(AtomicBool::new(false));
241///
242/// // In a background thread we'll eventually set the value
243/// let bg_work = {
244/// let live = live.clone();
245/// thread::spawn(move || {
246/// // Do some work, then make the value live
247/// do_some_work();
248/// live.store(true, Ordering::Release);
249/// })
250/// };
251///
252/// // Back on our current thread, we wait for the value to be set
253/// while !live.load(Ordering::Acquire) {
254/// // The spin loop is a hint to the CPU that we're waiting, but probably
255/// // not for very long
256/// hint::spin_loop();
257/// }
258///
259/// // The value is now set
260/// # fn do_some_work() {}
261/// do_some_work();
262/// bg_work.join()?;
263/// # Ok::<(), Box<dyn core::any::Any + Send + 'static>>(())
264/// ```
265///
266/// [`thread::yield_now`]: ../../std/thread/fn.yield_now.html
267#[inline(always)]
268#[stable(feature = "renamed_spin_loop", since = "1.49.0")]
269pub fn spin_loop() {
270 #[cfg(target_arch = "x86")]
271 {
272 // SAFETY: the `cfg` attr ensures that we only execute this on x86 targets.
273 unsafe { crate::arch::x86::_mm_pause() };
274 }
275
276 #[cfg(target_arch = "x86_64")]
277 {
278 // SAFETY: the `cfg` attr ensures that we only execute this on x86_64 targets.
279 unsafe { crate::arch::x86_64::_mm_pause() };
280 }
281
282 #[cfg(target_arch = "riscv32")]
283 {
284 crate::arch::riscv32::pause();
285 }
286
287 #[cfg(target_arch = "riscv64")]
288 {
289 crate::arch::riscv64::pause();
290 }
291
292 #[cfg(any(target_arch = "aarch64", target_arch = "arm64ec"))]
293 {
294 // SAFETY: the `cfg` attr ensures that we only execute this on aarch64 targets.
295 unsafe { crate::arch::aarch64::__isb(crate::arch::aarch64::SY) };
296 }
297
298 #[cfg(all(target_arch = "arm", target_feature = "v6"))]
299 {
300 // SAFETY: the `cfg` attr ensures that we only execute this on arm targets
301 // with support for the v6 feature.
302 unsafe { crate::arch::arm::__yield() };
303 }
304}
305
306/// An identity function that *__hints__* to the compiler to be maximally pessimistic about what
307/// `black_box` could do.
308///
309/// Unlike [`std::convert::identity`], a Rust compiler is encouraged to assume that `black_box` can
310/// use `dummy` in any possible valid way that Rust code is allowed to without introducing undefined
311/// behavior in the calling code. This property makes `black_box` useful for writing code in which
312/// certain optimizations are not desired, such as benchmarks.
313///
314/// <div class="warning">
315///
316/// Note however, that `black_box` is only (and can only be) provided on a "best-effort" basis. The
317/// extent to which it can block optimisations may vary depending upon the platform and code-gen
318/// backend used. Programs cannot rely on `black_box` for *correctness*, beyond it behaving as the
319/// identity function. As such, it **must not be relied upon to control critical program behavior.**
320/// This also means that this function does not offer any guarantees for cryptographic or security
321/// purposes.
322///
323/// </div>
324///
325/// [`std::convert::identity`]: crate::convert::identity
326///
327/// # When is this useful?
328///
329/// While not suitable in those mission-critical cases, `black_box`'s functionality can generally be
330/// relied upon for benchmarking, and should be used there. It will try to ensure that the
331/// compiler doesn't optimize away part of the intended test code based on context. For
332/// example:
333///
334/// ```
335/// fn contains(haystack: &[&str], needle: &str) -> bool {
336/// haystack.iter().any(|x| x == &needle)
337/// }
338///
339/// pub fn benchmark() {
340/// let haystack = vec!["abc", "def", "ghi", "jkl", "mno"];
341/// let needle = "ghi";
342/// for _ in 0..10 {
343/// contains(&haystack, needle);
344/// }
345/// }
346/// ```
347///
348/// The compiler could theoretically make optimizations like the following:
349///
350/// - The `needle` and `haystack` do not change, move the call to `contains` outside the loop and
351/// delete the loop
352/// - Inline `contains`
353/// - `needle` and `haystack` have values known at compile time, `contains` is always true. Remove
354/// the call and replace with `true`
355/// - Nothing is done with the result of `contains`: delete this function call entirely
356/// - `benchmark` now has no purpose: delete this function
357///
358/// It is not likely that all of the above happens, but the compiler is definitely able to make some
359/// optimizations that could result in a very inaccurate benchmark. This is where `black_box` comes
360/// in:
361///
362/// ```
363/// use std::hint::black_box;
364///
365/// // Same `contains` function.
366/// fn contains(haystack: &[&str], needle: &str) -> bool {
367/// haystack.iter().any(|x| x == &needle)
368/// }
369///
370/// pub fn benchmark() {
371/// let haystack = vec!["abc", "def", "ghi", "jkl", "mno"];
372/// let needle = "ghi";
373/// for _ in 0..10 {
374/// // Force the compiler to run `contains`, even though it is a pure function whose
375/// // results are unused.
376/// black_box(contains(
377/// // Prevent the compiler from making assumptions about the input.
378/// black_box(&haystack),
379/// black_box(needle),
380/// ));
381/// }
382/// }
383/// ```
384///
385/// This essentially tells the compiler to block optimizations across any calls to `black_box`. So,
386/// it now:
387///
388/// - Treats both arguments to `contains` as unpredictable: the body of `contains` can no longer be
389/// optimized based on argument values
390/// - Treats the call to `contains` and its result as volatile: the body of `benchmark` cannot
391/// optimize this away
392///
393/// This makes our benchmark much more realistic to how the function would actually be used, where
394/// arguments are usually not known at compile time and the result is used in some way.
395///
396/// # How to use this
397///
398/// In practice, `black_box` serves two purposes:
399///
400/// 1. It prevents the compiler from making optimizations related to the value returned by `black_box`
401/// 2. It forces the value passed to `black_box` to be calculated, even if the return value of `black_box` is unused
402///
403/// ```
404/// use std::hint::black_box;
405///
406/// let zero = 0;
407/// let five = 5;
408///
409/// // The compiler will see this and remove the `* five` call, because it knows that multiplying
410/// // any integer by 0 will result in 0.
411/// let c = zero * five;
412///
413/// // Adding `black_box` here disables the compiler's ability to reason about the first operand in the multiplication.
414/// // It is forced to assume that it can be any possible number, so it cannot remove the `* five`
415/// // operation.
416/// let c = black_box(zero) * five;
417/// ```
418///
419/// While most cases will not be as clear-cut as the above example, it still illustrates how
420/// `black_box` can be used. When benchmarking a function, you usually want to wrap its inputs in
421/// `black_box` so the compiler cannot make optimizations that would be unrealistic in real-life
422/// use.
423///
424/// ```
425/// use std::hint::black_box;
426///
427/// // This is a simple function that increments its input by 1. Note that it is pure, meaning it
428/// // has no side-effects. This function has no effect if its result is unused. (An example of a
429/// // function *with* side-effects is `println!()`.)
430/// fn increment(x: u8) -> u8 {
431/// x + 1
432/// }
433///
434/// // Here, we call `increment` but discard its result. The compiler, seeing this and knowing that
435/// // `increment` is pure, will eliminate this function call entirely. This may not be desired,
436/// // though, especially if we're trying to track how much time `increment` takes to execute.
437/// let _ = increment(black_box(5));
438///
439/// // Here, we force `increment` to be executed. This is because the compiler treats `black_box`
440/// // as if it has side-effects, and thus must compute its input.
441/// let _ = black_box(increment(black_box(5)));
442/// ```
443///
444/// There may be additional situations where you want to wrap the result of a function in
445/// `black_box` to force its execution. This is situational though, and may not have any effect
446/// (such as when the function returns a zero-sized type such as [`()` unit][unit]).
447///
448/// Note that `black_box` has no effect on how its input is treated, only its output. As such,
449/// expressions passed to `black_box` may still be optimized:
450///
451/// ```
452/// use std::hint::black_box;
453///
454/// // The compiler sees this...
455/// let y = black_box(5 * 10);
456///
457/// // ...as this. As such, it will likely simplify `5 * 10` to just `50`.
458/// let _0 = 5 * 10;
459/// let y = black_box(_0);
460/// ```
461///
462/// In the above example, the `5 * 10` expression is considered distinct from the `black_box` call,
463/// and thus is still optimized by the compiler. You can prevent this by moving the multiplication
464/// operation outside of `black_box`:
465///
466/// ```
467/// use std::hint::black_box;
468///
469/// // No assumptions can be made about either operand, so the multiplication is not optimized out.
470/// let y = black_box(5) * black_box(10);
471/// ```
472///
473/// During constant evaluation, `black_box` is treated as a no-op.
474#[inline]
475#[stable(feature = "bench_black_box", since = "1.66.0")]
476#[rustc_const_stable(feature = "const_black_box", since = "1.86.0")]
477pub const fn black_box<T>(dummy: T) -> T {
478 crate::intrinsics::black_box(dummy)
479}
480
481/// An identity function that causes an `unused_must_use` warning to be
482/// triggered if the given value is not used (returned, stored in a variable,
483/// etc) by the caller.
484///
485/// This is primarily intended for use in macro-generated code, in which a
486/// [`#[must_use]` attribute][must_use] either on a type or a function would not
487/// be convenient.
488///
489/// [must_use]: http://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute
490///
491/// # Example
492///
493/// ```
494/// #![feature(hint_must_use)]
495///
496/// use core::fmt;
497///
498/// pub struct Error(/* ... */);
499///
500/// #[macro_export]
501/// macro_rules! make_error {
502/// ($($args:expr),*) => {
503/// core::hint::must_use({
504/// let error = $crate::make_error(core::format_args!($($args),*));
505/// error
506/// })
507/// };
508/// }
509///
510/// // Implementation detail of make_error! macro.
511/// #[doc(hidden)]
512/// pub fn make_error(args: fmt::Arguments<'_>) -> Error {
513/// Error(/* ... */)
514/// }
515///
516/// fn demo() -> Option<Error> {
517/// if true {
518/// // Oops, meant to write `return Some(make_error!("..."));`
519/// Some(make_error!("..."));
520/// }
521/// None
522/// }
523/// #
524/// # // Make rustdoc not wrap the whole snippet in fn main, so that $crate::make_error works
525/// # fn main() {}
526/// ```
527///
528/// In the above example, we'd like an `unused_must_use` lint to apply to the
529/// value created by `make_error!`. However, neither `#[must_use]` on a struct
530/// nor `#[must_use]` on a function is appropriate here, so the macro expands
531/// using `core::hint::must_use` instead.
532///
533/// - We wouldn't want `#[must_use]` on the `struct Error` because that would
534/// make the following unproblematic code trigger a warning:
535///
536/// ```
537/// # struct Error;
538/// #
539/// fn f(arg: &str) -> Result<(), Error>
540/// # { Ok(()) }
541///
542/// #[test]
543/// fn t() {
544/// // Assert that `f` returns error if passed an empty string.
545/// // A value of type `Error` is unused here but that's not a problem.
546/// f("").unwrap_err();
547/// }
548/// ```
549///
550/// - Using `#[must_use]` on `fn make_error` can't help because the return value
551/// *is* used, as the right-hand side of a `let` statement. The `let`
552/// statement looks useless but is in fact necessary for ensuring that
553/// temporaries within the `format_args` expansion are not kept alive past the
554/// creation of the `Error`, as keeping them alive past that point can cause
555/// autotrait issues in async code:
556///
557/// ```
558/// # #![feature(hint_must_use)]
559/// #
560/// # struct Error;
561/// #
562/// # macro_rules! make_error {
563/// # ($($args:expr),*) => {
564/// # core::hint::must_use({
565/// # // If `let` isn't used, then `f()` produces a non-Send future.
566/// # let error = make_error(core::format_args!($($args),*));
567/// # error
568/// # })
569/// # };
570/// # }
571/// #
572/// # fn make_error(args: core::fmt::Arguments<'_>) -> Error {
573/// # Error
574/// # }
575/// #
576/// async fn f() {
577/// // Using `let` inside the make_error expansion causes temporaries like
578/// // `unsync()` to drop at the semicolon of that `let` statement, which
579/// // is prior to the await point. They would otherwise stay around until
580/// // the semicolon on *this* statement, which is after the await point,
581/// // and the enclosing Future would not implement Send.
582/// log(make_error!("look: {:p}", unsync())).await;
583/// }
584///
585/// async fn log(error: Error) {/* ... */}
586///
587/// // Returns something without a Sync impl.
588/// fn unsync() -> *const () {
589/// 0 as *const ()
590/// }
591/// #
592/// # fn test() {
593/// # fn assert_send(_: impl Send) {}
594/// # assert_send(f());
595/// # }
596/// ```
597#[unstable(feature = "hint_must_use", issue = "94745")]
598#[must_use] // <-- :)
599#[inline(always)]
600pub const fn must_use<T>(value: T) -> T {
601 value
602}
603
604/// Hints to the compiler that a branch condition is likely to be true.
605/// Returns the value passed to it.
606///
607/// It can be used with `if` or boolean `match` expressions.
608///
609/// When used outside of a branch condition, it may still influence a nearby branch, but
610/// probably will not have any effect.
611///
612/// It can also be applied to parts of expressions, such as `likely(a) && unlikely(b)`, or to
613/// compound expressions, such as `likely(a && b)`. When applied to compound expressions, it has
614/// the following effect:
615/// ```text
616/// likely(!a) => !unlikely(a)
617/// likely(a && b) => likely(a) && likely(b)
618/// likely(a || b) => a || likely(b)
619/// ```
620///
621/// See also the function [`cold_path()`] which may be more appropriate for idiomatic Rust code.
622///
623/// # Examples
624///
625/// ```
626/// #![feature(likely_unlikely)]
627/// use core::hint::likely;
628///
629/// fn foo(x: i32) {
630/// if likely(x > 0) {
631/// println!("this branch is likely to be taken");
632/// } else {
633/// println!("this branch is unlikely to be taken");
634/// }
635///
636/// match likely(x > 0) {
637/// true => println!("this branch is likely to be taken"),
638/// false => println!("this branch is unlikely to be taken"),
639/// }
640///
641/// // Use outside of a branch condition may still influence a nearby branch
642/// let cond = likely(x != 0);
643/// if cond {
644/// println!("this branch is likely to be taken");
645/// }
646/// }
647/// ```
648///
649///
650#[unstable(feature = "likely_unlikely", issue = "136873")]
651#[inline(always)]
652pub const fn likely(b: bool) -> bool {
653 crate::intrinsics::likely(b)
654}
655
656/// Hints to the compiler that a branch condition is unlikely to be true.
657/// Returns the value passed to it.
658///
659/// It can be used with `if` or boolean `match` expressions.
660///
661/// When used outside of a branch condition, it may still influence a nearby branch, but
662/// probably will not have any effect.
663///
664/// It can also be applied to parts of expressions, such as `likely(a) && unlikely(b)`, or to
665/// compound expressions, such as `unlikely(a && b)`. When applied to compound expressions, it has
666/// the following effect:
667/// ```text
668/// unlikely(!a) => !likely(a)
669/// unlikely(a && b) => a && unlikely(b)
670/// unlikely(a || b) => unlikely(a) || unlikely(b)
671/// ```
672///
673/// See also the function [`cold_path()`] which may be more appropriate for idiomatic Rust code.
674///
675/// # Examples
676///
677/// ```
678/// #![feature(likely_unlikely)]
679/// use core::hint::unlikely;
680///
681/// fn foo(x: i32) {
682/// if unlikely(x > 0) {
683/// println!("this branch is unlikely to be taken");
684/// } else {
685/// println!("this branch is likely to be taken");
686/// }
687///
688/// match unlikely(x > 0) {
689/// true => println!("this branch is unlikely to be taken"),
690/// false => println!("this branch is likely to be taken"),
691/// }
692///
693/// // Use outside of a branch condition may still influence a nearby branch
694/// let cond = unlikely(x != 0);
695/// if cond {
696/// println!("this branch is likely to be taken");
697/// }
698/// }
699/// ```
700#[unstable(feature = "likely_unlikely", issue = "136873")]
701#[inline(always)]
702pub const fn unlikely(b: bool) -> bool {
703 crate::intrinsics::unlikely(b)
704}
705
706/// Hints to the compiler that given path is cold, i.e., unlikely to be taken. The compiler may
707/// choose to optimize paths that are not cold at the expense of paths that are cold.
708///
709/// # Examples
710///
711/// ```
712/// #![feature(cold_path)]
713/// use core::hint::cold_path;
714///
715/// fn foo(x: &[i32]) {
716/// if let Some(first) = x.get(0) {
717/// // this is the fast path
718/// } else {
719/// // this path is unlikely
720/// cold_path();
721/// }
722/// }
723///
724/// fn bar(x: i32) -> i32 {
725/// match x {
726/// 1 => 10,
727/// 2 => 100,
728/// 3 => { cold_path(); 1000 }, // this branch is unlikely
729/// _ => { cold_path(); 10000 }, // this is also unlikely
730/// }
731/// }
732/// ```
733#[unstable(feature = "cold_path", issue = "136873")]
734#[inline(always)]
735pub const fn cold_path() {
736 crate::intrinsics::cold_path()
737}
738
739/// Returns either `true_val` or `false_val` depending on the value of
740/// `condition`, with a hint to the compiler that `condition` is unlikely to be
741/// correctly predicted by a CPU’s branch predictor.
742///
743/// This method is functionally equivalent to
744/// ```ignore (this is just for illustrative purposes)
745/// fn select_unpredictable<T>(b: bool, true_val: T, false_val: T) -> T {
746/// if b { true_val } else { false_val }
747/// }
748/// ```
749/// but might generate different assembly. In particular, on platforms with
750/// a conditional move or select instruction (like `cmov` on x86 or `csel`
751/// on ARM) the optimizer might use these instructions to avoid branches,
752/// which can benefit performance if the branch predictor is struggling
753/// with predicting `condition`, such as in an implementation of binary
754/// search.
755///
756/// Note however that this lowering is not guaranteed (on any platform) and
757/// should not be relied upon when trying to write cryptographic constant-time
758/// code. Also be aware that this lowering might *decrease* performance if
759/// `condition` is well-predictable. It is advisable to perform benchmarks to
760/// tell if this function is useful.
761///
762/// # Examples
763///
764/// Distribute values evenly between two buckets:
765/// ```
766/// #![feature(select_unpredictable)]
767///
768/// use std::hash::BuildHasher;
769/// use std::hint;
770///
771/// fn append<H: BuildHasher>(hasher: &H, v: i32, bucket_one: &mut Vec<i32>, bucket_two: &mut Vec<i32>) {
772/// let hash = hasher.hash_one(&v);
773/// let bucket = hint::select_unpredictable(hash % 2 == 0, bucket_one, bucket_two);
774/// bucket.push(v);
775/// }
776/// # let hasher = std::collections::hash_map::RandomState::new();
777/// # let mut bucket_one = Vec::new();
778/// # let mut bucket_two = Vec::new();
779/// # append(&hasher, 42, &mut bucket_one, &mut bucket_two);
780/// # assert_eq!(bucket_one.len() + bucket_two.len(), 1);
781/// ```
782#[inline(always)]
783#[unstable(feature = "select_unpredictable", issue = "133962")]
784pub fn select_unpredictable<T>(condition: bool, true_val: T, false_val: T) -> T {
785 // FIXME(http://github.com/rust-lang/unsafe-code-guidelines/issues/245):
786 // Change this to use ManuallyDrop instead.
787 let mut true_val = MaybeUninit::new(true_val);
788 let mut false_val = MaybeUninit::new(false_val);
789 // SAFETY: The value that is not selected is dropped, and the selected one
790 // is returned. This is necessary because the intrinsic doesn't drop the
791 // value that is not selected.
792 unsafe {
793 crate::intrinsics::select_unpredictable(!condition, &mut true_val, &mut false_val)
794 .assume_init_drop();
795 crate::intrinsics::select_unpredictable(condition, true_val, false_val).assume_init()
796 }
797}