Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reducing Instruction Cache misses (in C++)

Let's say I have a C++ class whose implementation looks something like this:

// ...

MyClass::iterativeFunction() {
     for (int i = 0; i < 1000000; i++) {
          performAction(i);
     }
}

MyClass::performAction(int index) {
     // Block of code (non-inline-able)
}

// ...

At the C++ level, do I have any control over the spacial locality of these methods, or do I just have to hope the compiler will notice the related methods and optimise its assembly accordingly? Ideally I would like them to be right next to each other so they will be loaded into the instruction cache together, but I have no idea how to let the compiler know I'd really like this to happen.

like image 583
Ephemera Avatar asked Feb 19 '23 15:02

Ephemera


1 Answers

In either case, the code can't run until it gets into the cache. In either case, it will be equally obvious to the CPU where the code flow goes because the flow is unconditional. So it won't make any difference. A modern code cache doesn't fetch ahead in address space, it fetches ahead in the instruction flow, following unconditional branches and predicting conditional branches as necessary.

So there is no reason to care about this. It won't make any difference.

like image 100
David Schwartz Avatar answered Mar 04 '23 05:03

David Schwartz