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

Commit efd0c16

Browse files
committed
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
1 parent 4a319fc commit efd0c16

File tree

28 files changed

+47
-48
lines changed

28 files changed

+47
-48
lines changed

src/backend/catalog/objectaddress.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2186,7 +2186,7 @@ pg_get_object_address(PG_FUNCTION_ARGS)
21862186
else
21872187
{
21882188
name = textarray_to_strvaluelist(namearr);
2189-
if (list_length(name) < 1)
2189+
if (name == NIL)
21902190
ereport(ERROR,
21912191
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
21922192
errmsg("name list length must be at least %d", 1)));

src/backend/catalog/pg_depend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ getIdentitySequence(Oid relid, AttrNumber attnum, bool missing_ok)
947947

948948
if (list_length(seqlist) > 1)
949949
elog(ERROR, "more than one owned sequence found");
950-
else if (list_length(seqlist) < 1)
950+
else if (seqlist == NIL)
951951
{
952952
if (missing_ok)
953953
return InvalidOid;

src/backend/commands/event_trigger.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,9 +1143,9 @@ trackDroppedObjectsNeeded(void)
11431143
* true if any sql_drop, table_rewrite, ddl_command_end event trigger
11441144
* exists
11451145
*/
1146-
return list_length(EventCacheLookup(EVT_SQLDrop)) > 0 ||
1147-
list_length(EventCacheLookup(EVT_TableRewrite)) > 0 ||
1148-
list_length(EventCacheLookup(EVT_DDLCommandEnd)) > 0;
1146+
return (EventCacheLookup(EVT_SQLDrop) != NIL) ||
1147+
(EventCacheLookup(EVT_TableRewrite) != NIL) ||
1148+
(EventCacheLookup(EVT_DDLCommandEnd) != NIL);
11491149
}
11501150

11511151
/*
@@ -1616,7 +1616,7 @@ EventTriggerAlterTableEnd(void)
16161616
parent = currentEventTriggerState->currentCommand->parent;
16171617

16181618
/* If no subcommands, don't collect */
1619-
if (list_length(currentEventTriggerState->currentCommand->d.alterTable.subcmds) != 0)
1619+
if (currentEventTriggerState->currentCommand->d.alterTable.subcmds != NIL)
16201620
{
16211621
MemoryContext oldcxt;
16221622

src/backend/commands/functioncmds.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ interpret_function_parameter_list(ParseState *pstate,
419419
* Make sure no variables are referred to (this is probably dead
420420
* code now that add_missing_from is history).
421421
*/
422-
if (list_length(pstate->p_rtable) != 0 ||
422+
if (pstate->p_rtable != NIL ||
423423
contain_var_clause(def))
424424
ereport(ERROR,
425425
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
@@ -1209,7 +1209,7 @@ CreateFunction(ParseState *pstate, CreateFunctionStmt *stmt)
12091209
returnsSet = false;
12101210
}
12111211

1212-
if (list_length(trftypes_list) > 0)
1212+
if (trftypes_list != NIL)
12131213
{
12141214
ListCell *lc;
12151215
Datum *arr;

src/backend/commands/publicationcmds.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -848,12 +848,12 @@ CreatePublication(ParseState *pstate, CreatePublicationStmt *stmt)
848848
&schemaidlist);
849849

850850
/* FOR ALL TABLES IN SCHEMA requires superuser */
851-
if (list_length(schemaidlist) > 0 && !superuser())
851+
if (schemaidlist != NIL && !superuser())
852852
ereport(ERROR,
853853
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
854854
errmsg("must be superuser to create FOR ALL TABLES IN SCHEMA publication"));
855855

856-
if (list_length(relations) > 0)
856+
if (relations != NIL)
857857
{
858858
List *rels;
859859

@@ -871,7 +871,7 @@ CreatePublication(ParseState *pstate, CreatePublicationStmt *stmt)
871871
CloseTableList(rels);
872872
}
873873

874-
if (list_length(schemaidlist) > 0)
874+
if (schemaidlist != NIL)
875875
{
876876
/*
877877
* Schema lock is held until the publication is created to prevent

src/backend/commands/statscmds.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ CreateStatistics(CreateStatsStmt *stmt)
339339
if ((list_length(stmt->exprs) == 1) && (list_length(stxexprs) == 1))
340340
{
341341
/* statistics kinds not specified */
342-
if (list_length(stmt->stat_types) > 0)
342+
if (stmt->stat_types != NIL)
343343
ereport(ERROR,
344344
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
345345
errmsg("when building statistics on a single expression, statistics kinds may not be specified")));
@@ -391,7 +391,7 @@ CreateStatistics(CreateStatsStmt *stmt)
391391
* automatically. This allows calculating good estimates for stats that
392392
* consider per-clause estimates (e.g. functional dependencies).
393393
*/
394-
build_expressions = (list_length(stxexprs) > 0);
394+
build_expressions = (stxexprs != NIL);
395395

396396
/*
397397
* Check that at least two columns were specified in the statement, or

src/backend/commands/subscriptioncmds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ get_publications_str(List *publications, StringInfo dest, bool quote_literal)
410410
ListCell *lc;
411411
bool first = true;
412412

413-
Assert(list_length(publications) > 0);
413+
Assert(publications != NIL);
414414

415415
foreach(lc, publications)
416416
{

src/backend/commands/tablecmds.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,7 +2097,7 @@ ExecuteTruncateGuts(List *explicit_rels,
20972097
* Assemble an array of relids so we can write a single WAL record for the
20982098
* whole action.
20992099
*/
2100-
if (list_length(relids_logged) > 0)
2100+
if (relids_logged != NIL)
21012101
{
21022102
xl_heap_truncate xlrec;
21032103
int i = 0;
@@ -16264,11 +16264,11 @@ ATPrepChangePersistence(Relation rel, bool toLogged)
1626416264
}
1626516265

1626616266
/*
16267-
* Check that the table is not part any publication when changing to
16268-
* UNLOGGED as UNLOGGED tables can't be published.
16267+
* Check that the table is not part of any publication when changing to
16268+
* UNLOGGED, as UNLOGGED tables can't be published.
1626916269
*/
1627016270
if (!toLogged &&
16271-
list_length(GetRelationPublications(RelationGetRelid(rel))) > 0)
16271+
GetRelationPublications(RelationGetRelid(rel)) != NIL)
1627216272
ereport(ERROR,
1627316273
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1627416274
errmsg("cannot change table \"%s\" to unlogged because it is part of a publication",

src/backend/commands/typecmds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3503,7 +3503,7 @@ domainAddConstraint(Oid domainOid, Oid domainNamespace, Oid baseTypeOid,
35033503
* Domains don't allow variables (this is probably dead code now that
35043504
* add_missing_from is history, but let's be sure).
35053505
*/
3506-
if (list_length(pstate->p_rtable) != 0 ||
3506+
if (pstate->p_rtable != NIL ||
35073507
contain_var_clause(expr))
35083508
ereport(ERROR,
35093509
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),

src/backend/executor/execPartition.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate,
685685
* list and searching for ancestry relationships to each index in the
686686
* ancestor table.
687687
*/
688-
if (list_length(rootResultRelInfo->ri_onConflictArbiterIndexes) > 0)
688+
if (rootResultRelInfo->ri_onConflictArbiterIndexes != NIL)
689689
{
690690
List *childIdxs;
691691

src/backend/libpq/auth.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2915,14 +2915,14 @@ CheckRADIUSAuth(Port *port)
29152915
Assert(offsetof(radius_packet, vector) == 4);
29162916

29172917
/* Verify parameters */
2918-
if (list_length(port->hba->radiusservers) < 1)
2918+
if (port->hba->radiusservers == NIL)
29192919
{
29202920
ereport(LOG,
29212921
(errmsg("RADIUS server not specified")));
29222922
return STATUS_ERROR;
29232923
}
29242924

2925-
if (list_length(port->hba->radiussecrets) < 1)
2925+
if (port->hba->radiussecrets == NIL)
29262926
{
29272927
ereport(LOG,
29282928
(errmsg("RADIUS secret not specified")));

src/backend/libpq/hba.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,7 @@ parse_hba_line(TokenizedAuthLine *tok_line, int elevel)
15641564
MANDATORY_AUTH_ARG(parsedline->radiusservers, "radiusservers", "radius");
15651565
MANDATORY_AUTH_ARG(parsedline->radiussecrets, "radiussecrets", "radius");
15661566

1567-
if (list_length(parsedline->radiusservers) < 1)
1567+
if (parsedline->radiusservers == NIL)
15681568
{
15691569
ereport(elevel,
15701570
(errcode(ERRCODE_CONFIG_FILE_ERROR),
@@ -1575,7 +1575,7 @@ parse_hba_line(TokenizedAuthLine *tok_line, int elevel)
15751575
return NULL;
15761576
}
15771577

1578-
if (list_length(parsedline->radiussecrets) < 1)
1578+
if (parsedline->radiussecrets == NIL)
15791579
{
15801580
ereport(elevel,
15811581
(errcode(ERRCODE_CONFIG_FILE_ERROR),

src/backend/optimizer/path/costsize.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,7 @@ compute_cpu_sort_cost(PlannerInfo *root, List *pathkeys, int nPresortedKeys,
19571957
List *cache_varinfos = NIL;
19581958

19591959
/* fallback if pathkeys is unknown */
1960-
if (list_length(pathkeys) == 0)
1960+
if (pathkeys == NIL)
19611961
{
19621962
/*
19631963
* If we'll use a bounded heap-sort keeping just K tuples in memory,

src/backend/optimizer/plan/createplan.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2462,7 +2462,7 @@ create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path)
24622462

24632463
if (rollup->is_hashed)
24642464
strat = AGG_HASHED;
2465-
else if (list_length(linitial(rollup->gsets)) == 0)
2465+
else if (linitial(rollup->gsets) == NIL)
24662466
strat = AGG_PLAIN;
24672467
else
24682468
strat = AGG_SORTED;

src/backend/optimizer/plan/planner.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3097,7 +3097,7 @@ reorder_grouping_sets(List *groupingsets, List *sortclause)
30973097
GroupingSetData *gs = makeNode(GroupingSetData);
30983098

30993099
while (list_length(sortclause) > list_length(previous) &&
3100-
list_length(new_elems) > 0)
3100+
new_elems != NIL)
31013101
{
31023102
SortGroupClause *sc = list_nth(sortclause, list_length(previous));
31033103
int ref = sc->tleSortGroupRef;
@@ -4120,7 +4120,7 @@ consider_groupingsets_paths(PlannerInfo *root,
41204120
/*
41214121
* If we have sorted input but nothing we can do with it, bail.
41224122
*/
4123-
if (list_length(gd->rollups) == 0)
4123+
if (gd->rollups == NIL)
41244124
return;
41254125

41264126
/*
@@ -6477,7 +6477,7 @@ add_paths_to_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel,
64776477
group_clauses,
64786478
orderAggPathkeys);
64796479

6480-
Assert(list_length(pathkey_orderings) > 0);
6480+
Assert(pathkey_orderings != NIL);
64816481

64826482
/* process all potentially interesting grouping reorderings */
64836483
foreach(lc2, pathkey_orderings)
@@ -6650,7 +6650,7 @@ add_paths_to_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel,
66506650
group_clauses,
66516651
orderAggPathkeys);
66526652

6653-
Assert(list_length(pathkey_orderings) > 0);
6653+
Assert(pathkey_orderings != NIL);
66546654

66556655
/* process all potentially interesting grouping reorderings */
66566656
foreach(lc2, pathkey_orderings)
@@ -6994,7 +6994,7 @@ create_partial_grouping_paths(PlannerInfo *root,
69946994
group_clauses,
69956995
orderAggPathkeys);
69966996

6997-
Assert(list_length(pathkey_orderings) > 0);
6997+
Assert(pathkey_orderings != NIL);
69986998

69996999
/* process all potentially interesting grouping reorderings */
70007000
foreach(lc2, pathkey_orderings)
@@ -7145,7 +7145,7 @@ create_partial_grouping_paths(PlannerInfo *root,
71457145
group_clauses,
71467146
orderAggPathkeys);
71477147

7148-
Assert(list_length(pathkey_orderings) > 0);
7148+
Assert(pathkey_orderings != NIL);
71497149

71507150
/* process all potentially interesting grouping reorderings */
71517151
foreach(lc2, pathkey_orderings)

src/backend/partitioning/partprune.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2383,7 +2383,7 @@ get_steps_using_prefix(GeneratePruningStepsContext *context,
23832383
context->rel->part_scheme->strategy == PARTITION_STRATEGY_HASH);
23842384

23852385
/* Quick exit if there are no values to prefix with. */
2386-
if (list_length(prefix) == 0)
2386+
if (prefix == NIL)
23872387
{
23882388
PartitionPruneStep *step;
23892389

src/backend/replication/logical/tablesync.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ process_syncing_tables_for_apply(XLogRecPtr current_lsn)
383383
* immediate restarts. We don't need it if there are no tables that need
384384
* syncing.
385385
*/
386-
if (table_states_not_ready && !last_start_times)
386+
if (table_states_not_ready != NIL && !last_start_times)
387387
{
388388
HASHCTL ctl;
389389

@@ -397,7 +397,7 @@ process_syncing_tables_for_apply(XLogRecPtr current_lsn)
397397
* Clean up the hash table when we're done with all tables (just to
398398
* release the bit of memory).
399399
*/
400-
else if (!table_states_not_ready && last_start_times)
400+
else if (table_states_not_ready == NIL && last_start_times)
401401
{
402402
hash_destroy(last_start_times);
403403
last_start_times = NULL;
@@ -1498,7 +1498,7 @@ FetchTableStates(bool *started_tx)
14981498
* if table_state_not_ready was empty we still need to check again to
14991499
* see if there are 0 tables.
15001500
*/
1501-
has_subrels = (list_length(table_states_not_ready) > 0) ||
1501+
has_subrels = (table_states_not_ready != NIL) ||
15021502
HasSubscriptionRelations(MySubscription->oid);
15031503

15041504
table_states_valid = true;
@@ -1534,7 +1534,7 @@ AllTablesyncsReady(void)
15341534
* Return false when there are no tables in subscription or not all tables
15351535
* are in ready state; true otherwise.
15361536
*/
1537-
return has_subrels && list_length(table_states_not_ready) == 0;
1537+
return has_subrels && (table_states_not_ready == NIL);
15381538
}
15391539

15401540
/*

src/backend/replication/pgoutput/pgoutput.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
450450
errmsg("client sent proto_version=%d but we only support protocol %d or higher",
451451
data->protocol_version, LOGICALREP_PROTO_MIN_VERSION_NUM)));
452452

453-
if (list_length(data->publication_names) < 1)
453+
if (data->publication_names == NIL)
454454
ereport(ERROR,
455455
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
456456
errmsg("publication_names parameter missing")));

src/backend/rewrite/rewriteDefine.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ DefineQueryRewrite(const char *rulename,
313313
*
314314
* So there cannot be INSTEAD NOTHING, ...
315315
*/
316-
if (list_length(action) == 0)
316+
if (action == NIL)
317317
ereport(ERROR,
318318
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
319319
errmsg("INSTEAD NOTHING rules on SELECT are not implemented"),

src/backend/statistics/mcv.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,6 @@ mcv_get_match_bitmap(PlannerInfo *root, List *clauses,
16101610

16111611
/* The bitmap may be partially built. */
16121612
Assert(clauses != NIL);
1613-
Assert(list_length(clauses) >= 1);
16141613
Assert(mcvlist != NULL);
16151614
Assert(mcvlist->nitems > 0);
16161615
Assert(mcvlist->nitems <= STATS_MCVLIST_MAX_ITEMS);

src/backend/storage/lmgr/lmgr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ WaitForLockersMultiple(List *locktags, LOCKMODE lockmode, bool progress)
913913
int done = 0;
914914

915915
/* Done if no locks to wait for */
916-
if (list_length(locktags) == 0)
916+
if (locktags == NIL)
917917
return;
918918

919919
/* Collect the transactions we need to wait on */

src/backend/utils/adt/jsonb_gin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ extract_jsp_path_expr(JsonPathGinContext *cxt, JsonPathGinPath path,
567567
/* extract a list of nodes to be AND-ed */
568568
List *nodes = extract_jsp_path_expr_nodes(cxt, path, jsp, scalar);
569569

570-
if (list_length(nodes) <= 0)
570+
if (nodes == NIL)
571571
/* no nodes were extracted => full scan is needed for this path */
572572
return NULL;
573573

src/backend/utils/adt/jsonpath_exec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2552,7 +2552,7 @@ JsonValueListLength(const JsonValueList *jvl)
25522552
static bool
25532553
JsonValueListIsEmpty(JsonValueList *jvl)
25542554
{
2555-
return !jvl->singleton && list_length(jvl->list) <= 0;
2555+
return !jvl->singleton && (jvl->list == NIL);
25562556
}
25572557

25582558
static JsonbValue *

src/backend/utils/adt/jsonpath_gram.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ makeIndexArray(List *list)
459459
ListCell *cell;
460460
int i = 0;
461461

462-
Assert(list_length(list) > 0);
462+
Assert(list != NIL);
463463
v->value.array.nelems = list_length(list);
464464

465465
v->value.array.elems = palloc(sizeof(v->value.array.elems[0]) *

src/backend/utils/adt/ruleutils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8114,7 +8114,7 @@ get_parameter(Param *param, deparse_context *context)
81148114
{
81158115
deparse_namespace *dpns = lfirst(lc);
81168116

8117-
if (list_length(dpns->rtable_names) > 0)
8117+
if (dpns->rtable_names != NIL)
81188118
{
81198119
should_qualify = true;
81208120
break;

src/backend/utils/adt/selfuncs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3408,7 +3408,7 @@ estimate_num_groups_incremental(PlannerInfo *root, List *groupExprs,
34083408
* for normal cases with GROUP BY or DISTINCT, but it is possible for
34093409
* corner cases with set operations.)
34103410
*/
3411-
if (groupExprs == NIL || (pgset && list_length(*pgset) < 1))
3411+
if (groupExprs == NIL || (pgset && *pgset == NIL))
34123412
return 1.0;
34133413

34143414
/*

src/backend/utils/adt/tsquery.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ parse_tsquery(char *buf,
829829

830830
close_tsvector_parser(state.valstate);
831831

832-
if (list_length(state.polstr) == 0)
832+
if (state.polstr == NIL)
833833
{
834834
ereport(NOTICE,
835835
(errmsg("text-search query doesn't contain lexemes: \"%s\"",

0 commit comments

Comments
 (0)