Re: Strange Bitmapset manipulation in DiscreteKnapsack()

David Rowley <dgrowleyml@gmail.com>

From: David Rowley <dgrowleyml@gmail.com>
To: Andy Fan <zhihuifan1213@163.com>
Cc: pgsql-hackers@lists.postgresql.org
Date: 2024-01-18T21:31:33Z
Lists: pgsql-hackers
On Fri, 19 Jan 2024 at 01:07, Andy Fan <zhihuifan1213@163.com> wrote:
> I find the following code in DiscreteKnapsack is weird.
>
>
>         for (i = 0; i <= max_weight; ++i)
>         {
>                 values[i] = 0;
>
> ** memory allocation here, and the num_items bit is removed later **
>
>                 sets[i] = bms_make_singleton(num_items);
>         }
>
>
>         ** num_items bit is removed here **
>         result = bms_del_member(bms_copy(sets[max_weight]), num_items);

It does not seem weird to me.  If the set is going to have multiple
words then adding a member 1 higher than the highest we'll ever add
ensures the set has enough words and we don't need to repalloc to grow
the set when we bms_add_member().

David



Commits

  1. Fix broken Bitmapset optimization in DiscreteKnapsack()

  2. Require empty Bitmapsets to be represented as NULL.