Forcing code out of line in GCC and C++11

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?

Filed under: Coding
Posted at 18:10:00 BST on 17th September 2012.

About Matt Godbolt

Matt Godbolt is a C++ developer living in Chicago. Follow him on Mastodon or Bluesky.