You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all, kudos to your cache-aware indices. Though the underlying logic is not obvious (!), it does work and improves performances.
Two small things, not new to version 1.1:
The line: char padding_[kCacheLineSize - sizeof(writeIdxCache_)]
has no effect as the global structure has a size that is already quantized, because of alignas(kCacheLineSize) members.
The way you compute kPadding is not optimal, because if sizeof(T) == kCacheLineSize, then kPadding is 1, while 0 would be better.
1. The line:
`char padding_[kCacheLineSize - sizeof(writeIdxCache_)]`
has no effect as the global structure has a size that is already quantized, because of `alignas(kCacheLineSize)` members.
Just because sizeof(T)==kCacheLineSize doesn't mean that alignment of T is kCacheLineSize. You could do something better by taking alignment into account.
The stackoverflow link you mention is about support for aligned-new support, which is not the same thing as padding.
Compilers automatically insert padding at the end of a struct, to ensure that arrays of the struct will be properly aligned. This does not depend on the C++ version. http://eel.is/c++draft/expr.sizeof#2
Just because sizeof(T)==kCacheLineSize doesn't mean that alignment of T is kCacheLineSize. You could do something better by taking alignment into account.
This is right. At the same time, if T is aligned on kCacheLineSize, then its size is commonly sizeof(T) for the reason mention earlier.
I say "commonly", because we typically store small objects in concurrent queues. In this case, my original remark stands.
Compilers automatically insert padding at the end of a struct, to ensure that arrays of the struct will be properly aligned. This does not depend on the C++ version. http://eel.is/c++draft/expr.sizeof#2
I see what you are saying, the padding will always be there. If overaligned new is not supported the padding will still be there. Thanks!
This is right. At the same time, if T is aligned on kCacheLineSize, then its size is commonly sizeof(T) for the reason mention earlier.
I say "commonly", because we typically store small objects in concurrent queues. In this case, my original remark stands.
Better to waste some memory and be safe than sorry. Using constexpr and alignof(T) I could make padding 0 if alignof(T) > kCacheLineSize.
Both suggestions are now addressed on master. The redundant trailing padding was removed in 1053918. PR #69 now also removes buffer padding when alignof(T) is at least the false-sharing range, including equality. Types whose size matches the range but whose alignment is smaller retain padding. Regression tests cover allocation counts and boundaries, aligned elements, and queue wraparound; all CI checks pass. Thanks for the suggestions!
Activity
rigtorp commentedon Jul 26, 2021
It's required if compiling using C++14 or earlier since alignment is not guaranteed for over-aligned types. https://stackoverflow.com/questions/49373287/gcc-over-aligned-new-support-alignas
Just because
sizeof(T)==kCacheLineSizedoesn't mean that alignment of T iskCacheLineSize. You could do something better by taking alignment into account.Philippe91 commentedon Jul 26, 2021
The stackoverflow link you mention is about support for aligned-new support, which is not the same thing as padding.
Compilers automatically insert padding at the end of a struct, to ensure that arrays of the struct will be properly aligned. This does not depend on the C++ version.
http://eel.is/c++draft/expr.sizeof#2
This is right. At the same time, if T is aligned on kCacheLineSize, then its size is commonly sizeof(T) for the reason mention earlier.
I say "commonly", because we typically store small objects in concurrent queues. In this case, my original remark stands.
rigtorp commentedon Jul 26, 2021
I see what you are saying, the padding will always be there. If overaligned new is not supported the padding will still be there. Thanks!
Better to waste some memory and be safe than sorry. Using
constexprandalignof(T)I could make padding 0 ifalignof(T) > kCacheLineSize.Remove redundant padding
rigtorp commentedon Nov 17, 2026
Both suggestions are now addressed on master. The redundant trailing padding was removed in 1053918. PR #69 now also removes buffer padding when alignof(T) is at least the false-sharing range, including equality. Types whose size matches the range but whose alignment is smaller retain padding. Regression tests cover allocation counts and boundaries, aligned elements, and queue wraparound; all CI checks pass. Thanks for the suggestions!