Plu*_*uto 6 c++ caching stl x86-64
以下是 MSVC STL 中的部分代码std::any
:
class any {
// ...
struct _Small_storage_t {
unsigned char _Data[_Any_small_space_size];
const _Any_small_RTTI* _RTTI;
};
static_assert(sizeof(_Small_storage_t) == _Any_trivial_space_size);
struct _Big_storage_t {
// Pad so that _Ptr and _RTTI might share _TypeData's cache line
unsigned char _Padding[_Any_small_space_size - sizeof(void*)];
void* _Ptr;
const _Any_big_RTTI* _RTTI;
};
static_assert(sizeof(_Big_storage_t) == _Any_trivial_space_size);
struct _Storage_t {
union {
unsigned char _TrivialData[_Any_trivial_space_size];
_Small_storage_t _SmallStorage;
_Big_storage_t _BigStorage;
};
uintptr_t _TypeData;
};
static_assert(sizeof(_Storage_t) == _Any_trivial_space_size + sizeof(void*));
static_assert(is_standard_layout_v<_Storage_t>);
union {
_Storage_t _Storage{};
max_align_t _Dummy;
};
};
Run Code Online (Sandbox Code Playgroud)
我们可以看到,根据保存的对象的类型,主要存储部分是_Storage_t
,这将是_Small_storage_t
,_Big_storage_t
或者只是数组。unsigned char
std::any
我无法理解以下评论的含义_Big_storage_t
:
// Pad so that _Ptr and _RTTI might share _TypeData's cache line
unsigned char _Padding[_Any_small_space_size - sizeof(void*)];
Run Code Online (Sandbox Code Playgroud)
在我的平台上,一些参数的值为:
sizeof(void*); // 8 bytes
constexpr int _Small_object_num_ptrs = 6 + 16 / sizeof(void*); // 8
constexpr size_t _Any_small_space_size = (_Small_object_num_ptrs - 2) * sizeof(void*); // 48
constexpr size_t _Any_trivial_space_size = (_Small_object_num_ptrs - 1) * sizeof(void*); // 56
sizeof(_Small_storage_t); // 56 bytes
sizeof(_Big_storage_t); // 56 bytes
Run Code Online (Sandbox Code Playgroud)
我的问题是
为什么_Padding
允许_Ptr
并与_RTTI
共享缓存行_TypeData
?
这样做(共享缓存行)有什么好处?
归档时间: |
|
查看次数: |
65 次 |
最近记录: |