From 09adc9a8c09c9640de05c7023b27fb83c761e91c Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Tue, 5 Apr 2016 15:47:49 -0400 Subject: [PATCH] Align all shared memory allocations to cache line boundaries. Experimentation shows this only costs about 6kB, which seems well worth it given the major performance effects that can be caused by insufficient alignment, especially on larger systems. Discussion: 14166.1458924422@sss.pgh.pa.us --- src/backend/storage/ipc/shmem.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c index 81506eaffd0..1ad68cda4e5 100644 --- a/src/backend/storage/ipc/shmem.c +++ b/src/backend/storage/ipc/shmem.c @@ -171,9 +171,17 @@ ShmemAlloc(Size size) void *newSpace; /* - * ensure all space is adequately aligned. + * Ensure all space is adequately aligned. We used to only MAXALIGN this + * space but experience has proved that on modern systems that is not good + * enough. Many parts of the system are very sensitive to critical data + * structures getting split across cache line boundaries. To avoid that, + * attempt to align the beginning of the allocation to a cache line + * boundary. The calling code will still need to be careful about how it + * uses the allocated space - e.g. by padding each element in an array of + * structures out to a power-of-two size - but without this, even that + * won't be sufficient. */ - size = MAXALIGN(size); + size = CACHELINEALIGN(size); Assert(ShmemSegHdr != NULL);