Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2053)

Unified Diff: Source/wtf/HashMapTest.cpp

Issue 1176303008: Avoid argument copying in WTF hash-based containers (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fix NRVO in test key traits on Win Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | Source/wtf/HashTable.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/wtf/HashMapTest.cpp
diff --git a/Source/wtf/HashMapTest.cpp b/Source/wtf/HashMapTest.cpp
index cbdbca6eb6c1c11b65ad3537b508e6267e0067ea..f60596134e6f391657d3164f720f663714c6720d 100644
--- a/Source/wtf/HashMapTest.cpp
+++ b/Source/wtf/HashMapTest.cpp
@@ -303,6 +303,60 @@ TEST(HashMapTest, ValueTypeDestructed)
EXPECT_EQ(0, InstanceCounter::counter);
}
+class CopyCounter {
+public:
+ CopyCounter() = default;
+ explicit CopyCounter(int data) : m_data(data) { }
+
+ CopyCounter(const CopyCounter& copy) : m_data(copy.m_data) { ++s_counter; }
+
+ int data() const { return m_data; }
+
+ bool isHashTableDeletedValue() const { return m_data == HashTableDeletedValue; }
+
+ static int s_counter;
+private:
+ int m_data;
+};
+
+int CopyCounter::s_counter = 0;
+
+struct CopyCounterHash {
+ static unsigned hash(const CopyCounter& key)
+ {
+ return IntHash<unsigned>::hash(key.data());
+ }
+
+ static bool equal(const CopyCounter& a, const CopyCounter& b)
+ {
+ return a.data() == b.data();
+ }
+
+ static const bool safeToCompareToEmptyOrDeleted = true;
+};
+
+struct CopyCounterHashTraits: SimpleClassHashTraits<CopyCounter> {
+#if COMPILER(MSVC)
+ static CopyCounter emptyValue()
tkent 2015/07/30 23:55:54 Please add a comment why this is necessary.
+ {
+ CopyCounter empty;
+ return empty;
+ }
+#endif
+ static const bool hasIsEmptyValueFunction = true;
+ static bool isEmptyValue(const CopyCounter& counter) { return !counter.data(); }
+};
+
+TEST(HashMapTest, LookupNoKeyCopies)
+{
+ HashMap<CopyCounter, int, CopyCounterHash, CopyCounterHashTraits> map;
+
+ map.contains(CopyCounter(1));
+ auto it = map.find(CopyCounter(1));
+ ALLOW_UNUSED_LOCAL(it);
+ EXPECT_EQ(0, CopyCounter::s_counter);
+}
+
} // anonymous namespace
} // namespace WTF
« no previous file with comments | « no previous file | Source/wtf/HashTable.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698