From: Tom Lane <tgl@sss.pgh.pa.us>
To: Wen Yi <chuxuec@outlook.com>
Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>
Subject: Re: Why not use the calloc to replace malloc?
Date: Sat, 22 Apr 2023 23:59:37 -0400
Message-ID: <3765291.1682222377@sss.pgh.pa.us> (raw)
In-Reply-To: <TYAP286MB0636A72D4EC3E52779FDB4F0A9669@TYAP286MB0636.JPNP286.PROD.OUTLOOK.COM>
References: <TYAP286MB0636A72D4EC3E52779FDB4F0A9669@TYAP286MB0636.JPNP286.PROD.OUTLOOK.COM>
Wen Yi <chuxuec@outlook.com> writes:
> [ use calloc to replace zeroing fields individually ]
The reason we like to do it like that is that it provides greppability,
that is you can search the source code to see where a particular field
is initialized or modified. The backend code is often intentionally
inefficient in this way: you can find a lot of places that do
makeNode(some-node-type) and then zero out fields within the node, even
though makeNode() always provides a zeroed-out struct. An important
reason why this is a good idea is that the code isn't dependent on
whether the particular value you need to initialize the field to
happens to be bitwise zeros or something else.
People have complained about this practice off-and-on, but no one has
provided any evidence that there's a significant performance cost.
The maintenance benefits are real though.
regards, tom lane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, chuxuec@outlook.com
Subject: Re: Why not use the calloc to replace malloc?
In-Reply-To: <3765291.1682222377@sss.pgh.pa.us>
References: <TYAP286MB0636A72D4EC3E52779FDB4F0A9669@TYAP286MB0636.JPNP286.PROD.OUTLOOK.COM> <3765291.1682222377@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox