Re: Simplify newNode()

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Peter Eisentraut <peter(at)eisentraut(dot)org>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, John Naylor <johncnaylorls(at)gmail(dot)com>, Thomas Munro <thomas(dot)munro(at)gmail(dot)com>
Subject: Re: Simplify newNode()
Date: 2023-12-18 14:28:31
Message-ID: b0e2c1fe-5db6-4bac-9bc8-ce5d574cd2ec@iki.fi
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 15/12/2023 00:44, Tom Lane wrote:
>> Good point. Looking closer, modern compilers will actually turn the
>> MemSetLoop() in MemoryContextAllocZeroAligned() into a call to memset()
>> anyway! Funny. That is true for recent versions of gcc, clang, and MSVC.
> Not here ...

Hmm, according to godbolt, the change happened in GCC version 10.1.
Starting with gcc 10.1, it is turned into a memset(). On clang, the same
change happened in version 3.4.1.

I think we have consensus on patch v2. It's simpler and not less
performant than what we have now, at least on modern compilers. Barring
objections, I'll commit that.

I'm not planning to spend more time on this, but there might be some
room for further optimization if someone is interested to do the
micro-benchmarking. The obvious thing would be to persuade modern
compilers to not switch to memset() in MemoryContextAllocZeroAligned
(*), making the old macro logic work the same way it used to on old
compilers.

Also, instead of palloc0, it might be better for newNode() to call
palloc followed by memset. That would allow the compiler to partially
optimize away the memset. Most callers fill at least some of the fields
after calling makeNode(), so the compiler could generate code that
clears only the uninitialized fields and padding bytes.

(*) or rather, a new function like MemoryContextAllocZeroAligned but
without the 'context' argument. We want to keep the savings in the
callers from eliminating the extra argument.

--
Heikki Linnakangas
Neon (https://neon.tech)

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Emre Hasegeli 2023-12-18 14:34:58 Re: "pgoutput" options missing on documentation
Previous Message Euler Taveira 2023-12-18 13:56:07 Re: Add --check option to pgindent