Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vTbhP-005mEW-2h for pgsql-hackers@arkaria.postgresql.org; Thu, 11 Dec 2025 08:06:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vTbhO-002yv5-23 for pgsql-hackers@arkaria.postgresql.org; Thu, 11 Dec 2025 08:06:55 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vTbhO-002yux-16 for pgsql-hackers@lists.postgresql.org; Thu, 11 Dec 2025 08:06:55 +0000 Received: from mail-ej1-x630.google.com ([2a00:1450:4864:20::630]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vTbhN-0006xA-2T for pgsql-hackers@postgresql.org; Thu, 11 Dec 2025 08:06:54 +0000 Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-b77030ffad9so99221466b.0 for ; Thu, 11 Dec 2025 00:06:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765440412; x=1766045212; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=h3RvSCrvL01xAZqB2AitOLYCdZR+lCU2rNE4lhyCYn4=; b=WDAl4kG5EBpQeJllBJs/7tH6qTYdTtPJf+YxBogZR62U33aLNZwcErHhfSCYaKl+jH jv0guZiPeep8HWPXzSQvmpWhjIkY6YQVOKdCVGFRhgOSVOueVTPY9kPwWsf+xmbpi19N KcjAd7B4N/nWG/bImZFY1HsPbDnVvvdufVa51f2sG4kq9iHOmSL3/UGdTSFAvyvZ8Jts H7CByiXLo/zKAEWf4h93IEhJlSM2d4gdHBnAgKDolUi0QWm+VrO7ScE84PsZeKUnBmz1 LXa42OhVOPcGPr5k7RCN87LgFwOqXLSohrpp5JhrNuIHK+mvAthkj8KdVVh6hbhJdOWY +thA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765440412; x=1766045212; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=h3RvSCrvL01xAZqB2AitOLYCdZR+lCU2rNE4lhyCYn4=; b=UqIfJs47ptvJ3MadvT2ZVuk87oLi1pH5F3cBT/Q7P0L7JGbJSBr+i+zhIPje/oto00 uXNdjWHLaj0Wcs6nxCiATh7rnKaMl38QbikFS+KLND51H+VPyg+R465EIUvazEZWnMOY fcTAKH8T3dOGRicReOjOn+RyuAjAPbXkf9qzxQ/HCzuG5pkdAhd+QxlxFrR/bmmEgdp0 bgQRHxt75d4IxFlf9xA6NNiYa/eMAyJOfQQxwP0KxRn9DRIyQAlCpFRIwU6dSWRiFBLa 5f+lZ7F9Fn+D5BHVWkTZLWy+2syutZfeHrFPHt74n+7UEGCfeaGTjpSNm0mgC/3j2LF9 gXuA== X-Forwarded-Encrypted: i=1; AJvYcCVOL1ki+KoZpo6qgFNcoJtubC6yc78A8TkKzQRpoBZLPaoeSs8edqsbymJTIG950LhNrH75y2a4CjTM9iX3@postgresql.org X-Gm-Message-State: AOJu0YyvTyV4IpYPVu3mSpAoq1WDEX08jy6LU3D1nUCdKB0kCU4ZkgOw 2JsDOmLrBJk0oR7J7PidW/LtXhkagP1q1U1odFxngxW7racteOP4kR3e X-Gm-Gg: ASbGnctlTn0Q797mhFU4VMdVl2ZMFnzPBw3536+hE2lSHSm+r2t4n+i5OwgwN54jlmM 49W5iUy6bLBusu5liwdjv6ST55TGtu1hHcuH/qzV/dd09yaSLMyii/IlgOBzaKI6VUc7y7NCKUs o2K/t8cSWUY2y2IagA3uDKxjpwyOxsWyQD40kgOpNgJF+QkjKAqRo0F0TV5SGWk11VxwjFP3Bfq wdZtQY61Tm81ngpEXnVZadZ7xiA2cFdR0ydtlsdCY608T1NHyoh2mLTGRl8XrpGG4IgLrexavvj PeOK7/sgxGI18vCwcFHDbJzWqEQk7QF0bXr7xnlYLEd7c6MnJdxJfo2JQw758twHiwK1qU4jaLV HRx1VW1aoT5HZqgTog9+ci1Q7G0jjCsoVa+hE7vHFNns4YfRdMbSwD3d7dzH+Yqrx1+PB0kgK88 A49iXerNRHmjDIU5E= X-Google-Smtp-Source: AGHT+IFDmCxvR7JEIV5+acjlS46fXBfBMQsxXoa+AFX8Pi8fhaFDVzjzS4gxGl01nNmZ+ySGWWFAVQ== X-Received: by 2002:a17:906:c104:b0:b73:6d56:7332 with SMTP id a640c23a62f3a-b7ce82d345bmr674537966b.13.1765440411700; Thu, 11 Dec 2025 00:06:51 -0800 (PST) Received: from [192.168.2.32] ([165.225.27.7]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b7cfa29f6cbsm199321766b.5.2025.12.11.00.06.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 11 Dec 2025 00:06:51 -0800 (PST) Message-ID: Date: Thu, 11 Dec 2025 09:06:50 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Consistently use palloc_object() and palloc_array() To: Michael Paquier Cc: Tom Lane , Thomas Munro , Chao Li , pgsql-hackers References: <1e7f0c17-a965-4d5b-ad88-b435a4d97490@gmail.com> <143d3184-b575-49bd-8bc1-f37f45f010cc@gmail.com> <534126.1764722446@sss.pgh.pa.us> <4ab4a12b-49e9-4ebf-9055-225c8055fed0@gmail.com> Content-Language: en-US From: David Geier In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 11.12.2025 06:33, Michael Paquier wrote: > On Thu, Dec 11, 2025 at 08:01:49AM +0900, Michael Paquier wrote: >> I still need to get through the remaining dubious changes you have >> posted, including the llvm one that was wrong. It seems like some of >> these things warrant a backpatch. > > I have been looking at the rest of these changes with some -O2, and I > have been puzzled by the differences in hstore_gin.c and hstore_op.c. > In all these cases we generate more instructions with the patch than > without the patch. Anyway, I assume that these are the ones that > matter: > entries = (Datum *) palloc(sizeof(Datum) * 2 * count); > out_datums = palloc_array(Datum, count * 2); Yeah, in a few cases the different bracketing has some surprisingly big influence on the generated assmebly. > pg_trgm.c was less puzzling. For example: > - trg1 = (trgm *) palloc(sizeof(trgm) * (slen1 / 2 + 1) * 3); > + trg1 = palloc_array(trgm, (slen1 / 2 + 1) * 3); > This one leads to something like that before vs after: > < 1418: 83 c0 01 add eax,0x1 >> 1418: 8d 44 40 03 lea eax,[rax+rax*2+0x3] > > In execPartition.c and partprune.c, as far as I can see we are cutting > a few mov, leading to less instructions overall. > > For mvdistinct.c, we are cutting things overall. I am seeing less. > > All the fuzz in postgres_fdw.c is caused by this one: > - p_values = (const char **) palloc(sizeof(char *) * fmstate->p_nums > * numSlots); > + p_values = palloc_array(const char *, fmstate->p_nums * numSlots); > > bufmgr.c did not matter, same before and after. > > I am not completely sure about the one in fuzzystrmatch, I would need > to study more the metaphone code. :) Actually, the change in fuzzystrmatch.c is incorrect. With the new code, palloc(0) will be called for strlen(word) == 0. Then no space for the null-terminator byte will be left. Even if palloc(0) returns memory that can be dereferenced, I don't think it's a good idea to rely on that. > One formula in llvmjit_expr.c has been wrong since v10, so I have > backpatched a fix for it. > > In pg_buffercache_pages.c, the difference is here: > - os_page_status = palloc(sizeof(uint64) * os_page_count); > + os_page_status = palloc_array(int, os_page_count); > Your formula is correct, the previous one was not by using a uint64. > So it allocated twice too much memory. Backpatched this one down to > v18. > > And that should close this thread, at least from my side.. Thanks Michael for taking care of this patch! -- David Geier