Обсуждение: [PATCH] Simplify trivial shmem size calculations
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 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
Вложения
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
On Tue, Mar 10, 2026 at 08:59:22PM +0800, Tender Wang wrote: > 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? I think it's fine as it is. -- nathan
Hi,
Junwang Zhao <zhjwpku@gmail.com> 于2026年3月10日周二 21:32写道:
> > 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.
Yeah, XLogRecoveryShmemSize() may be optimized by the compiler, I only
want the codes to look more consistent.
>I think it's fine as it is.
Ok.
--
Thanks,
Tender Wang