Re: Why not use the calloc to replace malloc?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Wen Yi <chuxuec(at)outlook(dot)com>
Cc: PostgreSQL General <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Why not use the calloc to replace malloc?
Date: 2023-04-23 03:59:37
Message-ID: 3765291.1682222377@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Wen Yi <chuxuec(at)outlook(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Thorsten Glaser 2023-04-23 14:21:48 Re: Why not use the calloc to replace malloc?
Previous Message Wen Yi 2023-04-23 03:44:32 Why not use the calloc to replace malloc?