From efd0c16becbf45e3b0215e124fde75fee8fcbce4 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Wed, 17 Aug 2022 11:12:35 -0400 Subject: 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 --- src/backend/utils/adt/jsonb_gin.c | 2 +- src/backend/utils/adt/jsonpath_exec.c | 2 +- src/backend/utils/adt/jsonpath_gram.y | 2 +- src/backend/utils/adt/ruleutils.c | 2 +- src/backend/utils/adt/selfuncs.c | 2 +- src/backend/utils/adt/tsquery.c | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) (limited to 'src/backend/utils/adt') diff --git a/src/backend/utils/adt/jsonb_gin.c b/src/backend/utils/adt/jsonb_gin.c index c5325acde4f..731eb018d4e 100644 --- a/src/backend/utils/adt/jsonb_gin.c +++ b/src/backend/utils/adt/jsonb_gin.c @@ -567,7 +567,7 @@ extract_jsp_path_expr(JsonPathGinContext *cxt, JsonPathGinPath path, /* extract a list of nodes to be AND-ed */ List *nodes = extract_jsp_path_expr_nodes(cxt, path, jsp, scalar); - if (list_length(nodes) <= 0) + if (nodes == NIL) /* no nodes were extracted => full scan is needed for this path */ return NULL; diff --git a/src/backend/utils/adt/jsonpath_exec.c b/src/backend/utils/adt/jsonpath_exec.c index 10c7e64aab3..5b6a4805721 100644 --- a/src/backend/utils/adt/jsonpath_exec.c +++ b/src/backend/utils/adt/jsonpath_exec.c @@ -2552,7 +2552,7 @@ JsonValueListLength(const JsonValueList *jvl) static bool JsonValueListIsEmpty(JsonValueList *jvl) { - return !jvl->singleton && list_length(jvl->list) <= 0; + return !jvl->singleton && (jvl->list == NIL); } static JsonbValue * diff --git a/src/backend/utils/adt/jsonpath_gram.y b/src/backend/utils/adt/jsonpath_gram.y index f903dba3e34..ce5d5af8916 100644 --- a/src/backend/utils/adt/jsonpath_gram.y +++ b/src/backend/utils/adt/jsonpath_gram.y @@ -459,7 +459,7 @@ makeIndexArray(List *list) ListCell *cell; int i = 0; - Assert(list_length(list) > 0); + Assert(list != NIL); v->value.array.nelems = list_length(list); v->value.array.elems = palloc(sizeof(v->value.array.elems[0]) * diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index d575aa00662..8964f73b929 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -8114,7 +8114,7 @@ get_parameter(Param *param, deparse_context *context) { deparse_namespace *dpns = lfirst(lc); - if (list_length(dpns->rtable_names) > 0) + if (dpns->rtable_names != NIL) { should_qualify = true; break; diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c index d35e5605de8..50b588e3d0f 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -3408,7 +3408,7 @@ estimate_num_groups_incremental(PlannerInfo *root, List *groupExprs, * for normal cases with GROUP BY or DISTINCT, but it is possible for * corner cases with set operations.) */ - if (groupExprs == NIL || (pgset && list_length(*pgset) < 1)) + if (groupExprs == NIL || (pgset && *pgset == NIL)) return 1.0; /* diff --git a/src/backend/utils/adt/tsquery.c b/src/backend/utils/adt/tsquery.c index f54f2988149..f49e6bb1578 100644 --- a/src/backend/utils/adt/tsquery.c +++ b/src/backend/utils/adt/tsquery.c @@ -829,7 +829,7 @@ parse_tsquery(char *buf, close_tsvector_parser(state.valstate); - if (list_length(state.polstr) == 0) + if (state.polstr == NIL) { ereport(NOTICE, (errmsg("text-search query doesn't contain lexemes: \"%s\"", -- cgit v1.2.3