Search code examples
c++cpu-cache

Code duplication reduces effective cache size


I'm reading a presentation from Scott Mayor, he mentiones this line:

Down side of inlining: Code duplication reduces effective cache size

I am not seeing how code duplication has anything to do with effective cache size


Solution

  • Duplicating code means that the same instructions are duplicated on consecutive memory addresses instead of having one single copy of those instructions in the body of a loop. That means that many cache lines are filled with the same content instead of just a few. As those instructions are frequently accessed (once per loop run) they are likely not leaving the cache, so other code (or data if cache is unified) must leave, which wouldn't happen if there were less number of frequently accessed instructions filling cache entries.