llvm.org GIT mirror llvm / 33ae313
DenseMap: make .resize() do the intuitive thing In some places, like InstCombine, we resize a DenseMap to fit the elements we intend to put in it, then insert those elements (to avoid continual reallocations as it grows). But .resize(foo) doesn't actually do what people think; it resizes to foo buckets (which is really an implementation detail the user of DenseMap probably shouldn't care about), not the space required to fit foo elements. DenseMap grows if 3/4 of its buckets are full, so this actually causes one forced reallocation every time instead of avoiding a reallocation. This patch makes .resize(foo) do the intuitive thing: it grows to the size necessary to fit foo elements without new allocations. Also include a test to verify that .resize() actually does what we think it does. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@263522 91177308-0d34-0410-b5e6-96231b3b80d8 Fiona Glaser 4 years ago
2 changed file(s) with 18 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
8080 }
8181 unsigned size() const { return getNumEntries(); }
8282
83 /// Grow the densemap so that it has at least Size buckets. Does not shrink
83 /// Grow the densemap so that it can contain at least Size items before
84 /// resizing again. This means somewhat more than Size buckets because
85 /// densemap resizes upon reaching 3/4 full.
8486 void resize(size_type Size) {
87 // Size *= (4/3), rounding up.
88 Size = (Size * 4 + 2) / 3;
8589 incrementEpoch();
8690 if (Size > getNumBuckets())
8791 grow(Size);
338338 EXPECT_TRUE(cit == cit2);
339339 }
340340
341 // Make sure resize actually gives us enough buckets to insert N items
342 // without increasing allocation size.
343 TEST(DenseMapCustomTest, ResizeTest) {
344 for (unsigned Size = 16; Size < 32; ++Size) {
345 DenseMap Map;
346 Map.resize(Size);
347 unsigned MemorySize = Map.getMemorySize();
348 for (unsigned i = 0; i < Size; ++i)
349 Map[i] = i;
350 EXPECT_TRUE(Map.getMemorySize() == MemorySize);
351 }
352 }
353
341354 // Make sure DenseMap works with StringRef keys.
342355 TEST(DenseMapCustomTest, StringRefTest) {
343356 DenseMap M;