aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/ADT/DenseMapTest.cpp
diff options
context:
space:
mode:
authorFiona Glaser <escha@apple.com>2016-03-15 01:50:46 +0000
committerFiona Glaser <escha@apple.com>2016-03-15 01:50:46 +0000
commita4b1ace4610fb6ec2a1d06b3b978285f3dafbb20 (patch)
tree77afa28718fcd15c79abaebc900de79aacd22119 /llvm/unittests/ADT/DenseMapTest.cpp
parent1b640e05ba57e805ed00831771b75cd2e617f656 (diff)
downloadllvm-a4b1ace4610fb6ec2a1d06b3b978285f3dafbb20.zip
llvm-a4b1ace4610fb6ec2a1d06b3b978285f3dafbb20.tar.gz
llvm-a4b1ace4610fb6ec2a1d06b3b978285f3dafbb20.tar.bz2
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. llvm-svn: 263522
Diffstat (limited to 'llvm/unittests/ADT/DenseMapTest.cpp')
-rw-r--r--llvm/unittests/ADT/DenseMapTest.cpp13
1 files changed, 13 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp
index f3dcf95..0fda2c6 100644
--- a/llvm/unittests/ADT/DenseMapTest.cpp
+++ b/llvm/unittests/ADT/DenseMapTest.cpp
@@ -339,6 +339,19 @@ TYPED_TEST(DenseMapTest, ConstIteratorTest) {
EXPECT_TRUE(cit == cit2);
}
+// Make sure resize actually gives us enough buckets to insert N items
+// without increasing allocation size.
+TEST(DenseMapCustomTest, ResizeTest) {
+ for (unsigned Size = 16; Size < 32; ++Size) {
+ DenseMap<unsigned, unsigned> Map;
+ Map.resize(Size);
+ unsigned MemorySize = Map.getMemorySize();
+ for (unsigned i = 0; i < Size; ++i)
+ Map[i] = i;
+ EXPECT_TRUE(Map.getMemorySize() == MemorySize);
+ }
+}
+
// Make sure DenseMap works with StringRef keys.
TEST(DenseMapCustomTest, StringRefTest) {
DenseMap<StringRef, int> M;