Re: [PATCH] Simplify trivial shmem size calculations - Mailing list pgsql-hackers

From Junwang Zhao
Subject Re: [PATCH] Simplify trivial shmem size calculations
Date
Msg-id CAEG8a3JN=CEozjfxcBzc0Jm-4MSPwqowTYiVMonQn2ddpT_h7w@mail.gmail.com
Whole thread Raw
In response to [PATCH] Simplify trivial shmem size calculations  (Tender Wang <tndrwang@gmail.com>)
Responses Re: [PATCH] Simplify trivial shmem size calculations
List pgsql-hackers
On Tue, Mar 10, 2026 at 8:59 PM Tender Wang <tndrwang@gmail.com> wrote:
>
> Hi,
>
> While learning how PostgreSQL implements streaming replication, I
> noticed that WalRcvShmemSize() calculating the shared
> memory sizes that use a multi-step add_size pattern for single,
> fixed-size structures.
> ...
> Size
> WalRcvShmemSize(void)
> {
>     Size        size = 0;
>
>     size = add_size(size, sizeof(WalRcvData));
>
>     return size;
> }
> ...
>
> "return sizeof(WalRcvData);" is equal to the above code.
> I searched the other modules to see how they perform the calculations.
> For example,
> ...
> Size
> VarsupShmemSize(void)
> {
>     return sizeof(TransamVariablesData);
> }
> ...
> And I also found another case:
> Size
> XLogRecoveryShmemSize(void)
> {
>     Size        size;
>
>     /* XLogRecoveryCtl */
>     size = sizeof(XLogRecoveryCtlData);
>
>     return size;
> }
>
> The above code does not need to define the local variable size;
> directly returning sizeof(XLogRecoveryCtlData) seems simpler.

I bet the compiler will optimize it away.

> I searched for other XXXShmemSize() calls in CalculateShmemSize() and
> simplified them where possible.
> Please see the attached patch.
>
> I realize this might be a matter of coding style preference rather
> than a functional necessity.
> Is it worth standardizing these cases, or should we stick with the
> current boilerplate for consistency?
> Any thoughts?
>
> --
> Thanks,
> Tender Wang



--
Regards
Junwang Zhao



pgsql-hackers by date:

Previous
From: Robert Haas
Date:
Subject: Re: Potential security risk associated with function call
Next
From: Madyshev Egor
Date:
Subject: RE: Idea to enhance pgbench by more modes to generate data (multi-TXNs, UNNEST, COPY BINARY)