Re: Add new for_each macros for iterating over a List that do not require ListCell pointer - Mailing list pgsql-hackers

From Nathan Bossart
Subject Re: Add new for_each macros for iterating over a List that do not require ListCell pointer
Date
Msg-id 20231201042020.GA1577008@nathanxps13
Whole thread Raw
In response to Re: Add new for_each macros for iterating over a List that do not require ListCell pointer  (Jelte Fennema <postgres@jeltef.nl>)
Responses Re: Add new for_each macros for iterating over a List that do not require ListCell pointer
List 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



pgsql-hackers by date:

Previous
From: "Zhijie Hou (Fujitsu)"
Date:
Subject: RE: Synchronizing slots from primary to standby
Next
From: Nathan Bossart
Date:
Subject: Re: optimize atomic exchanges