What is a “cache-friendly” code?

前端 未结 9 2314
误落风尘
误落风尘 2020-11-22 02:16

What is the difference between \"cache unfriendly code\" and the \"cache friendly\" code?

How can I make sure I write cache-efficie

9条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 03:06

    In addition to @Marc Claesen's answer, I think that an instructive classic example of cache-unfriendly code is code that scans a C bidimensional array (e.g. a bitmap image) column-wise instead of row-wise.

    Elements that are adjacent in a row are also adjacent in memory, thus accessing them in sequence means accessing them in ascending memory order; this is cache-friendly, since the cache tends to prefetch contiguous blocks of memory.

    Instead, accessing such elements column-wise is cache-unfriendly, since elements on the same column are distant in memory from each other (in particular, their distance is equal to the size of the row), so when you use this access pattern you are jumping around in memory, potentially wasting the effort of the cache of retrieving the elements nearby in memory.

    And all that it takes to ruin the performance is to go from

    // Cache-friendly version - processes pixels which are adjacent in memory
    for(unsigned int y=0; y

    to

    // Cache-unfriendly version - jumps around in memory for no good reason
    for(unsigned int x=0; x

    This effect can be quite dramatic (several order of magnitudes in speed) in systems with small caches and/or working with big arrays (e.g. 10+ megapixels 24 bpp images on current machines); for this reason, if you have to do many vertical scans, often it's better to rotate the image of 90 degrees first and perform the various analysis later, limiting the cache-unfriendly code just to the rotation.

提交回复
热议问题