Replace padding fields with alignas
for memory alignment in ConcurrentArena
.
#12705
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Each shard begins with 40 bytes of padding, bringing the size of
Shard
to 64 bytes, a common cache line size for many CPUs. However, this is insufficient to prevent CPU false sharing. The base address must also be a multiple of a cache line. The classCoreLocalArray<>
does not explicitly align the block of contiguousT
objects to an address that is a multiple of a cache line.By applying
alignas(64)
to theShard
structure, we achieve two things. First, the compiler adds padding at the end, maintaining the object size at 64 bytes. Second, the new operator honorsalignas
, performing a 64-byte aligned memory allocation forT[]
.Similarly,
ConcurrentArena
currently uses padding fields before and after the essential fields. While this technique helps prevent false sharing with adjacent fields of aConcurrentArena
object, it wastes memory and reduces code readability. Usingalignas(64)
on ConcurrentArena would ensure cache line alignment of the object (reducing its size from 2472 to 2364 bytes). Additionally, it guarantees cache line alignment for any object containing aConcurrentArena
field, including heap-allocated objects.Manual verification using static_asserts (not included in this PR). No functional changes.