Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit efdcca5

Browse files
committed
Make better use of the new List implementation in a couple of places
In nodeAppend.c and nodeMergeAppend.c there were some foreach loops which looped over the list of subplans and only performed any work if the subplan index was found in a Bitmapset. With the old linked list implementation of List, this form made sense as accessing the Nth list element was O(N). However, thanks to 1cff1b9 we now have array-based lists, so accessing the Nth element has become O(1). Here we make the most of the O(1) lookups and just loop over the set members of the Bitmapset with bms_next_member(). This performs slightly better when a small number of the list items are in the Bitmapset. Micro benchmarks show that when the Bitmapset contains all or most of the list items then the new code is ever so slightly slower. In practice, the cost is so small that it's drowned out by various other things such as locking the relations belonging to each subplan, etc. The primary goal here is to leave better code examples around which benefit better from the new list implementation. Reviewed-by: Tom Lane Discussion: https://postgr.es/m/CAKJS1f8ZcsLVgkF4wOfRyMYTcPgLFiUAOedFC+U2vK_aFZk-BA@mail.gmail.com
1 parent 23bccc8 commit efdcca5

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

src/backend/executor/nodeAppend.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ ExecInitAppend(Append *node, EState *estate, int eflags)
107107
int firstvalid;
108108
int i,
109109
j;
110-
ListCell *lc;
111110

112111
/* check for unsupported flags */
113112
Assert(!(eflags & EXEC_FLAG_MARK));
@@ -211,24 +210,20 @@ ExecInitAppend(Append *node, EState *estate, int eflags)
211210
*
212211
* While at it, find out the first valid partial plan.
213212
*/
214-
j = i = 0;
213+
j = 0;
215214
firstvalid = nplans;
216-
foreach(lc, node->appendplans)
215+
i = -1;
216+
while ((i = bms_next_member(validsubplans, i)) >= 0)
217217
{
218-
if (bms_is_member(i, validsubplans))
219-
{
220-
Plan *initNode = (Plan *) lfirst(lc);
218+
Plan *initNode = (Plan *) list_nth(node->appendplans, i);
221219

222-
/*
223-
* Record the lowest appendplans index which is a valid partial
224-
* plan.
225-
*/
226-
if (i >= node->first_partial_plan && j < firstvalid)
227-
firstvalid = j;
220+
/*
221+
* Record the lowest appendplans index which is a valid partial plan.
222+
*/
223+
if (i >= node->first_partial_plan && j < firstvalid)
224+
firstvalid = j;
228225

229-
appendplanstates[j++] = ExecInitNode(initNode, estate, eflags);
230-
}
231-
i++;
226+
appendplanstates[j++] = ExecInitNode(initNode, estate, eflags);
232227
}
233228

234229
appendstate->as_first_partial_plan = firstvalid;

src/backend/executor/nodeMergeAppend.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ ExecInitMergeAppend(MergeAppend *node, EState *estate, int eflags)
7070
int nplans;
7171
int i,
7272
j;
73-
ListCell *lc;
7473

7574
/* check for unsupported flags */
7675
Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
@@ -177,16 +176,13 @@ ExecInitMergeAppend(MergeAppend *node, EState *estate, int eflags)
177176
* call ExecInitNode on each of the valid plans to be executed and save
178177
* the results into the mergeplanstates array.
179178
*/
180-
j = i = 0;
181-
foreach(lc, node->mergeplans)
179+
j = 0;
180+
i = -1;
181+
while ((i = bms_next_member(validsubplans, i)) >= 0)
182182
{
183-
if (bms_is_member(i, validsubplans))
184-
{
185-
Plan *initNode = (Plan *) lfirst(lc);
183+
Plan *initNode = (Plan *) list_nth(node->mergeplans, i);
186184

187-
mergeplanstates[j++] = ExecInitNode(initNode, estate, eflags);
188-
}
189-
i++;
185+
mergeplanstates[j++] = ExecInitNode(initNode, estate, eflags);
190186
}
191187

192188
mergestate->ps.ps_ProjInfo = NULL;

0 commit comments

Comments
 (0)