What's cooler than being cool?
🧊 Ice Code 🧊
Alright alright alright alright alright alright alright alright
What is this?
This crate provides the ice!
macro, which labels a given block of Rust code as a
"cold path" in the application--one that is rarely executed. This information allows the
compiler to prioritize optimizing the other code paths, leading to better method inlining
behavior and boosting performance in the common case.
Usage
Source
Here's an example method that branches into two paths:
- The common case, where the argument it receives is exactly what it's expecting.
- The error handling branch, where the validation logic rejects the argument.
/// Returns the length of the provided string as long as it's shorter than 256 bytes.
Compiled output
As this godbolt.org output shows, the happy path's code got inlined into main
while all of the machinery needed to construct an error string has been pushed
somewhere else and is hidden behind a call
instruction.
Labeled cold code
If you use multiple ice!
invocations in the same method, the mangled anonymous names produced
by the compiler may be difficult for humans to disambiguate. You can add a label to the generated
assembly by using this syntax:
cold!
This will produce assembly like the following: