对齐在C ++ 11中的性能是否重要? [英] Does alignment really matter for performance in C++11?

查看:115
本文介绍了对齐在C ++ 11中的性能是否重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对齐对于C ++ 11中的性能是否重要?

Does alignment really matter for performance in C++11?

在Stroustrup的书中有一个建议,命令结构中的成员
最大到最小。但我不知道有人
是否已经测量实际上看是否有什么区别,
和如果值得考虑当编写代码。

There is an advice in Stroustrup's book to order the members in a struct beginning from the biggest to the smallest. But I wonder if someone has made measurements to actually see if this makes any difference, and if it is worth it to think about when writing code.

推荐答案

对齐不仅关系到性能,而且也是正确性。如果数据未正确对齐,或访问错误的内存位置,某些体系结构将失败,并显示处理器陷阱。在其他人,访问未对齐的变量被分成多个访问和位移(通常在硬件内,有时由操作系统陷阱处理程序),失去原子性。

Alignment matters not only for performance, but also for correctness. Some architectures will fail with an processor trap if the data is not aligned correctly, or access the wrong memory location. On others, access to unaligned variables is broken into multiple accesses and bitshifts (often inside the hardware, sometimes by OS trap handler), losing atomicity.

按照大小降序对成员进行排序是为了通过填充而不是为了对齐或速度而浪费的最佳打包/最小空间。无论以什么顺序列出它们,成员都将正确对齐,除非您请求不一致的布局使用专用的pragmas(即非便携式 #pragma pack )或关键字。虽然总体结构大小受填充影响,并且也影响速度,但通常还有另一个顺序是最佳的。

The advice to sort members in descending order of size is for optimal packing / minimum space wasted by padding, not for alignment or speed. Members will be correctly aligned no matter what order you list them in, unless you request non-conformant layout using specialized pragmas (i.e. the non-portable #pragma pack) or keywords. Although total structure size is affected by padding and also affects speed, often there is another ordering that is optimal.

为了获得最佳性能,应该尝试获取使用的成员一起到相同的高速缓存行中,以及由不同的线程访问的成员到不同的高速缓存行中。有时,这意味着很多填充,以获取一个跨线程共享变量单独在自己的缓存行。但这比从假分享中获得的成绩更好。

For best performance, you should try to get members which are used together into the same cache line, and members that are accessed by different threads into different cache lines. Sometimes that means a lot of padding to get a cross-thread shared variable alone in its own cache line. But that's better than taking a performance hit from false sharing.

这篇关于对齐在C ++ 11中的性能是否重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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