最近看到一篇文章
https://segment.com/blog/allocation-efficiency-in-high-performance-go-services/
里面关于指针提到了一点
Copying objects within a cache line is the roughly equivalent to copying a single pointer.
CPUs move memory between caching layers and main memory on cache lines of constant size. On x86 this is 64 bytes.
Further, Go uses a technique called Duff ’ s device to make common memory operations like copies very efficient.
大致意思是现代 CPU 从缓存和主存储器之间移动数据存在一个固定的尺寸,即使使用指针也最少会产生这个尺寸的交换.所以函数参数如果是一个不大的结构,使用指针还是拷贝原变量并没有很大的区别.
再加上使用指针的话会导致 Go 把变量逃逸到堆而不是分配在栈上,结果是函数参数使用指针反而效率不如使用拷贝.
看完之后对我之前几乎无脑传入 /返回指针的代码方式产生了很大的 shock
想问一下有关于 CPU 这块策略的学习资料嘛?