Function hide

Source
pub fn hide<T: Pessimize>(x: T) -> T
Expand description

Re-emit the input value as its output (identity function), but force the compiler to assume that it is a completely different value.

If you want to re-do the exact same computation in a loop, you can pass its inputs through this barrier to prevent the compiler from optimizing out the redundant computations.

Although Pessimize is implemented for zero-sized types, hide() will not serve its normal purpose of obscuring output on those types, because there is only one possible return value so the compiler knows that the output value is the same as the input value. Since zero-sized types may only hold state through global and thread-local variables, implementations of Pessimize::hide for stateful ZSTs should feature an assume_globals_accessed() optimization barrier.

If you need a hide alternative for a variable x that does not implement Pessimize, you can use *hide(&x), at the cost of forcing all data reachable via x which is currently cached in registers to be spilled to memory and reloaded when needed later on.

If you are familiar with the unstable core::hint::black_box function or analogs in benchmarking libraries like Criterion, please note that although this function has a similar API signature, it does not have the same semantics and cannot be used as a direct replacement. For example, core::hint::black_box(&mut x) should have the effect of pessimize::assume_accessed(&mut x), whereas pessimize::hide(x) does not enforce any compiler assumptions concerning the input value, it just turns it into another value that looks unrelated in the eye of the compiler.