Exploring pass-by-value for small Bitmapset sets

Поиск
Список
Период
Сортировка
Искать

Exploring pass-by-value for small Bitmapset sets

От:
Thomas Munro <thomas.munro@gmail.com>
Дата:
Вложения:
Hi,

I played around with the idea of supporting "immediate" Bitmapset
values that skip allocation and pointer chasing overheads and live in
registers, as long as they fit.  Prototype-grade patch attached.

The main thing that had stopped me exploring this earlier is that it
has to work within the Node object model.  Here I tried out a
technique from dynamically typed language implementations*: a Node
pointer can be tagged with zero bit = 1 meaning "this is an
immediate/unboxed value", since valid pointers to structs with alignof
> 1 can't hold odd addresses.  That leaves 63 (or 31) free bits in
which to store a value.  If more Node types wanted an immediate
representation then it seems like we could either steal at least one
more bit for a fixed- or variable-width type selector field, which
doesn't seem very scalable, or screw the Node object model down a bit
so we wouldn't need it**.  I haven't looked into that much... for now
I just wanted the simplest hardcoded thing to let me try out the
bitmapset.c part.

Then I inserted a bunch of immediate value handling, for example:

bms_equal() has:

    if (bms_is_immediate(a))
        return a == b;
    else if (bms_is_immediate(b))
        return false;

bms_intersect() has:

    if (bms_is_immediate(a) || bms_is_immediate(b))
    {
        bitmapword  aw = bms_first_word(a);
        bitmapword  bw = bms_first_word(b);

        return bms_make_from_one_word(aw & bw);
    }

bms_make_from_one_word() has:

    if (likely(bms_word_fits_in_immediate(aw)))
        return bms_word_to_immediate(aw);

That turns into:

    if (aw == 0)
        return NULL;

    aw <<= NODE_PTR_TAG_MASK_WIDTH;
    aw |= NODE_PTR_T_Bitmapset;
    return (Bitmapset *) aw;

There are still non-immediate Bitmapset objects with nwords == 1, but
only for sets containing 63 (or 31) as the highest member, since the
tag eats a bit and the value is shifted.  That's why I added a bunch
of edge cases around that boundary to the tests.

Something involving a union or Datum-like thing rather than casts of a
pointer might be better C style, but that's superficial and I didn't
want to run around and change types all over the tree for this
demonstration.  Another superficial aspect is that it feels like some
common operations are obvious candidates for inlining of at least
their happy paths.

The bitmapset.c API suits the resulting pass-by-value-sometimes
semantics perfectly: NULL is already a special case for the extreme
case of small sets, and all modifying operations return their result,
so callers already store them and require no change.  (The list API
also has that property.  Bitmapset replaced an earlier use of lists of
small integers to hold relids and attrs etc, and lists were probably
originally CONS chains with NIL as empty and outputs that share
structure with their input, hence this API style.)

I'm interested to hear whether people think this sort of thing is
worth pursuing, how much of a problem Bitmapset manipulation really
is, whether it's worth hacking on the Node system to achieve that, etc
etc.


* Modern pointer tagging systems often steal high bits instead (or as
well), knowing that virtual addresses are limited to 48-52 bits or
something like that by most architectures.  They're also sometimes
interested in tagging some values that *are* pointers (imagine if our
node->tag were moved into the upper bits of the node pointer itself
and had to be masked off before dereferencing), whereas here I only
want to mark a value as *not* a pointer.  The high end leads to very
slightly cheaper bitswizzling perhaps since you don't need to shift,
but I have dim recollections that that AIX broke that assumption and
couldn't run certain open source virtual machines for Javascript etc
as a result.  So I went for the low end of the word, a traditional
version of the trick from the 32 bit days.  Of course you could use
different bits/operations for different systems and word sizes if it's
really worth it, in a more developed version...

** I suspect that if we got rid of the fully general print() etc that
we inherited from Lisp (which was in fact using tagged pointers and
immediates and only doing the equivalent of node->tag in user code to
discriminate struct types, BTW) and always used more specific
print_expr() etc functions, as we already do when we're recursing,
then we probably wouldn't need know about pointer tags anywhere
outside eg bitmapset.c, if it were true that no place in the grammar
of valid Node tree productions permits discriminated unions of types
with an immediate representation.  Perhaps that same line of thinking
would already work today in the parallel universe of Datums, if, say,
there were an immediate representation of tiny varlena to support
small text, numeric etc values.  On the other hand, it doesn't seem
too bad to add a couple of cycles to the most general
node-type-dispatching functions, and it would be avoided in the common
case if we had a different nodeType() function that doesn't test for
them for the places with known possible types that doesn't include
immediates in our grammar of legal trees.  I haven't studied any of
that very much, and for this I just did the easiest thing to make it
work...

Re: Exploring pass-by-value for small Bitmapset sets

От:
David Rowley <dgrowleyml@gmail.com>
Дата:
On Tue, 3 Mar 2026 at 15:55, Thomas Munro  wrote:
> I played around with the idea of supporting "immediate" Bitmapset
> values that skip allocation and pointer chasing overheads and live in
> registers, as long as they fit.  Prototype-grade patch attached.

> I'm interested to hear whether people think this sort of thing is
> worth pursuing, how much of a problem Bitmapset manipulation really
> is, whether it's worth hacking on the Node system to achieve that, etc
> etc.

It would be interesting to know if you've seen any sign that this
would result in any performance improvements? It's going to be much
easier to justify adding complexity when there's a demonstration of
some sort of performance gain from doing so.

There was some testing in [1] that showed that the planner can be
quite sensitive to Bitmapset performance in certain cases. IIRC, the
2x speed up in there was due to the bms_is_subset short-circuit that
was added in that patch for "/* 'a' can't be a subset of 'b' if it
contains more words */". Prior to that change, a Bitmapset could have
trailing zero words. I think primarily those tests focus on Bitmapsets
with multiple words, so it might not be the test for your case. I'd
would be good to check it doesn't make it too much slower, however.

> Another superficial aspect is that it feels like some
> common operations are obvious candidates for inlining of at least
> their happy paths.

I was wondering this too. I think the gains would be much more if you
could avoid the pointer dereference AND the function call overhead.
The more simple ones are maybe worth a look at to consider. Most times
I see bms_is_member(), I consider if it would be worth inlining. It's
just that elog(ERROR) that puts me off. I do wonder if we should be
returning false for < 0 and maybe convert the elog to an Assert.

bms_overlap(), bms_equal() and bms_is_subset() are likely good
candidates. They're heavily used during the planner's join search by
join_is_legal(). make_rels_by_clause_joins() uses bms_overlap(). For
benchmarking, you might need to carefully craft a query that is fairly
restricted in terms of join order so that not too many Paths are
generated, which could well drown out any gains you might get.

David

[1] https://www.postgresql.org/message-id/CAJ2pMkYcKHFBD_OMUSVyhYSQU0-j9T6NZ0pL6pwbZsUCohWc7Q@mail.gmail.com


Re: Exploring pass-by-value for small Bitmapset sets

От:
Matthias van de Meent <boekewurm+postgres@gmail.com>
Дата:
On Tue, 3 Mar 2026 at 03:55, Thomas Munro  wrote:
>
> Hi,
>
> I played around with the idea of supporting "immediate" Bitmapset
> values that skip allocation and pointer chasing overheads and live in
> registers, as long as they fit.  Prototype-grade patch attached.
>[...]
> I'm interested to hear whether people think this sort of thing is
> worth pursuing, how much of a problem Bitmapset manipulation really
> is, whether it's worth hacking on the Node system to achieve that, etc
> etc.

I'd love this, if only because it'll allow us to avoid memory
allocations in long-lived cache contexts.  Of course, David's point
about performance stands, but if we don't lose on it I think this will
be a nice improvement in memory usage.

As example, the RelationData struct has 5 bitmapset fields for
table-like relations, and avoiding allocations in the common case of
tables with <<64 or <<32 attributes would likely be a nice saving.
Generally, at least 3 of those bitmaps are populated (key-, pk-, and
hotblockingattr), so avoiding palloc'd memory would save >=72B of
allocations per relcache table; which is ~14% of the current size of
RelationData. That's quite good.

Where do you think this needs hacking on the Node system?  AFAIK, all
Bitmapset support functions already are custom, so IIUC nothing needs
to be done for gen_node_support.pl.  And I've not noticed any
bitmapsets that are stored in a Node field anywhere; with all of
copy/equal/out/jumble/read*funcs calling directly into the specialized
implementations rather than dispatching through
_*Node()->_*Bitmapset().

Relatedly: I think we should remove Bitmapset's Node tag (removing it
from the Node types) because it is more and more like a scalar/string
type than a proper participant in the Node infrastructure.  We don't
tag all palloc'd types with a NodeTag (by far), so I think the reason
we're keeping the tag around now is now mostly historical.  The
incidental places that use the generic node methods on Bitmapsets can
probably be adapted, as I think it's highly unlikely that there are
many places that call into generic Node methods with a known Bitmapset
as input.


Kind regards,

Matthias van de Meent
Databricks (https://www.databricks.com)


FAQ