Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pqQt4-0004hH-VN for pgsql-general@arkaria.postgresql.org; Sun, 23 Apr 2023 03:59:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pqQt3-0004x3-GZ for pgsql-general@arkaria.postgresql.org; Sun, 23 Apr 2023 03:59:41 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pqQt3-0004wt-3P for pgsql-general@lists.postgresql.org; Sun, 23 Apr 2023 03:59:41 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1pqQt0-001Nmw-Ed for pgsql-general@lists.postgresql.org; Sun, 23 Apr 2023 03:59:39 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 33N3xbEC3765292; Sat, 22 Apr 2023 23:59:37 -0400 From: Tom Lane To: Wen Yi cc: PostgreSQL General Subject: Re: Why not use the calloc to replace malloc? In-reply-to: References: Comments: In-reply-to Wen Yi message dated "Sun, 23 Apr 2023 03:44:32 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3765290.1682222377.1@sss.pgh.pa.us> Date: Sat, 22 Apr 2023 23:59:37 -0400 Message-ID: <3765291.1682222377@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Wen Yi 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