Why do some types (e.g. Float80) have a memory alignment bigger than word size?

后端 未结 2 806
傲寒
傲寒 2021-02-02 03:48

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 align

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-02 04:36

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

    Cache lines.

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

    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.

    Cache effects

提交回复
热议问题