Re: Small improvements to pg_list.h's linitial(), lsecond(), lthird() etc macros
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: David Rowley <dgrowleyml@gmail.com>
Cc: PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>
Date: 2020-09-28T22:37:52Z
Lists: pgsql-hackers
Attachments
- introduce-for_each_from.patch (text/x-diff) patch
I wrote: > list_second_cell() does have uses, although I observe that they > are almost exclusively in locutions such as > for_each_cell(lc, rollups, list_second_cell(rollups)) > to iterate over all-but-the-first elements of a list. I wonder if > we ought to come up with a better notation for that. I'm imagining > something like > for_each_from(lc, rollups, 1) > to start from list index 1. It looks like this would be marginally > more efficient, and perhaps more readable. Concretely, I'm thinking about the attached. This does seem simpler. It reduces the size of the executable by about 560 bytes on my machine, or 46 bytes per usage, which isn't bad. (Note: this is in an assert-enabled build, might be different otherwise.) I didn't try to measure performance changes, but it should be for the better. Looking at the remaining instances of for_each_cell, I see several where it seems like it'd be simpler and clearer to use for_each_from. But for the moment I confined myself to changing just the instances following the pattern above. I noticed while messing with this that I'd neglected to const-ify the support functions for for_each_cell() and for_both_cell(), so this fixes that too. I'm somewhat inclined to back-patch this into v13. The missing const decoration seems arguably a bug, which we've missed noticing only because of our generally lamentable under-usage of const. And I think it'll be helpful for future back-patching if for_each_from is available in all versions with the new List API. regards, tom lane
Commits
-
Add for_each_from, to simplify loops starting from non-first list cells.
- 56fe008996bc 14.0 landed
- 67b2ceea0157 13.1 landed
-
Minor mop-up for List improvements.
- 9d299a492454 14.0 landed
-
Improve pg_list.h's linitial(), lsecond() and co macros
- cc99baa43e0e 14.0 landed