assume_read

Function assume_read 

Source
pub fn assume_read<T: Pessimize>(x: &T)
Expand description

Force the compiler to assume that a value, and data transitively reachable via that value (for pointers/refs), is being used if Rust rules allow for it.

You can apply this barrier to unused computation results in order to prevent the compiler from optimizing out the associated computations.

On pointers/references, it will have the side effect of spilling target data resident in CPU registers to memory, although the in-register copies remain valid and can be reused later on without reloading.

If you need an assume_read alternative for a variable x that does not implement Pessimize, you can use assume_read(&x), at the cost of forcing any data from x which is currently cached in registers to be spilled into memory.

The assume_read implementation of *const T and *mut T may not work as expected if an &mut T reference to the same data exists somewhere, because dereferencing the pointer in that situation would be undefined behavior, which by definition does not exist in the eye of the compiler.

For pointer types, this operation may sometimes be pessimized into a full assume_accessed() optimization barrier, as a result of rustc not leveraging the underlying readonly optimization hint. It is hoped that future versions of rustc will take stronger notice of that hint.