背景
阅读有关缓存优化的论文(与循环中的缓存行相关等)。
这个问题与此上下文相关:拥有 1024 个整数的数组。
尺寸:CPU 缓存 64k,缓存行 32 字节,整数大小为 4 字节。
Intel Core 2 Duo 处理器。
问题
根据我的 CPU,8 个整数适合在一个缓存行中。
[0,1,2,3,4,5,6,7,8,9,10,...,1023]
^
If I want to access 4 and go downward, 3,2,1 and 0 will be loaded already. 5,6,7 are loaded uselessly.
[0,1,2,3,4,5,6,7,8,..,1023]
^
If I want to access 7 and go downward, all the next elements will be in cache already. if I want to go upward, according to my cpu I will have to load another cache line immediatly after the arr[7] read.
我是正确的吗?
更进一步
那么什么会告诉我arr [4]不是在地址上,会导致缓存行加载而不是arr [7]? 如果我的陈述是正确的,我们不仅应该考虑数组对齐,还应该考虑整个程序的内存对齐以最小化缓存浪费,对吗?