为什么某些类型(例如Float80)的内存对齐方式大于字长? [英] Why do some types (e.g. Float80) have a memory alignment bigger than word size?

查看:256
本文介绍了为什么某些类型(例如Float80)的内存对齐方式大于字长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了明确起见,我只想知道为什么在我的64位Mac上,Swift编译器说某些类型的对齐方式(如Float80)是16. 要检查一种类型的内存对齐要求,请使用alignof函数.

To make it specific, I only want to know why on my 64 bit mac, the Swift compiler says the alignment of some types like Float80 is 16. To check the memory alignment requirement of a type, I use the alignof function.

sizeof(Float80) // ~> 16 bytes, it only needs 10 bytes, but because of hardware design decisions it has to be a power of 2
strideof(Float80) // ~> 16 bytes, clear because it is exact on a power of 2, struct types with Float80 in it, can be bigger
alignof(Float80) // ~> 16 bytes, why not 8 bytes, like String ?

我知道小于或等于单词大小的类型的内存对齐方式是有益的.

I understand the memory alignment of types less or equal the size of the word is beneficial.

sizeof(String) // ~> 24 bytes, clear because 24 is multiple of 8
strideof(String) // ~> 24 bytes, clear because 24 is multiple of 8
alignof(String) // ~> 8 bytes, clear because something greater or equal to 8 bytes should align to 8 bytes

许多具有较大内存大小占用空间的类型,例如String(大小为24),确实具有8字节的内存对齐要求.我希望这是我使用的CPU/RAM总线的大小,因为我有64位的mac和os. 我检查类型的大小,而无需使用sizeof函数进行最后的填充,而使用strideof函数在末尾添加填充(strideof在结构数组中更有用,Swift然后将字节添加到结束以达到对齐要求的下一个倍数.)

Many types with a bigger memory size footprint like String (with a size of 24) does have a memory alignment requirement of 8 bytes. I expect that is the size of my CPU/RAM bus in use, because I have a 64 bit mac and os. I check the size of the type without the last padding with the sizeof function and with the adding padding to the end with the strideof function (strideof is more helpful in arrays of structs, Swift then adds bytes to the end to reach the next multiple of the alignment requirement.)

我知道对于小于或等于8字节大小的类型,填充是必要的.

I understand that padding is necessary for types lesser or equal than the size of 8 byte.

但是我不明白为什么在我的64位Mac上具有大于8个字节的内存对齐要求是有利的.

Float80的值需要80位,即10个字节,具有6个填充字节.

以下是我要说清楚的图片. Float80允许使用绿色位置,不允许使用红色位置. 此图片中的内存为8字节块.

Here is an image to make it more clear, what I mean. The green positions are allowed for a Float80, the red positions not. The memory is in 8 byte chunks in this picture.

推荐答案

借助Martin R的链接并暗示这是处理器设计决定.我发现了为什么.

With the help of Martin R's link and the hint that it is a processor design decision. I found the readon why.

缓存行.

高速缓存行对于处理器来说是非常小的内存,在我的Intel Mac 64位上,它是128位(16字节).

Cache lines are a very small memory for the processor, on the Intel Mac 64 bit of mine it is 128 bit (16 bytes).

从问题图片中可以看出,虚线和粗线之间存在区别.粗线在处理器的高速缓存线之间.如果您可以用更多的内存成本来做得更好,则您不想加载2条缓存行.因此,如果处理器仅允许,则将8字节(或更大)大小的类型在高速缓存行的开头(16的倍数)对齐.与高速缓存行一样大的类型将不会有两次高速缓存行读取(在我的情况下,字长为16字节,为双倍).如您在图片中所看到的,只有红色块穿过粗线(因此,每个设计都不允许使用它们.)

As seen in the picture of the question I knew there was a difference between the dotted and the bold lines. The bold lines are between the cache lines of the processor. You don't want to load 2 cache lines if you could do better with a little more memory cost. So if the processor only allows, that types with a size of 8 bytes (or bigger) are aligned on the start of a cache line (every multiple of 16). There will be no two cache line reads for a type that is as big as a cache line (double the word size im my case, 16 bytes). As you can see in the picture only the red blocks are crossing the bold line (so they are not allowed per design).

请参阅附件中的链接以获取更多信息.

See the link attached for more info.

缓存效果

这篇关于为什么某些类型(例如Float80)的内存对齐方式大于字长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆