Re: Add new for_each macros for iterating over a List that do not require ListCell pointer

From: Nathan Bossart <nathandbossart(at)gmail(dot)com>
To: Jelte Fennema <postgres(at)jeltef(dot)nl>
Cc: David Rowley <dgrowleyml(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Add new for_each macros for iterating over a List that do not require ListCell pointer
Date: 2023-12-01 04:20:20
Message-ID: 20231201042020.GA1577008@nathanxps13
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Oct 25, 2023 at 12:39:01PM +0200, Jelte Fennema wrote:
> Attached is a slightly updated version, with a bit simpler
> implementation of foreach_delete_current.
> Instead of decrementing i and then adding 1 to it when indexing the
> list, it now indexes the list using a postfix decrement.

Both the macros and the comments in 0001 seem quite repetitive to me.
Could we simplify it with something like the following?

#define foreach_internal(var, lst, func) \
for (ForEachState var##__state = {(lst), 0}; \
(var##__state.l != NIL && \
var##__state.i < var##__state.l->length && \
(var = func(&var##__state.l->elements[var##__state.i]), true)); \
var##__state.i++)

#define foreach_ptr(var, lst) foreach_internal(var, lst, lfirst)
#define foreach_int(var, lst) foreach_internal(var, lst, lfirst_int)
#define foreach_oid(var, lst) foreach_internal(var, lst, lfirst_oid)
#define foreach_xid(var, lst) foreach_internal(var, lst, lfirst_xid)

#define foreach_node(type, var, lst) \
for (ForEachState var##__state = {(lst), 0}; \
(var##__state.l != NIL && \
var##__state.i < var##__state.l->length && \
(var = lfirst_node(type, &var##__state.l->elements[var##__state.i]), true));\
var##__state.i++)

There might be a way to use foreach_internal for foreach_node, too, but
this is probably already too magical...

--
Nathan Bossart
Amazon Web Services: https://aws.amazon.com

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Nathan Bossart 2023-12-01 04:35:22 Re: optimize atomic exchanges
Previous Message Zhijie Hou (Fujitsu) 2023-12-01 04:10:21 RE: Synchronizing slots from primary to standby