Fix broken Bitmapset optimization in DiscreteKnapsack()
authorDavid Rowley <[email protected]>
Thu, 18 Jan 2024 21:44:36 +0000 (10:44 +1300)
committerDavid Rowley <[email protected]>
Thu, 18 Jan 2024 21:44:36 +0000 (10:44 +1300)
Some code in DiscreteKnapsack() attempted to zero all words in a
Bitmapset by performing bms_del_members() to delete all the members from
itself before replacing those members with members from another set.
When that code was written, this was a valid way to manipulate the set
in such a way to save from palloc having to be called to allocate a new
Bitmapset.  However, 00b41463c modified Bitmapsets so that an empty set is
*always* represented as NULL and this breaks the optimization as the
Bitmapset code will always pfree the memory when the set becomes empty.

Since DiscreteKnapsack() has been coded to avoid as many unneeded
allocations as possible, it seems risky to not fix this.  Here we add
bms_replace_members() to effectively perform an in-place copy of another
set, reusing the memory of the existing set, when possible.

This got broken in v16, but no backpatch for now as there've been no
complaints.

Reviewed-by: Richard Guo
Discussion: https://postgr.es/m/CAApHDvoTCBkBU2PJghNOFUiO0q=QP4WAWHi5sJP6_4=b2WodrA@mail.gmail.com

src/backend/lib/knapsack.c
src/backend/nodes/bitmapset.c
src/include/nodes/bitmapset.h

index 13d800718f025b1168728ed22a89a7412fe925d7..439da1ad70d00186146b8c0600fef1a7d8ff7eb0 100644 (file)
@@ -89,10 +89,7 @@ DiscreteKnapsack(int max_weight, int num_items,
            {
                /* copy sets[ow] to sets[j] without realloc */
                if (j != ow)
-               {
-                   sets[j] = bms_del_members(sets[j], sets[j]);
-                   sets[j] = bms_add_members(sets[j], sets[ow]);
-               }
+                   sets[j] = bms_replace_members(sets[j], sets[ow]);
 
                sets[j] = bms_add_member(sets[j], i);
 
index b0f908f978f8b49ddfa8a985c7c29e43625bda29..65805d45277afd1f5858aae140270784b805f88f 100644 (file)
@@ -976,6 +976,50 @@ bms_add_members(Bitmapset *a, const Bitmapset *b)
    return result;
 }
 
+/*
+ * bms_replace_members
+ *     Remove all existing members from 'a' and repopulate the set with members
+ *     from 'b', recycling 'a', when possible.
+ */
+Bitmapset *
+bms_replace_members(Bitmapset *a, const Bitmapset *b)
+{
+   int         i;
+
+   Assert(bms_is_valid_set(a));
+   Assert(bms_is_valid_set(b));
+
+   if (a == NULL)
+       return bms_copy(b);
+   if (b == NULL)
+   {
+       pfree(a);
+       return NULL;
+   }
+
+   if (a->nwords < b->nwords)
+       a = (Bitmapset *) repalloc(a, BITMAPSET_SIZE(b->nwords));
+
+   i = 0;
+   do
+   {
+       a->words[i] = b->words[i];
+   } while (++i < b->nwords);
+
+   a->nwords = b->nwords;
+
+#ifdef REALLOCATE_BITMAPSETS
+
+   /*
+    * There's no guarantee that the repalloc returned a new pointer, so copy
+    * and free unconditionally here.
+    */
+   a = bms_copy_and_free(a);
+#endif
+
+   return a;
+}
+
 /*
  * bms_add_range
  *     Add members in the range of 'lower' to 'upper' to the set.
index efc8890ce61ffc6587eb222866ddf5d90785b30f..906e8dcc1575742f7bf52045023c531acfe4d42e 100644 (file)
@@ -109,6 +109,7 @@ extern BMS_Membership bms_membership(const Bitmapset *a);
 extern Bitmapset *bms_add_member(Bitmapset *a, int x);
 extern Bitmapset *bms_del_member(Bitmapset *a, int x);
 extern Bitmapset *bms_add_members(Bitmapset *a, const Bitmapset *b);
+extern Bitmapset *bms_replace_members(Bitmapset *a, const Bitmapset *b);
 extern Bitmapset *bms_add_range(Bitmapset *a, int lower, int upper);
 extern Bitmapset *bms_int_members(Bitmapset *a, const Bitmapset *b);
 extern Bitmapset *bms_del_members(Bitmapset *a, const Bitmapset *b);