Skip to main content

rand_float/
cold.rs

1//! A compiler barrier against if-conversion of rare branches.
2
3/// A do-nothing function that forces the compiler to keep the branch arm
4/// calling it a real branch.
5///
6/// This is a kluge, and it is here for speed only. Techniques that draw further
7/// words from the source on a rare path (a pool refill, a second-word
8/// extension) make the next source state depend on which path was taken.
9/// Without a genuine function call in the rare arm, LLVM if-converts it in
10/// bulk-generation loops (observed both on Apple Silicon and 12th-gen Intel),
11/// selecting the next source state with a conditional move: that puts the whole
12/// conversion on the loop-carried dependency chain of the source, several times
13/// slower than predicting the branch, which is taken once per ~2¹² calls. A
14/// call cannot be speculated, so the arm containing it cannot be flattened;
15/// branch-weight hints alone (`std::hint::cold_path`) proved insufficient. The
16/// `black_box` keeps the body from being inferred side-effect-free, which would
17/// let the call be optimized away.
18#[cold]
19#[inline(never)]
20pub(crate) fn cold_barrier() {
21    std::hint::black_box(());
22}