简介:
C 17 引入了两个常量,std: :hardware_structive_interference_size 和 std::hardware_constructive_interference_size,以促进数据结构的对齐和布局,以便在不同的硬件架构上实现最佳性能。
这些常量旨在为目标架构提供 L1 缓存行大小的近似值。
考虑以下示例:
struct CacheOptimizedStruct { alignas(std::hardware_constructive_interference_size) int a; alignas(std::hardware_constructive_interference_size) int b; }; int main() { CacheOptimizedStruct data; // Accessing data.a and data.b in a tight loop will likely benefit from better cache locality. }
在此示例中,alignas 属性使用常量来确保 data.a 和 data .b 具有用于真正共享的最佳对齐和内存布局。
是的,这可能是一个问题。这些常量不能保证为所有目标计算机提供精确的缓存行大小。
但是,您可以定义自己的常量或宏,以根据系统特定信息提供更准确的值,例如:
#ifdef KNOWN_L1_CACHE_LINE_SIZE constexpr std::size_t cache_line_size = KNOWN_L1_CACHE_LINE_SIZE; #else constexpr std::size_t cache_line_size = std::hardware_destructive_interference_size; #endif
以上是`std::hardware_delta_interference_size` 和 `std::hardware_constructive_interference_size` 与 L1 缓存线大小有何关系?它们的用例是什么?的详细内容。更多信息请关注PHP中文网其他相关文章!