aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/ADT/StringMapTest.cpp
diff options
context:
space:
mode:
authorMehdi Amini <mehdi.amini@apple.com>2016-03-25 05:58:04 +0000
committerMehdi Amini <mehdi.amini@apple.com>2016-03-25 05:58:04 +0000
commitcb708b265def6cbe9111b4b22060d1e4b6045701 (patch)
tree19ba59ba64f5860ec9a058fdb157dbd9218387ce /llvm/unittests/ADT/StringMapTest.cpp
parentbe8a57f9bfb9620907f4679314fab54ff006eb05 (diff)
downloadllvm-cb708b265def6cbe9111b4b22060d1e4b6045701.zip
llvm-cb708b265def6cbe9111b4b22060d1e4b6045701.tar.gz
llvm-cb708b265def6cbe9111b4b22060d1e4b6045701.tar.bz2
Query the StringMap only once when creating MDString (NFC)
Summary: Loading IR with debug info improves MDString::get() from 19ms to 10ms. This is a rework of D16597 with adding an "emplace" method on the StringMap to avoid requiring the MDString move ctor to be public. Reviewers: dexonsmith Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D17920 From: Mehdi Amini <mehdi.amini@apple.com> llvm-svn: 264386
Diffstat (limited to 'llvm/unittests/ADT/StringMapTest.cpp')
-rw-r--r--llvm/unittests/ADT/StringMapTest.cpp61
1 files changed, 47 insertions, 14 deletions
diff --git a/llvm/unittests/ADT/StringMapTest.cpp b/llvm/unittests/ADT/StringMapTest.cpp
index f027f0d..c986a9c 100644
--- a/llvm/unittests/ADT/StringMapTest.cpp
+++ b/llvm/unittests/ADT/StringMapTest.cpp
@@ -358,24 +358,28 @@ TEST_F(StringMapTest, MoveDtor) {
namespace {
// Simple class that counts how many moves and copy happens when growing a map
-struct CountCopyAndMove {
+struct CountCtorCopyAndMove {
+ static unsigned Ctor;
static unsigned Move;
static unsigned Copy;
- CountCopyAndMove() {}
+ int Data = 0;
+ CountCtorCopyAndMove(int Data) : Data(Data) { Ctor++; }
+ CountCtorCopyAndMove() { Ctor++; }
- CountCopyAndMove(const CountCopyAndMove &) { Copy++; }
- CountCopyAndMove &operator=(const CountCopyAndMove &) {
+ CountCtorCopyAndMove(const CountCtorCopyAndMove &) { Copy++; }
+ CountCtorCopyAndMove &operator=(const CountCtorCopyAndMove &) {
Copy++;
return *this;
}
- CountCopyAndMove(CountCopyAndMove &&) { Move++; }
- CountCopyAndMove &operator=(const CountCopyAndMove &&) {
+ CountCtorCopyAndMove(CountCtorCopyAndMove &&) { Move++; }
+ CountCtorCopyAndMove &operator=(const CountCtorCopyAndMove &&) {
Move++;
return *this;
}
};
-unsigned CountCopyAndMove::Copy = 0;
-unsigned CountCopyAndMove::Move = 0;
+unsigned CountCtorCopyAndMove::Copy = 0;
+unsigned CountCtorCopyAndMove::Move = 0;
+unsigned CountCtorCopyAndMove::Ctor = 0;
} // anonymous namespace
@@ -385,14 +389,43 @@ TEST(StringMapCustomTest, InitialSizeTest) {
// 1 is an "edge value", 32 is an arbitrary power of two, and 67 is an
// arbitrary prime, picked without any good reason.
for (auto Size : {1, 32, 67}) {
- StringMap<CountCopyAndMove> Map(Size);
- CountCopyAndMove::Copy = 0;
- CountCopyAndMove::Move = 0;
+ StringMap<CountCtorCopyAndMove> Map(Size);
+ CountCtorCopyAndMove::Move = 0;
+ CountCtorCopyAndMove::Copy = 0;
for (int i = 0; i < Size; ++i)
- Map.insert(std::make_pair(Twine(i).str(), CountCopyAndMove()));
- EXPECT_EQ((unsigned)Size * 3, CountCopyAndMove::Move);
- EXPECT_EQ(0u, CountCopyAndMove::Copy);
+ Map.insert(std::make_pair(Twine(i).str(), CountCtorCopyAndMove()));
+ EXPECT_EQ((unsigned)Size * 3, CountCtorCopyAndMove::Move);
+ EXPECT_EQ(0u, CountCtorCopyAndMove::Copy);
}
}
+TEST(StringMapCustomTest, BracketOperatorCtor) {
+ StringMap<CountCtorCopyAndMove> Map;
+ CountCtorCopyAndMove::Ctor = 0;
+ Map["abcd"];
+ EXPECT_EQ(1u, CountCtorCopyAndMove::Ctor);
+ // Test that operator[] does not create a value when it is already in the map
+ CountCtorCopyAndMove::Ctor = 0;
+ Map["abcd"];
+ EXPECT_EQ(0u, CountCtorCopyAndMove::Ctor);
+}
+
+namespace {
+struct NonMoveableNonCopyableType {
+ int Data = 0;
+ NonMoveableNonCopyableType() = default;
+ NonMoveableNonCopyableType(int Data) : Data(Data) {}
+ NonMoveableNonCopyableType(const NonMoveableNonCopyableType &) = delete;
+ NonMoveableNonCopyableType(NonMoveableNonCopyableType &&) = delete;
+};
+}
+
+// Test that we can "emplace" an element in the map without involving map/move
+TEST(StringMapCustomTest, EmplaceTest) {
+ StringMap<NonMoveableNonCopyableType> Map;
+ Map.emplace_second("abcd", 42);
+ EXPECT_EQ(1u, Map.count("abcd"));
+ EXPECT_EQ(42, Map["abcd"].Data);
+}
+
} // end anonymous namespace