1

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?

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
  • In optimization, there are so-called "hot" (likely to execute) and "cold" (unlikely to execute) paths. Sometimes we can speed up the hot path by slowing down the cold path (e.g. by not inlining the cold path, allowing for better hot path code locality). The "cold" attribute hints to the optimizer to perform that optimization. – PitaJ Sep 01 '22 at 21:09
  • @PitaJ: I believe it is also common for the linker to try to group hot code/functions together (so the number of code pages that contain hot code is smaller, and therefore more likely to fit in cache/memory and stay there through constant use); cold code therefore becomes even slower (it's more likely to need to be paged in when called), but that shouldn't matter since the cost is paid infrequently or not at all. – ShadowRanger Sep 01 '22 at 21:33
  • Yeah my example was only one possible optimization. Another I could mention is moving around branching instructions for better pipelining and speculative execution behavior. – PitaJ Sep 01 '22 at 21:56

1 Answers1

3

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.

cameron1024
  • 9,083
  • 2
  • 16
  • 36
  • 1
    `cold` is more than `unlikely`. It prevents inlining and uses a different calling convention IIRC. `unlikely` only affects the order of branches, I think. – Chayim Friedman Sep 02 '22 at 05:34
  • Accodring to the top comment on [this reddit thread](https://www.reddit.com/r/rust/comments/gtcsem/what_does_cold_actually_do/) , it still uses `fastcc` even with `cold`, but you can force it to use `coldcc` by also adding `#[inline(never)]`. I haven't tested this myself though, and perhaps it's changed in a more recent compiler version? – cameron1024 Sep 02 '22 at 13:03