Have you ever wanted to force some code completely out of line? Maybe some rare case or exception that really you don’t give a heck about when on your hot path, but still sits there and pollutes your instruction cache?
You have?
Boy is it your lucky day today!
Before:
void test() {
// do stuff here, update failure
if (failed) {
// Not only is this unlikely, but I don't
// want this code polluting the i-cache.
cout << "Oh no!" << endl;
}
}
After:
void test() {
// do stuff here, update failure
if (failed) {
[&]() __attribute__(noinline,cold) {
cout << "Oh no!" << endl;
}();
}
}
Basically, define a C++11 lambda function, mark it as cold and non-inlineable, then execute it immediately. A function being “cold” makes GCC treat the code as “don’t predict a branch to this”, makes it optimized for size instead of speed, and also places it in a section that gets linked away from “hot” code.
Check out the difference on GCC Explorer.
Cute trick, eh?
Matt Godbolt is a C++ developer living in Chicago. He works for Hudson River Trading on super fun but secret things. He is one half of the Two's Complement podcast. Follow him on Mastodon or Bluesky.