Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace padding fields with alignas for memory alignment in ConcurrentArena. #12705

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

aractnido
Copy link

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 class CoreLocalArray<> does not explicitly align the block of contiguous T objects to an address that is a multiple of a cache line.

By applying alignas(64) to the Shard structure, we achieve two things. First, the compiler adds padding at the end, maintaining the object size at 64 bytes. Second, the new operator honors alignas, performing a 64-byte aligned memory allocation for T[].

Similarly, ConcurrentArena currently uses padding fields before and after the essential fields. While this technique helps prevent false sharing with adjacent fields of a ConcurrentArena object, it wastes memory and reduces code readability. Using alignas(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 a ConcurrentArena field, including heap-allocated objects.

Manual verification using static_asserts (not included in this PR). No functional changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants