Reduced 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 spatial locale of these methods, or do I just need to hope that the compiler will notice the related methods and optimize its assembly accordingly? Ideally, I would like them to be next to each other, so they will be loaded into the cache together, but I have no idea how to tell the compiler that I really like it.

+4
source share
4 answers

In any case, the code cannot work until it hits the cache. In any case, this will be equally obvious for the CPU, where the code stream goes because the stream is unconditional. So it will not make any difference. The modern code cache is not pushed forward in the address space, it is pushed forward in the command stream, following the unconditional branches and predicting conditional branches as necessary.

Therefore, there is no reason to care about this. It will not make any difference.

+5
source

From a technical point of view, no. However, on modern processors, you usually don't need to worry about the instruction cache almost as much as you do the data cache, unless you have very large executable or really terrible branches.

The reasons for this are that the cache lines are only about 64 bytes, which means that if your methods are more than 64 bytes (and they are), they will need to be loaded into several cache entries, even if they directly follow each other in physical memory .

+2
source

If you need this level of control and optimization, then C ++ is the wrong language for you.

But the real answer to your question is "No."

+1
source

No, as far as I know, you will not be able to determine the location of your methods. If C ++ allows nested procedures, this will be one way to ensure that the called procedure is local.

0
source

Source: https://habr.com/ru/post/1443361/


All Articles