From: | Heikki Linnakangas <hlinnaka(at)iki(dot)fi> |
---|---|
To: | Peter Eisentraut <peter(at)eisentraut(dot)org>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: Simplify newNode() |
Date: | 2023-12-14 13:53:27 |
Message-ID: | 9102d38c-ee1c-447c-baba-2bb1d7e1faf5@iki.fi |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
On 14/12/2023 10:32, Peter Eisentraut wrote:
> I notice that the existing comments point out that the size argument
> should be a compile-time constant, but that is no longer the case for
> ExtensibleNode(). Also, newNode() is the only caller of palloc0fast(),
> which also points out that the size argument should be a compile-time
> constant, and palloc0fast() is the only caller of MemSetTest(). I can
> see how an older compiler might have gotten too confused by all that.
> But if we think that compilers are now smart enough, maybe we can unwind
> this whole stack a bit more? Maybe we don't need MemSetTest() and/or
> palloc0fast() and/or newNode() at all?
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.
Here's a link to a godbolt snippet to play with this:
https://godbolt.org/z/9b89P3c8x (full link at [0]).
Yeah, +1 on removing all that, including MemoryContextAllocZeroAligned.
It's not doing any good as it is, as it gets compiled to be identical to
MemoryContextAllocZero. (There are small differences depending compiler
and version, but e.g. on gcc 13.2, the code generated for
MemoryContextAllocZero() is actually smaller even though both call memset())
Another approach would be to add more hints to
MemoryContextAllocZeroAligned to dissuade the compiler from turning the
loop into a memset() call. If you add an "if (size > 1024) abort" there,
then gcc 13 doesn't optimize into a memset() call, but clang still does.
Some micro-benchmarks on that would be nice.
But given that the compiler has been doing this optimization for a while
and we haven't noticed, I think memset() should be considered the status
quo, and converting it to a loop again should be considered a new
optimization.
Also, replacing MemoryContextAllocZeroAligned(CurrentMemoryContext,
size) with palloc0(size) has one fewer argument and the assembly code of
the call has one fewer instruction. That's something too.
[0]
https://godbolt.org/#z:OYLghAFBqd5QCxA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esE0vBRtaCHaQNrHX9CcB66QIrJWg%2BDeG415rOeusi%2BL31svlexs5/F8kZwkggA%3D%3D
--
Heikki Linnakangas
Neon (https://neon.tech)
Attachment | Content-Type | Size |
---|---|---|
v2-0001-Simplify-newNode-by-removing-now-unnecessary-fast.patch | text/x-patch | 9.0 KB |
From | Date | Subject | |
---|---|---|---|
Next Message | Jeff Davis | 2023-12-14 14:01:59 | Re: Built-in CTYPE provider |
Previous Message | Nazir Bilal Yavuz | 2023-12-14 13:40:29 | Re: Build the docs if there are changes in docs and don't run other tasks if the changes are only in docs |