Skip to content

Commit f90ded5

Browse files
[ADT] Reduce memory allocation in SmallPtrSet::reserve() (#153126)
Previously, reserve() allocated double the required number of buckets. For example, for NumEntries in the range [49, 96], it would reserve 256 buckets when only 128 are needed to maintain the load factor. This patch removes "+ 1" in the NewSize calculation.
1 parent 06cc888 commit f90ded5

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

llvm/include/llvm/ADT/SmallPtrSet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class SmallPtrSetImplBase : public DebugEpochBase {
129129
// We must Grow -- find the size where we'd be 75% full, then round up to
130130
// the next power of two.
131131
size_type NewSize = NumEntries + (NumEntries / 3);
132-
NewSize = 1 << (Log2_32_Ceil(NewSize) + 1);
132+
NewSize = 1 << Log2_32_Ceil(NewSize);
133133
// Like insert_imp_big, always allocate at least 128 elements.
134134
NewSize = std::max(128u, NewSize);
135135
Grow(NewSize);

llvm/unittests/ADT/SmallPtrSetTest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,4 +475,8 @@ TEST(SmallPtrSetTest, Reserve) {
475475
EXPECT_EQ(Set.capacity(), 128u);
476476
EXPECT_EQ(Set.size(), 6u);
477477
EXPECT_THAT(Set, UnorderedElementsAre(&Vals[0], &Vals[1], &Vals[2], &Vals[3], &Vals[4], &Vals[5]));
478+
479+
// Reserving 192 should result in 256 buckets.
480+
Set.reserve(192);
481+
EXPECT_EQ(Set.capacity(), 256u);
478482
}

0 commit comments

Comments
 (0)