Require empty Bitmapsets to be represented as NULL.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 2 Mar 2023 16:47:26 +0000 (11:47 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 2 Mar 2023 16:47:26 +0000 (11:47 -0500)
commit00b41463c21615f9bf3927f207e37f9e215d32e6
tree355258ff476ae3209f6ee14f4c75d4e59ddbdd35
parent141225b2518f20ca7bd68d4458953c3404d2e364
Require empty Bitmapsets to be represented as NULL.

When I designed the Bitmapset module, I set things up so that an empty
Bitmapset could be represented either by a NULL pointer, or by an
allocated object all of whose bits are zero.  I've recently come to
the conclusion that that was a bad idea and we should instead have a
convention like the longstanding invariant for Lists, whereby an empty
list is represented by NIL and nothing else.

To do this, we need to fix bms_intersect, bms_difference, and a couple
of other functions to check for having produced an empty result; but
then we can replace bms_is_empty(a) by a simple "a == NULL" test.

This is very likely a (marginal) win performance-wise, because we
call bms_is_empty many more times than those other functions put
together.  However, the real reason to do it is that we have various
places that have hand-implemented a rule about "this Bitmapset
variable must be exactly NULL if empty", so that they can use
checks-for-null in place of bms_is_empty calls in particularly hot
code paths.  That is a really fragile, mistake-prone way to do things,
and I'm surprised that we've seldom been bitten by it.  It's not well
documented at all which variables have this property, so you can't
readily tell which code might be violating those conventions.  By
making the convention universal, we can eliminate a subtle source of
bugs.

Patch by me; thanks to Nathan Bossart and Richard Guo for review.

Discussion: https://postgr.es/m/1159933.1677621588@sss.pgh.pa.us
src/backend/nodes/bitmapset.c
src/include/nodes/bitmapset.h