Branches
branches provides branch prediction hints, control flow assumptions, abort, and manual data prefetch (read & write) helpers for performance optimization, using stable Rust primitives where available and falling back to core::intrinsics on nightly.
Usage
To use branches, add the following to your Cargo.toml file:
[]
= "0.2"
For a no_std environment, disable the default features by adding the following to your Cargo.toml instead:
[]
= { = "0.2", = false }
Functions
The following functions are provided by branches:
likely(b: bool) -> bool: Returns the input value but provides hints for the compiler that the statement is likely to be true.unlikely(b: bool) -> bool: Returns the input value but provides hints for the compiler that the statement is unlikely to be true.assume(b: bool): Assumes that the input condition is always true and causes undefined behavior if it is not. On stable Rust, this function usescore::hint::unreachable_unchecked()to achieve the same effect.abort(): Aborts the execution of the process immediately and without any cleanup.prefetch_read_data<T, const LOCALITY: i32>(addr: *const T): Hints the CPU to load data ataddrinto cache for an upcoming read.LOCALITYselects cache behavior (e.g. 0 = L1, 1 = L2, 2 = L3, other = non‑temporal or arch default). Unsafe:addrmust be a valid, properly aligned pointer; may not alias freed or unmapped memory.prefetch_write_data<T, const LOCALITY: i32>(addr: *const T): Hints the CPU to load a line for an upcoming write. SameLOCALITYsemantics as above. Unsafe for the same reasons, and you must ensure future writes are plausible (avoid prefetching arbitrary / constant addresses).
Guidelines:
- Only prefetch a small distance ahead (tune empirically).
- Too-far or excessive prefetching can evict useful cache lines.
- Never rely on prefetch for correctness; it is purely a performance hint.
Here's an example of how you can use likely to optimize a function:
use likely;
Loop manual prefetch example:
use ;
By correctly using the functions provided by branches, you can achieve a 10-20% improvement in the performance of your algorithms.
License
branches is licensed under the MIT license. See the LICENSE file for more information.