我应该将数据对齐到它们的数据类型还是 CPU 缓存行大小?

huangapple go评论60阅读模式
英文:

Should I align data to their data type or cpu cache line size?

问题

数据通常与其自身的数据类型对齐,即32位的int通常对齐到4字节,这使得加载/存储它们对处理器更有效率。

那么缓存行对齐什么时候起作用呢?如果x64缓存行大小为64字节,那么我是否应该使每个数据对齐到64字节?这似乎会浪费内存。

这两种对齐方式之间有什么关系?如果CPU与缓存行的交互始终是每次64位,那么为什么数据类型的对齐甚至重要呢?

英文:

Data is usually aligned with its own data type, i.e a 32-bit int is usually aligned to 4 bytes, this makes loading/storing them more efficient for the processor.

Now when does cache line alignment come into play? If x64 cache line size is 64 bytes then should I make each data aligned to 64 bytes? that seems like a waste of memory.

What is the relation between these two types of alignment? and if cpu interaction with cache line is always 64-bits at a time then why does data type alignment even matter?

答案1

得分: 2

数据通常与其自身的数据类型对齐,即32位整数通常对齐到4字节,这使得加载/存储它们对处理器更有效率。

在某些体系结构中,这不仅是效率问题,还涉及到您的代码是否能正常工作。尝试访问未对齐的对象可能会触发陷阱。此外,并不一定情况下给定数据类型的自然对齐与该数据类型的大小相同。自然对齐在实际中不会比类型的大小更大,但可以更小。

现在缓存行对齐是在什么情况下发挥作用?如果x64缓存行大小为64字节,那么我应该使每个数据都对齐到64字节吗?这似乎会浪费内存。

确实如此。而且会适得其反。缓存帮助的一种方式,也是缓存行大小通常比机器的本机字大小大数倍的原因之一,是一个地址上的字的访问非常频繁地跟随在附近地址的字的访问之后。因此,通过加载当前缓存行来提供将来读取的方式经常会提高效率。如果每个对象都对齐到缓存行大小(假设这是可行的),那么您将浪费掉缓存带来的许多优势。

这两种对齐方式之间有什么关系?

缓存行通常比任何本机数据类型更严格地对齐,因此对齐到缓存行边界的对象也将正确对齐到其数据类型。这也意味着没有本机数据类型的自然对齐对象会跨越两个缓存行。除此之外,我不太确定您可能会问什么。

如果CPU与缓存行的交互始终以64位为一次,那么数据类型对齐究竟有什么意义?

我想您可能是指64字节而不是64位。但是这个问题本来就不太合理。任何涉及缓存的都是CPU与内存交互的细节。因为缓存行对齐比任何本机数据类型都要严格,所以对象在缓存中的对齐方式与其在主内存中的对齐方式相同。缓存和对齐基本上是无关的事项。

总的来说,根据其数据类型自然对齐对象在某种程度上是一个相当重要的考虑因素(但不一定是必要的),但从第一近似角度来看,将单个对象对齐到缓存行没有特别重要性。缓存和缓存行在多个对象的访问模式的上下文中变得重要。理想情况下,我们将组织数据和代码,以便缓存不会被无必要地失效、刷新(或因此重新加载),但这与单个对象的对齐无关。

英文:

> Data is usually aligned with its own data type, i.e a 32-bit int is
> usually aligned to 4 bytes, this makes loading/storing them more
> efficient for the processor.

On some architectures, it's not so much a matter of efficiency but of your code working at all. Attempting to access a misaligned object can produce a trap. Also, it is not necessarily the case that the natural alignment for a given data type is the same as the size of that data type. The natural alignment cannot, in practice, be larger than the type's size, but it can be smaller.

> Now when does cache line alignment come into play? If x64 cache line
> size is 64 bytes then should I make each data aligned to 64 bytes?
> that seems like a waste of memory.

Indeed so. And counterproductive, too. One of the ways cache helps, and among the reasons that cache line size is generally several times larger than the machine's native word size, is that an access to a word at one address is very frequently followed by accesses to words at nearby addresses. Thus, one often provides for future reads by loading the cache line for the current one. If every object were aligned to the cache line size (supposing that were even feasible), you would thereby throw away a lot of the advantage otherwise obtained by caching.

> What is the relation between these two types of alignments?

Cache lines are ordinarily aligned more strictly than any native data type, so an object aligned to a cache line boundary will also be aligned properly for its data type. That also means that no naturally aligned object of native data type will straddle two cache lines. Other than that, I'm not really sure what you might asking.

> and if cpu interaction with cache line is always 64-bits at a time then why does data type alignment even matter?

I guess you meant 64 bytes, not bits. But the question is anyway ill-conceived. Any involvement of cache is a detail of CPU interaction with memory. And because cache line alignment is stricter than any native data type's, an object has the same alignment in cache as it has in main memory. Caching and alignment are pretty much orthogonal matters.

Overall, aligning objects naturally for their data types is a fairly important consideration (but not necessarily essential), but to a first approximation, aligning individual objects to cache lines is of no particular import. Cache and cache lines become important in the context of patterns of access to multiple objects. Ideally, one will structure the data and code so that cache does not get invalidated or flushed (or, therefore, reloaded) any more than necessary, but that has little to do with the alignment of individual objects.

huangapple
  • 本文由 发表于 2023年7月31日 23:43:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76805177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定