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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With