1
0
mirror of https://github.com/postgres/postgres.git synced 2025-10-21 02:52:47 +03:00

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
This commit is contained in:
Tom Lane
2022-08-17 11:12:35 -04:00
parent 4a319fce76
commit efd0c16bec
28 changed files with 47 additions and 48 deletions

View File

@@ -383,7 +383,7 @@ process_syncing_tables_for_apply(XLogRecPtr current_lsn)
* immediate restarts. We don't need it if there are no tables that need
* syncing.
*/
if (table_states_not_ready && !last_start_times)
if (table_states_not_ready != NIL && !last_start_times)
{
HASHCTL ctl;
@@ -397,7 +397,7 @@ process_syncing_tables_for_apply(XLogRecPtr current_lsn)
* Clean up the hash table when we're done with all tables (just to
* release the bit of memory).
*/
else if (!table_states_not_ready && last_start_times)
else if (table_states_not_ready == NIL && last_start_times)
{
hash_destroy(last_start_times);
last_start_times = NULL;
@@ -1498,7 +1498,7 @@ FetchTableStates(bool *started_tx)
* if table_state_not_ready was empty we still need to check again to
* see if there are 0 tables.
*/
has_subrels = (list_length(table_states_not_ready) > 0) ||
has_subrels = (table_states_not_ready != NIL) ||
HasSubscriptionRelations(MySubscription->oid);
table_states_valid = true;
@@ -1534,7 +1534,7 @@ AllTablesyncsReady(void)
* Return false when there are no tables in subscription or not all tables
* are in ready state; true otherwise.
*/
return has_subrels && list_length(table_states_not_ready) == 0;
return has_subrels && (table_states_not_ready == NIL);
}
/*