align算是老生常谈了:
数据按照某个cache line大小align的话会改进cache效率, 因此默认情况下,编译器会自动把class数据进行align;
class与其member的align尤其自己规则,直接导致:
struct size
...{
uint32 member0;
uint8 member1;
};
(sizeof(size)!=sizeof(uint32)+sizeof(uint8)) is usually true;
struct compose_good
...{
uint8 mem0;
uint16 mem1;
uint32 mem2;
};
struct compose_bad
...{
uint8 mem0;
uint32 mem1;
uint16 mem2;
};
(sizeof(compose_good)<sizeof(compose_bad)) is usually true;
本文探讨了数据对齐对于缓存效率的影响,并通过具体的C++结构体示例说明了编译器如何自动进行数据对齐以提高内存访问速度。通过对不同结构体布局的比较,展示了良好的数据对齐可以显著减少内存占用。
5万+

被折叠的 条评论
为什么被折叠?



