aboutsummaryrefslogtreecommitdiff
path: root/src/backend/lib/knapsack.c
diff options
context:
space:
mode:
authorDavid Rowley <drowley@postgresql.org>2024-01-19 10:44:36 +1300
committerDavid Rowley <drowley@postgresql.org>2024-01-19 10:44:36 +1300
commit4b310636439da432c473e0e85de661b3342bb745 (patch)
tree479ca894dff617f5d1f79bae96fd1adee53a7a70 /src/backend/lib/knapsack.c
parent57b440ec115f57ff6e6a3f0df26063822e3123d2 (diff)
downloadpostgresql-4b310636439da432c473e0e85de661b3342bb745.tar.gz
postgresql-4b310636439da432c473e0e85de661b3342bb745.zip
Fix broken Bitmapset optimization in DiscreteKnapsack()
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
Diffstat (limited to 'src/backend/lib/knapsack.c')
-rw-r--r--src/backend/lib/knapsack.c5
1 files changed, 1 insertions, 4 deletions
diff --git a/src/backend/lib/knapsack.c b/src/backend/lib/knapsack.c
index 13d800718f0..439da1ad70d 100644
--- a/src/backend/lib/knapsack.c
+++ b/src/backend/lib/knapsack.c
@@ -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);