Re: Use C11 alignas instead of palloc/malloc for alignment

From: best xmg <bestxmg(at)gmail(dot)com>
To: peter(at)eisentraut(dot)org
Cc: zhaorui126(at)gmail(dot)com, pgsql-hackers(at)lists(dot)postgresql(dot)org, hlinnaka(at)iki(dot)fi, andres(at)anarazel(dot)de
Subject: Re: Use C11 alignas instead of palloc/malloc for alignment
Date: 2026-11-08 01:58:33
Message-ID: CABkqMMu-DpTsAnmwwmuFNuqeYmrAPfYZS8WjJtEhbPSdNVJ5Bg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Peter,

While reading v4, I noticed a small thing in _mdfd_getseg() and
dsm_impl_mmap(). The new zero buffers there are const locals:

const PGIOAlignedBlock zerobuf = {0};
alignas(MAXIMUM_ALIGNOF) const char zbuffer[ZBUFFER_SIZE] = {0};

So each time that code runs, 8 kB is filled on the stack. With GCC 15,
_mdfd_getseg() copies an 8 kB zero block into a stack frame of about
20 kB, and dsm_impl_mmap() clears 8 kB.

The buffers are never written, so they could be static, as in
pg_pwrite_zeros():

static const PGIOAlignedBlock zbuffer = {0};

Then nothing is copied or cleared, and the stack frame of
_mdfd_getseg() drops to a few hundred bytes. Because the buffers are
read-only, they need no thread_local. So I think the concern upthread
about function-level statics does not apply here.

I tested this on top of v4, with 6-block segments and
dynamic_shared_memory_type = mmap. The regression tests pass. A hash
index spread over 217 segments also works, before and after crash
recovery.

Regards,
Linden

On Wed, Oct 28, 2026 04:11 PM, Rui Zhao <zhaorui126(at)gmail(dot)com> wrote:

> Hi Peter,
>
> Thanks very much for replacing allocations that were only needed for
> alignment. I reviewed v4 and did not find any substantive issues.
>
> I checked the alignment, and it is fine:
> on x86-64 Linux, I get MAXIMUM_ALIGNOF = 8 and alignof(max_align_t) = 16.
> However, alignof(ControlFileData) is also 8,
> so MAXIMUM_ALIGNOF is sufficient for the buffer in pg_resetwal's
> read_controlfile().
>
> The core regression suite and the pg_resetwal and pg_waldump tests passed.
>
> Regards,
> Rui
>
>
>

In response to

Browse pgsql-hackers by date

  From Date Subject
Previous Message Bruce Momjian 2026-11-08 01:20:35 Re: First draft of PG 19 release notes