Re: Strange Bitmapset manipulation in DiscreteKnapsack() - Mailing list pgsql-hackers

From David Rowley
Subject Re: Strange Bitmapset manipulation in DiscreteKnapsack()
Date
Msg-id CAApHDvp=yqH2NULgADr_Lm-w=NidXmT0h+-ue8tda+6G0dnBQg@mail.gmail.com
Whole thread Raw
In response to Re: Strange Bitmapset manipulation in DiscreteKnapsack()  (Andy Fan <zhihuifan1213@163.com>)
Responses Re: Strange Bitmapset manipulation in DiscreteKnapsack()
List 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



pgsql-hackers by date:

Previous
From: Przemysław Sztoch
Date:
Subject: Re: UUID v7
Next
From: "David G. Johnston"
Date:
Subject: Re: ALTER ROLE documentation improvement