Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ee01d84

Browse files
committed
In bms_add_member(), use repalloc() if the bms needs to be enlarged.
Previously bms_add_member() would palloc a whole-new copy of the existing set, copy the words, and pfree the old one. repalloc() is potentially much faster, and more importantly, this is less surprising if CurrentMemoryContext is not the same as the context the old set is in. bms_add_member() still allocates a new bitmapset in CurrentMemoryContext if NULL is passed as argument, but that is a lot less likely to induce bugs. Nicholas White.
1 parent 357f752 commit ee01d84

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/backend/nodes/bitmapset.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -632,21 +632,20 @@ bms_add_member(Bitmapset *a, int x)
632632
return bms_make_singleton(x);
633633
wordnum = WORDNUM(x);
634634
bitnum = BITNUM(x);
635+
636+
/* enlarge the set if necessary */
635637
if (wordnum >= a->nwords)
636638
{
637-
/* Slow path: make a larger set and union the input set into it */
638-
Bitmapset *result;
639-
int nwords;
639+
int oldnwords = a->nwords;
640640
int i;
641641

642-
result = bms_make_singleton(x);
643-
nwords = a->nwords;
644-
for (i = 0; i < nwords; i++)
645-
result->words[i] |= a->words[i];
646-
pfree(a);
647-
return result;
642+
a = (Bitmapset *) repalloc(a, BITMAPSET_SIZE(wordnum + 1));
643+
a->nwords = wordnum + 1;
644+
/* zero out the enlarged portion */
645+
for (i = oldnwords; i < a->nwords; i++)
646+
a->words[i] = 0;
648647
}
649-
/* Fast path: x fits in existing set */
648+
650649
a->words[wordnum] |= ((bitmapword) 1 << bitnum);
651650
return a;
652651
}

0 commit comments

Comments
 (0)