| From: | Peter Eisentraut <peter(at)eisentraut(dot)org> |
|---|---|
| To: | Tristan Partin <tristan(at)partin(dot)io> |
| Cc: | pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Michael Paquier <michael(at)paquier(dot)xyz> |
| Subject: | Re: Add malloc attribute to memory allocation functions |
| Date: | 2026-11-26 10:07:57 |
| Message-ID: | 3c641b5f-ba2a-450a-8a86-2e4c72166fe5@eisentraut.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On 16.11.26 23:02, Tristan Partin wrote:
> After talking to Peter off-list, I've come up with a new strategy by
> splitting up __attribute__((malloc)) and
> __attribute__((malloc(deallocator, arg))).
>
> For those that are unaware, these two attributes, though named the same,
> have two separate meanings:
>
> malloc:
>
>> Attribute malloc indicates that a function is malloc-like, i.e., that
>> the pointer P returned by the function cannot alias any other pointer
>> valid when the function returns, and moreover no pointers to valid
>> objects occur in any storage addressed by P. In addition, GCC predicts
>> that a function with the attribute returns non-null in most cases.
> Attached are patches which add pg_attribute_malloc and apply it to
> various memory allocating functions.
This (variant of the) attribute does not provide any extra warnings or
static analysis, it only provides optimization opportunities. So I
think this should be accompanied by some evidence that this has useful
effects.
What I'm unclear about is exactly how this description is to be
interpreted for things like palloc(). The returned pointer does not
alias any other pointer that the calling function would normally use,
but the palloc machinery does (or might) save the pointer internally for
bookkeeping and cleanup, which might technically violate that
description? (But libc presumably does similar things for plain malloc?)
Also note that we already apply pg_attribute_returns_nonnull in many
cases, so some of the optimization opportunities mentioned for the
malloc attribute are already obtained that way.
The annotations in fe_memutils.h that are more straightforward wrappers
around libc functionality seem fine.
That said, you can observe pretty significant generated code size
reduction in copyfuncs.o, presumably because the compiler can now know
that the "from" node and the newly allocated "newnode" don't overlap.
So one could attempt to make an argument that this could speed up the
planner, for example. But it would be great to see some data on this.
Maybe that malloc(deallocator) attribute would have been the easier one
to start with ... ;-)
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Maksim.Melnikov | 2026-11-26 10:20:15 | Re: Infinite loop in XLogPageRead() on standby |
| Previous Message | shveta malik | 2026-11-26 09:54:48 | Re: Proposal: Conflict log history table for Logical Replication |