From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Avoid using list_length() to test for empty list.
Date: Wed, 17 Aug 2022 15:12:43 +0000
Message-ID: <E1oOKin-001CGW-QC@gemulon.postgresql.org> (raw)
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
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/efd0c16becbf45e3b0215e124fde75fee8fcbce4
Modified Files
--------------
src/backend/catalog/objectaddress.c | 2 +-
src/backend/catalog/pg_depend.c | 2 +-
src/backend/commands/event_trigger.c | 8 ++++----
src/backend/commands/functioncmds.c | 4 ++--
src/backend/commands/publicationcmds.c | 6 +++---
src/backend/commands/statscmds.c | 4 ++--
src/backend/commands/subscriptioncmds.c | 2 +-
src/backend/commands/tablecmds.c | 8 ++++----
src/backend/commands/typecmds.c | 2 +-
src/backend/executor/execPartition.c | 2 +-
src/backend/libpq/auth.c | 4 ++--
src/backend/libpq/hba.c | 4 ++--
src/backend/optimizer/path/costsize.c | 2 +-
src/backend/optimizer/plan/createplan.c | 2 +-
src/backend/optimizer/plan/planner.c | 12 ++++++------
src/backend/partitioning/partprune.c | 2 +-
src/backend/replication/logical/tablesync.c | 8 ++++----
src/backend/replication/pgoutput/pgoutput.c | 2 +-
src/backend/rewrite/rewriteDefine.c | 2 +-
src/backend/statistics/mcv.c | 1 -
src/backend/storage/lmgr/lmgr.c | 2 +-
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 +-
src/test/modules/test_ddl_deparse/test_ddl_deparse.c | 2 +-
28 files changed, 47 insertions(+), 48 deletions(-)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: tgl@sss.pgh.pa.us
Subject: Re: pgsql: Avoid using list_length() to test for empty list.
In-Reply-To: <E1oOKin-001CGW-QC@gemulon.postgresql.org>
References: <E1oOKin-001CGW-QC@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox