diff options
| author | Tom Lane | 2022-08-17 15:12:35 +0000 |
|---|---|---|
| committer | Tom Lane | 2022-08-17 15:12:35 +0000 |
| commit | efd0c16becbf45e3b0215e124fde75fee8fcbce4 (patch) | |
| tree | 4583c8e845ce18ae4a6c6b4c008a4989c514b3cb /src/backend/optimizer | |
| parent | 4a319fce7671ffbe2a730f79529b7a2ef3794d41 (diff) | |
Avoid using list_length() to test for empty list.
The standard way to check for list emptiness is to compare the
List pointer to NIL; our list code goes out of its way to ensure
that that is the only representation of an empty list. (An
acceptable alternative is a plain boolean test for non-null
pointer, but explicit mention of NIL is usually preferable.)
Various places didn't get that memo and expressed the condition
with list_length(), which might not be so bad except that there
were such a variety of ways to check it exactly: equal to zero,
less than or equal to zero, less than one, yadda yadda. In the
name of code readability, let's standardize all those spellings
as "list == NIL" or "list != NIL". (There's probably some
microscopic efficiency gain too, though few of these look to be
at all performance-critical.)
A very small number of cases were left as-is because they seemed
more consistent with other adjacent list_length tests that way.
Peter Smith, with bikeshedding from a number of us
Discussion: https://postgr.es/m/CAHut+PtQYe+ENX5KrONMfugf0q6NHg4hR5dAhqEXEc2eefFeig@mail.gmail.com
Diffstat (limited to 'src/backend/optimizer')
| -rw-r--r-- | src/backend/optimizer/path/costsize.c | 2 | ||||
| -rw-r--r-- | src/backend/optimizer/plan/createplan.c | 2 | ||||
| -rw-r--r-- | src/backend/optimizer/plan/planner.c | 12 |
3 files changed, 8 insertions, 8 deletions
diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c index fb28e6411aa..7292d68dbcf 100644 --- a/src/backend/optimizer/path/costsize.c +++ b/src/backend/optimizer/path/costsize.c @@ -1957,7 +1957,7 @@ compute_cpu_sort_cost(PlannerInfo *root, List *pathkeys, int nPresortedKeys, List *cache_varinfos = NIL; /* fallback if pathkeys is unknown */ - if (list_length(pathkeys) == 0) + if (pathkeys == NIL) { /* * If we'll use a bounded heap-sort keeping just K tuples in memory, diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c index e37f2933eb8..aa9d4e51b9e 100644 --- a/src/backend/optimizer/plan/createplan.c +++ b/src/backend/optimizer/plan/createplan.c @@ -2462,7 +2462,7 @@ create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path) if (rollup->is_hashed) strat = AGG_HASHED; - else if (list_length(linitial(rollup->gsets)) == 0) + else if (linitial(rollup->gsets) == NIL) strat = AGG_PLAIN; else strat = AGG_SORTED; diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 64632db73cd..e636596b579 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -3097,7 +3097,7 @@ reorder_grouping_sets(List *groupingsets, List *sortclause) GroupingSetData *gs = makeNode(GroupingSetData); while (list_length(sortclause) > list_length(previous) && - list_length(new_elems) > 0) + new_elems != NIL) { SortGroupClause *sc = list_nth(sortclause, list_length(previous)); int ref = sc->tleSortGroupRef; @@ -4120,7 +4120,7 @@ consider_groupingsets_paths(PlannerInfo *root, /* * If we have sorted input but nothing we can do with it, bail. */ - if (list_length(gd->rollups) == 0) + if (gd->rollups == NIL) return; /* @@ -6477,7 +6477,7 @@ add_paths_to_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel, group_clauses, orderAggPathkeys); - Assert(list_length(pathkey_orderings) > 0); + Assert(pathkey_orderings != NIL); /* process all potentially interesting grouping reorderings */ foreach(lc2, pathkey_orderings) @@ -6650,7 +6650,7 @@ add_paths_to_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel, group_clauses, orderAggPathkeys); - Assert(list_length(pathkey_orderings) > 0); + Assert(pathkey_orderings != NIL); /* process all potentially interesting grouping reorderings */ foreach(lc2, pathkey_orderings) @@ -6994,7 +6994,7 @@ create_partial_grouping_paths(PlannerInfo *root, group_clauses, orderAggPathkeys); - Assert(list_length(pathkey_orderings) > 0); + Assert(pathkey_orderings != NIL); /* process all potentially interesting grouping reorderings */ foreach(lc2, pathkey_orderings) @@ -7145,7 +7145,7 @@ create_partial_grouping_paths(PlannerInfo *root, group_clauses, orderAggPathkeys); - Assert(list_length(pathkey_orderings) > 0); + Assert(pathkey_orderings != NIL); /* process all potentially interesting grouping reorderings */ foreach(lc2, pathkey_orderings) |
