jeudi 6 avril 2017

Does padding necessary with the usage of alignas

Given the following code:

const int CACHE_LINE_SIZE = 64;
template<typename T>
struct cache_line_storageA
{
    alignas(CACHE_LINE_SIZE)T data;
    char pad[CACHE_LINE_SIZE > sizeof(T) ? CACHE_LINE_SIZE - sizeof(T) : 1];
};

template<typename T>
struct cache_line_storageB
{
    alignas(CACHE_LINE_SIZE)T data;
};

Question> I need to align the type T to 64 bytes. Which definition of the cache_line_storage is better? Personally, I think the second one cache_line_storageB is better because the requirement of alignment to 64 bytes has been achieved through alignas. There is no need to manually insert a padding element.

Aucun commentaire:

Enregistrer un commentaire