prefetch_read

Function prefetch_read 

Source
pub const fn prefetch_read<T>(ptr: *const T, locality: Locality)
🔬This is a nightly-only experimental API. (hint_prefetch #146941)
Expand description

Prefetch the cache line containing ptr for a future read.

A strategically placed prefetch can reduce cache miss latency if the data is accessed soon after, but may also increase bandwidth usage or evict other cache lines.

A prefetch is a hint, and may be ignored on certain targets or by the hardware.

Passing a dangling or invalid pointer is permitted: the memory will not actually be dereferenced, and no faults are raised.

§Examples

#![feature(hint_prefetch)]
use std::hint::{Locality, prefetch_read};
use std::mem::size_of_val;

// Prefetch all of `slice` into the L1 cache.
fn prefetch_slice<T>(slice: &[T]) {
    // On most systems the cache line size is 64 bytes.
    for offset in (0..size_of_val(slice)).step_by(64) {
        prefetch_read(slice.as_ptr().wrapping_add(offset), Locality::L1);
    }
}