Index: lib/Support/StringMap.cpp =================================================================== --- lib/Support/StringMap.cpp +++ lib/Support/StringMap.cpp @@ -22,6 +22,13 @@ // If a size is specified, initialize the table with that many buckets. if (InitSize) { + // The table will grow when the number of entries reach 3/4 of the number of + // buckets. To guarantee that "InitSize" number of entries can be inserted + // in the table without growing, we allocate just what is needed here. + InitSize = (InitSize * 4 + 2) / 3; + // Size has to be a power of 2 + if (!isPowerOf2_32(InitSize)) + InitSize = NextPowerOf2(InitSize); init(InitSize); return; } Index: unittests/ADT/StringMapTest.cpp =================================================================== --- unittests/ADT/StringMapTest.cpp +++ unittests/ADT/StringMapTest.cpp @@ -231,12 +231,12 @@ // moved to a different bucket during internal rehashing. This depends on // the particular key, and the implementation of StringMap and HashString. // Changes to those might result in this test not actually checking that. - StringMap t(1); - EXPECT_EQ(1u, t.getNumBuckets()); + StringMap t(0); + EXPECT_EQ(0u, t.getNumBuckets()); StringMap::iterator It = t.insert(std::make_pair("abcdef", 42)).first; - EXPECT_EQ(2u, t.getNumBuckets()); + EXPECT_EQ(16u, t.getNumBuckets()); EXPECT_EQ("abcdef", It->first()); EXPECT_EQ(42u, It->second); }