我对 L1 缓存的理解是内存获取会加载缓存行。假设缓存行大小为 64 字节,如果我在 address 访问内存p
,它会将整个块从p
to加载p + 64
到缓存中。因此,最好从左到右(而不是从右到左)遍历数组以最大化缓存局部性。
但是,我编写了示例 C 代码,该代码分配了一个包含 1 亿个字符的数组,将随机值写入其中并求和(复制如下以供参考)。一个版本的代码从左到右求和,另一个从右到左求和。当我对其进行基准测试时,我得到了非常相似的结果(其中“时钟周期”是用 . 来衡量的clock
。代码是在没有优化的情况下编译的。
所以我的问题是:现代处理器除了“缓存读取+ 64字节”之外还做其他事情吗?他们是否向前和向后缓存?编译器可以“告诉”处理器代码正在向后迭代吗?
作为参考,我正在Mac OS X 10.13.3
使用gcc-7 (Homebrew GCC 7.2.0_1) 7.2.0
具有 64 字节缓存行的 x86-64 Intel 处理器运行。
基准测试:
$ ./a.out
Backward Iterating...took 150101 clock cycles
$ ./a.out
Forward Iterating...took 146545 clock cycles
我本来预计前向迭代会快大约 64 倍,因为每 64 个元素应该是缓存命中,而对于后向迭代,每个元素都应该是缓存未命中。
所以,我在上面调用了cachegrind。两者的缓存命中未命中率几乎相同:
# Left to right iteration
==21773==
==21773== I refs: 4,006,996,067
==21773== I1 misses: 5,183
==21773== LLi misses: 3,019
==21773== I1 miss rate: 0.00%
==21773== LLi miss rate: 0.00%
==21773==
==21773== D refs: 1,802,393,260 (1,401,627,925 rd + 400,765,335 wr)
==21773== D1 misses: 3,153,101 ( 1,588,104 rd + 1,564,997 wr)
==21773== LLd misses: 3,004,885 ( 1,440,660 rd + 1,564,225 wr)
==21773== D1 miss rate: 0.2% ( 0.1% + 0.4% )
==21773== LLd miss rate: 0.2% ( 0.1% + 0.4% )
==21773==
==21773== LL refs: 3,158,284 ( 1,593,287 rd + 1,564,997 wr)
==21773== LL misses: 3,007,904 ( 1,443,679 rd + 1,564,225 wr)
==21773== LL miss rate: 0.1% ( 0.0% + 0.4% )
# Right to left iteration
==21931==
==21931== I refs: 4,006,996,453
==21931== I1 misses: 5,198
==21931== LLi misses: 3,045
==21931== I1 miss rate: 0.00%
==21931== LLi miss rate: 0.00%
==21931==
==21931== D refs: 1,802,393,428 (1,401,628,038 rd + 400,765,390 wr)
==21931== D1 misses: 3,153,113 ( 1,588,079 rd + 1,565,034 wr)
==21931== LLd misses: 3,135,505 ( 1,571,219 rd + 1,564,286 wr)
==21931== D1 miss rate: 0.2% ( 0.1% + 0.4% )
==21931== LLd miss rate: 0.2% ( 0.1% + 0.4% )
==21931==
==21931== LL refs: 3,158,311 ( 1,593,277 rd + 1,565,034 wr)
==21931== LL misses: 3,138,550 ( 1,574,264 rd + 1,564,286 wr)
==21931== LL miss rate: 0.1% ( 0.0% + 0.4% )
代码:
#include <stdint.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define BUF_SIZE 100000000
int main() {
srand(time(NULL));
uint8_t *buf1 = (uint8_t *)malloc(BUF_SIZE);
// Fill the buf with random data
for (size_t i = 0; i < BUF_SIZE; ++i) {
buf1[i] = rand();
}
#ifdef BACKWARDS
printf("Backward Iterating...");
#else
printf("Forward Iterating...");
#endif
uint64_t sum = 0;
clock_t start = clock();
#ifdef BACKWARDS
for (size_t i = BUF_SIZE - 1; i != ~0; --i) {
#else
for (size_t i = 0; i < BUF_SIZE; ++i) {
#endif
sum += buf1[i];
}
clock_t end = clock();
printf("took %lu clock cycles\n", end - start);
printf("sum: %llu\n", sum);
free(buf1);
}