Re: [PATCH] Simplify trivial shmem size calculations

From: Junwang Zhao <zhjwpku(at)gmail(dot)com>
To: Tender Wang <tndrwang(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: [PATCH] Simplify trivial shmem size calculations
Date: 2026-03-10 13:32:08
Message-ID: CAEG8a3JN=CEozjfxcBzc0Jm-4MSPwqowTYiVMonQn2ddpT_h7w@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Mar 10, 2026 at 8:59 PM Tender Wang <tndrwang(at)gmail(dot)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

In response to

Responses

Browse pgsql-hackers by date

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