rustrust-attributes

When to use "cold" built-in codegen attribute in Rust?


There isn't much information on this attribute in the reference document other than

The cold attribute suggests that the attributed function is unlikely to be called.

How does it work internally and when a Rust developer should use it?


Solution

  • It tells LLVM to mark a function as cold (i.e. not called often), which changes how the function is optimized such that calls to this code is potentially slower, and calls to non-cold code is potentially faster.

    Mandatory disclaimer about performance tweaking:

    You really should have some benchmarks in place before you start marking various bits of code as cold. You may have some ideas about whether something is in the hot path or not, but unless you test it, you can't know for sure.

    FWIW, there's also the perma-unstable LLVM intrinsics likely and unlikely, which do a similar thing, but these have been known to actually hurt performance, even when used correctly, by preventing other optimizations from happening. Here's the RFC: https://github.com/rust-lang/rust/issues/26179

    As always: benchmark, benchmark, benchmark! And then benchmark some more.