Title: [123667] trunk/Source
Revision
123667
Author
caio.olive...@openbossa.org
Date
2012-07-25 15:15:05 -0700 (Wed, 25 Jul 2012)

Log Message

Source/_javascript_Core: Create a specialized pair for use in HashMap iterators
https://bugs.webkit.org/show_bug.cgi?id=92137

Reviewed by Ryosuke Niwa.

Update a couple of sites that relied on the fact that "contents" of iterators were
std::pairs.

* profiler/Profile.cpp:
(JSC): This code kept a vector of the pairs that were the "contents" of the iterators. This
is changed to use a KeyValuePair. We make use HashCount's ValueType (which represents only
the key) to get the proper key parameter for KeyValuePair.
* tools/ProfileTreeNode.h:
(ProfileTreeNode): Use HashMap::ValueType to declare the type of the contents of the hash
instead of declaring it manually. This will make use of the new KeyValuePair.

Source/WebKit2: Create a specialized struct for use in HashMap iterators
https://bugs.webkit.org/show_bug.cgi?id=92137

Reviewed by Ryosuke Niwa.

* Platform/CoreIPC/ArgumentCoders.h: Add encode/decode for KeyValuePair.

Source/WTF: Create a specialized pair for use in HashMap iterators
https://bugs.webkit.org/show_bug.cgi?id=92137

Reviewed by Ryosuke Niwa.

The type used for internal storage in HashMap is exposed in its interface as iterator
"contents". Currently HashMap uses std::pair<>, which this patch replaces with
KeyValuePair.

Having this specialized structure will allow us to customize the members to be called
key/value, improving readability in code using HashMap iterators. They are still called
first/second to separate this change from the mechanical change of updating callsites.

* wtf/HashIterators.h:
(HashTableConstKeysIterator):
(HashTableConstValuesIterator):
(HashTableKeysIterator):
(HashTableValuesIterator):
Use KeyValuePair instead of std::pair when defining the iterators.

* wtf/HashMap.h:
(WTF):
(WTF::KeyValuePairKeyExtractor::extract):
(HashMap):
Remove PairFirstExtractor. Add and use the KeyValuePair corresponding extractor.

(WTF::HashMapValueTraits::isEmptyValue): Use KeyValuePairHashTraits for HashMaps.
(WTF::HashMapTranslator::translate):
(WTF::HashMapTranslatorAdapter::translate):
The traits of the mapped value is now called ValueTraits instead of SecondTraits.

* wtf/HashTable.h:
(WTF::hashTableSwap): Add specialization for swapping KeyValuePairs.
(WTF): Remove now unneeded specialization for std::pairs.

* wtf/HashTraits.h:
(KeyValuePair):
(WTF::KeyValuePair::KeyValuePair):
(WTF):
Specialized pair. In the future difference from pair should be the member names.

(KeyValuePairHashTraits):
(WTF::KeyValuePairHashTraits::emptyValue):
(WTF::KeyValuePairHashTraits::constructDeletedValue):
(WTF::KeyValuePairHashTraits::isDeletedValue):
These traits are analogous to PairHashTraits but for KeyValuePair.

* wtf/RefPtrHashMap.h: Use KeyValuePairHashTraits.

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (123666 => 123667)


--- trunk/Source/_javascript_Core/ChangeLog	2012-07-25 22:07:50 UTC (rev 123666)
+++ trunk/Source/_javascript_Core/ChangeLog	2012-07-25 22:15:05 UTC (rev 123667)
@@ -1,3 +1,21 @@
+2012-07-25  Caio Marcelo de Oliveira Filho  <caio.olive...@openbossa.org>
+
+        Create a specialized pair for use in HashMap iterators
+        https://bugs.webkit.org/show_bug.cgi?id=92137
+
+        Reviewed by Ryosuke Niwa.
+
+        Update a couple of sites that relied on the fact that "contents" of iterators were
+        std::pairs.
+
+        * profiler/Profile.cpp:
+        (JSC): This code kept a vector of the pairs that were the "contents" of the iterators. This
+        is changed to use a KeyValuePair. We make use HashCount's ValueType (which represents only
+        the key) to get the proper key parameter for KeyValuePair.
+        * tools/ProfileTreeNode.h:
+        (ProfileTreeNode): Use HashMap::ValueType to declare the type of the contents of the hash
+        instead of declaring it manually. This will make use of the new KeyValuePair.
+
 2012-07-25  Patrick Gansterer  <par...@webkit.org>
 
         REGRESSION(r123505): Date.getYear() returns the same as Date.getFullYear()

Modified: trunk/Source/_javascript_Core/profiler/Profile.cpp (123666 => 123667)


--- trunk/Source/_javascript_Core/profiler/Profile.cpp	2012-07-25 22:07:50 UTC (rev 123666)
+++ trunk/Source/_javascript_Core/profiler/Profile.cpp	2012-07-25 22:15:05 UTC (rev 123667)
@@ -107,7 +107,7 @@
     m_head->debugPrintData(0);
 }
 
-typedef pair<StringImpl*, unsigned> NameCountPair;
+typedef WTF::KeyValuePair<FunctionCallHashCount::ValueType, unsigned> NameCountPair;
 
 static inline bool functionNameCountPairComparator(const NameCountPair& a, const NameCountPair& b)
 {

Modified: trunk/Source/_javascript_Core/tools/ProfileTreeNode.h (123666 => 123667)


--- trunk/Source/_javascript_Core/tools/ProfileTreeNode.h	2012-07-25 22:07:50 UTC (rev 123666)
+++ trunk/Source/_javascript_Core/tools/ProfileTreeNode.h	2012-07-25 22:15:05 UTC (rev 123667)
@@ -30,7 +30,7 @@
 
 class ProfileTreeNode {
     typedef HashMap<String, ProfileTreeNode> Map;
-    typedef std::pair<String, ProfileTreeNode> MapEntry;
+    typedef Map::ValueType MapEntry;
 
 public:
     ProfileTreeNode()

Modified: trunk/Source/WTF/ChangeLog (123666 => 123667)


--- trunk/Source/WTF/ChangeLog	2012-07-25 22:07:50 UTC (rev 123666)
+++ trunk/Source/WTF/ChangeLog	2012-07-25 22:15:05 UTC (rev 123667)
@@ -1,3 +1,54 @@
+2012-07-25  Caio Marcelo de Oliveira Filho  <caio.olive...@openbossa.org>
+
+        Create a specialized pair for use in HashMap iterators
+        https://bugs.webkit.org/show_bug.cgi?id=92137
+
+        Reviewed by Ryosuke Niwa.
+
+        The type used for internal storage in HashMap is exposed in its interface as iterator
+        "contents". Currently HashMap uses std::pair<>, which this patch replaces with
+        KeyValuePair.
+
+        Having this specialized structure will allow us to customize the members to be called
+        key/value, improving readability in code using HashMap iterators. They are still called
+        first/second to separate this change from the mechanical change of updating callsites.
+
+        * wtf/HashIterators.h:
+        (HashTableConstKeysIterator):
+        (HashTableConstValuesIterator):
+        (HashTableKeysIterator):
+        (HashTableValuesIterator):
+        Use KeyValuePair instead of std::pair when defining the iterators.
+
+        * wtf/HashMap.h:
+        (WTF):
+        (WTF::KeyValuePairKeyExtractor::extract):
+        (HashMap):
+        Remove PairFirstExtractor. Add and use the KeyValuePair corresponding extractor.
+
+        (WTF::HashMapValueTraits::isEmptyValue): Use KeyValuePairHashTraits for HashMaps.
+        (WTF::HashMapTranslator::translate):
+        (WTF::HashMapTranslatorAdapter::translate):
+        The traits of the mapped value is now called ValueTraits instead of SecondTraits.
+
+        * wtf/HashTable.h:
+        (WTF::hashTableSwap): Add specialization for swapping KeyValuePairs.
+        (WTF): Remove now unneeded specialization for std::pairs.
+
+        * wtf/HashTraits.h:
+        (KeyValuePair):
+        (WTF::KeyValuePair::KeyValuePair):
+        (WTF):
+        Specialized pair. In the future difference from pair should be the member names.
+
+        (KeyValuePairHashTraits):
+        (WTF::KeyValuePairHashTraits::emptyValue):
+        (WTF::KeyValuePairHashTraits::constructDeletedValue):
+        (WTF::KeyValuePairHashTraits::isDeletedValue):
+        These traits are analogous to PairHashTraits but for KeyValuePair.
+
+        * wtf/RefPtrHashMap.h: Use KeyValuePairHashTraits.
+
 2012-07-25  Andrew Wilson  <atwil...@chromium.org>
 
         Unreviewed, rolling out r123560.

Modified: trunk/Source/WTF/wtf/HashIterators.h (123666 => 123667)


--- trunk/Source/WTF/wtf/HashIterators.h	2012-07-25 22:07:50 UTC (rev 123666)
+++ trunk/Source/WTF/wtf/HashIterators.h	2012-07-25 22:15:05 UTC (rev 123667)
@@ -33,9 +33,9 @@
     template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableKeysIterator;
     template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableValuesIterator;
 
-    template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > {
+    template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstIteratorAdapter<HashTableType, KeyValuePair<KeyType, MappedType> > {
     private:
-        typedef std::pair<KeyType, MappedType> ValueType;
+        typedef KeyValuePair<KeyType, MappedType> ValueType;
     public:
         typedef HashTableConstKeysIterator<HashTableType, KeyType, MappedType> Keys;
         typedef HashTableConstValuesIterator<HashTableType, KeyType, MappedType> Values;
@@ -56,9 +56,9 @@
         typename HashTableType::const_iterator m_impl;
     };
 
-    template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > {
+    template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableIteratorAdapter<HashTableType, KeyValuePair<KeyType, MappedType> > {
     private:
-        typedef std::pair<KeyType, MappedType> ValueType;
+        typedef KeyValuePair<KeyType, MappedType> ValueType;
     public:
         typedef HashTableKeysIterator<HashTableType, KeyType, MappedType> Keys;
         typedef HashTableValuesIterator<HashTableType, KeyType, MappedType> Values;
@@ -86,7 +86,7 @@
 
     template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstKeysIterator {
     private:
-        typedef HashTableConstIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > ConstIterator;
+        typedef HashTableConstIteratorAdapter<HashTableType, KeyValuePair<KeyType, MappedType> > ConstIterator;
 
     public:
         HashTableConstKeysIterator(const ConstIterator& impl) : m_impl(impl) {}
@@ -103,7 +103,7 @@
 
     template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableConstValuesIterator {
     private:
-        typedef HashTableConstIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > ConstIterator;
+        typedef HashTableConstIteratorAdapter<HashTableType, KeyValuePair<KeyType, MappedType> > ConstIterator;
 
     public:
         HashTableConstValuesIterator(const ConstIterator& impl) : m_impl(impl) {}
@@ -120,8 +120,8 @@
 
     template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableKeysIterator {
     private:
-        typedef HashTableIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > Iterator;
-        typedef HashTableConstIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > ConstIterator;
+        typedef HashTableIteratorAdapter<HashTableType, KeyValuePair<KeyType, MappedType> > Iterator;
+        typedef HashTableConstIteratorAdapter<HashTableType, KeyValuePair<KeyType, MappedType> > ConstIterator;
 
     public:
         HashTableKeysIterator(const Iterator& impl) : m_impl(impl) {}
@@ -143,8 +143,8 @@
 
     template<typename HashTableType, typename KeyType, typename MappedType> struct HashTableValuesIterator {
     private:
-        typedef HashTableIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > Iterator;
-        typedef HashTableConstIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> > ConstIterator;
+        typedef HashTableIteratorAdapter<HashTableType, KeyValuePair<KeyType, MappedType> > Iterator;
+        typedef HashTableConstIteratorAdapter<HashTableType, KeyValuePair<KeyType, MappedType> > ConstIterator;
 
     public:
         HashTableValuesIterator(const Iterator& impl) : m_impl(impl) {}

Modified: trunk/Source/WTF/wtf/HashMap.h (123666 => 123667)


--- trunk/Source/WTF/wtf/HashMap.h	2012-07-25 22:07:50 UTC (rev 123666)
+++ trunk/Source/WTF/wtf/HashMap.h	2012-07-25 22:15:05 UTC (rev 123667)
@@ -26,7 +26,6 @@
 namespace WTF {
 
     template<typename KeyTraits, typename MappedTraits> struct HashMapValueTraits;
-    template<typename PairType> struct PairFirstExtractor;
 
     template<typename T> struct ReferenceTypeMaker {
         typedef T& ReferenceType;
@@ -35,6 +34,10 @@
         typedef T& ReferenceType;
     };
 
+    template<typename T> struct KeyValuePairKeyExtractor {
+        static const typename T::KeyType& extract(const T& p) { return p.first; }
+    };
+
     template<typename KeyArg, typename MappedArg, typename HashArg = typename DefaultHash<KeyArg>::Hash,
         typename KeyTraitsArg = HashTraits<KeyArg>, typename MappedTraitsArg = HashTraits<MappedArg> >
     class HashMap {
@@ -58,7 +61,7 @@
 
         typedef HashArg HashFunctions;
 
-        typedef HashTable<KeyType, ValueType, PairFirstExtractor<ValueType>,
+        typedef HashTable<KeyType, ValueType, KeyValuePairKeyExtractor<ValueType>,
             HashFunctions, ValueTraits, KeyTraits> HashTableType;
 
         class HashMapKeysProxy;
@@ -204,18 +207,14 @@
         HashTableType m_impl;
     };
 
-    template<typename KeyTraits, typename MappedTraits> struct HashMapValueTraits : PairHashTraits<KeyTraits, MappedTraits> {
+    template<typename KeyTraits, typename MappedTraits> struct HashMapValueTraits : KeyValuePairHashTraits<KeyTraits, MappedTraits> {
         static const bool hasIsEmptyValueFunction = true;
-        static bool isEmptyValue(const typename PairHashTraits<KeyTraits, MappedTraits>::TraitType& value)
+        static bool isEmptyValue(const typename KeyValuePairHashTraits<KeyTraits, MappedTraits>::TraitType& value)
         {
             return isHashTraitsEmptyValue<KeyTraits>(value.first);
         }
     };
 
-    template<typename PairType> struct PairFirstExtractor {
-        static const typename PairType::first_type& extract(const PairType& p) { return p.first; }
-    };
-
     template<typename ValueTraits, typename HashFunctions>
     struct HashMapTranslator {
         template<typename T> static unsigned hash(const T& key) { return HashFunctions::hash(key); }
@@ -223,7 +222,7 @@
         template<typename T, typename U, typename V> static void translate(T& location, const U& key, const V& mapped)
         {
             location.first = key;
-            ValueTraits::SecondTraits::store(mapped, location.second);
+            ValueTraits::ValueTraits::store(mapped, location.second);
         }
     };
 
@@ -234,7 +233,7 @@
         template<typename T, typename U, typename V> static void translate(T& location, const U& key, const V& mapped, unsigned hashCode)
         {
             Translator::translate(location.first, key, hashCode);
-            ValueTraits::SecondTraits::store(mapped, location.second);
+            ValueTraits::ValueTraits::store(mapped, location.second);
         }
     };
 

Modified: trunk/Source/WTF/wtf/HashTable.h (123666 => 123667)


--- trunk/Source/WTF/wtf/HashTable.h	2012-07-25 22:07:50 UTC (rev 123666)
+++ trunk/Source/WTF/wtf/HashTable.h	2012-07-25 22:15:05 UTC (rev 123667)
@@ -284,8 +284,7 @@
         swap(a, b);
     }
 
-    // Swap pairs by component, in case of pair members that specialize swap.
-    template<typename T, typename U> inline void hashTableSwap(std::pair<T, U>& a, std::pair<T, U>& b)
+    template<typename T, typename U> inline void hashTableSwap(KeyValuePair<T, U>& a, KeyValuePair<T, U>& b)
     {
         swap(a.first, b.first);
         swap(a.second, b.second);

Modified: trunk/Source/WTF/wtf/HashTraits.h (123666 => 123667)


--- trunk/Source/WTF/wtf/HashTraits.h	2012-07-25 22:07:50 UTC (rev 123666)
+++ trunk/Source/WTF/wtf/HashTraits.h	2012-07-25 22:15:05 UTC (rev 123667)
@@ -163,8 +163,6 @@
         return HashTraitsEmptyValueChecker<Traits, Traits::hasIsEmptyValueFunction>::isEmptyValue(value);
     }
 
-    // special traits for pairs, helpful for their use in HashMap implementation
-
     template<typename FirstTraitsArg, typename SecondTraitsArg>
     struct PairHashTraits : GenericHashTraits<std::pair<typename FirstTraitsArg::TraitType, typename SecondTraitsArg::TraitType> > {
         typedef FirstTraitsArg FirstTraits;
@@ -186,6 +184,53 @@
     template<typename First, typename Second>
     struct HashTraits<std::pair<First, Second> > : public PairHashTraits<HashTraits<First>, HashTraits<Second> > { };
 
+    template<typename KeyTypeArg, typename ValueTypeArg>
+    struct KeyValuePair {
+        typedef KeyTypeArg KeyType;
+
+        KeyValuePair()
+        {
+        }
+
+        KeyValuePair(const KeyTypeArg& key, const ValueTypeArg& value)
+            : first(key)
+            , second(value)
+        {
+        }
+
+        template <typename OtherKeyType, typename OtherValueType>
+        KeyValuePair(const KeyValuePair<OtherKeyType, OtherValueType>& other)
+            : first(other.first)
+            , second(other.second)
+        {
+        }
+
+        // TODO: Rename these to key and value. See https://bugs.webkit.org/show_bug.cgi?id=82784.
+        KeyTypeArg first;
+        ValueTypeArg second;
+    };
+
+    template<typename KeyTraitsArg, typename ValueTraitsArg>
+    struct KeyValuePairHashTraits : GenericHashTraits<KeyValuePair<typename KeyTraitsArg::TraitType, typename ValueTraitsArg::TraitType> > {
+        typedef KeyTraitsArg KeyTraits;
+        typedef ValueTraitsArg ValueTraits;
+        typedef KeyValuePair<typename KeyTraits::TraitType, typename ValueTraits::TraitType> TraitType;
+        typedef KeyValuePair<typename KeyTraits::EmptyValueType, typename ValueTraits::EmptyValueType> EmptyValueType;
+
+        static const bool emptyValueIsZero = KeyTraits::emptyValueIsZero && ValueTraits::emptyValueIsZero;
+        static EmptyValueType emptyValue() { return KeyValuePair<typename KeyTraits::EmptyValueType, typename ValueTraits::EmptyValueType>(KeyTraits::emptyValue(), ValueTraits::emptyValue()); }
+
+        static const bool needsDestruction = KeyTraits::needsDestruction || ValueTraits::needsDestruction;
+
+        static const int minimumTableSize = KeyTraits::minimumTableSize;
+
+        static void constructDeletedValue(TraitType& slot) { KeyTraits::constructDeletedValue(slot.first); }
+        static bool isDeletedValue(const TraitType& value) { return KeyTraits::isDeletedValue(value.first); }
+    };
+
+    template<typename Key, typename Value>
+    struct HashTraits<KeyValuePair<Key, Value> > : public KeyValuePairHashTraits<HashTraits<Key>, HashTraits<Value> > { };
+
 } // namespace WTF
 
 using WTF::HashTraits;

Modified: trunk/Source/WTF/wtf/RefPtrHashMap.h (123666 => 123667)


--- trunk/Source/WTF/wtf/RefPtrHashMap.h	2012-07-25 22:07:50 UTC (rev 123666)
+++ trunk/Source/WTF/wtf/RefPtrHashMap.h	2012-07-25 22:15:05 UTC (rev 123667)
@@ -34,7 +34,7 @@
     private:
         typedef KeyTraitsArg KeyTraits;
         typedef MappedTraitsArg MappedTraits;
-        typedef PairHashTraits<KeyTraits, MappedTraits> ValueTraits;
+        typedef KeyValuePairHashTraits<KeyTraits, MappedTraits> ValueTraits;
 
     public:
         typedef typename KeyTraits::TraitType KeyType;
@@ -51,7 +51,7 @@
         
         typedef HashArg HashFunctions;
 
-        typedef HashTable<KeyType, ValueType, PairFirstExtractor<ValueType>,
+        typedef HashTable<KeyType, ValueType, KeyValuePairKeyExtractor<ValueType>,
             HashFunctions, ValueTraits, KeyTraits> HashTableType;
 
         typedef HashMapTranslator<ValueTraits, HashFunctions>

Modified: trunk/Source/WebKit2/ChangeLog (123666 => 123667)


--- trunk/Source/WebKit2/ChangeLog	2012-07-25 22:07:50 UTC (rev 123666)
+++ trunk/Source/WebKit2/ChangeLog	2012-07-25 22:15:05 UTC (rev 123667)
@@ -1,3 +1,12 @@
+2012-07-25  Caio Marcelo de Oliveira Filho  <caio.olive...@openbossa.org>
+
+        Create a specialized struct for use in HashMap iterators
+        https://bugs.webkit.org/show_bug.cgi?id=92137
+
+        Reviewed by Ryosuke Niwa.
+
+        * Platform/CoreIPC/ArgumentCoders.h: Add encode/decode for KeyValuePair.
+
 2012-07-25  Anders Carlsson  <ander...@apple.com>
 
         Crash when calling PluginView::pluginFocusOrWindowFocusChanged when the web page is null

Modified: trunk/Source/WebKit2/Platform/CoreIPC/ArgumentCoders.h (123666 => 123667)


--- trunk/Source/WebKit2/Platform/CoreIPC/ArgumentCoders.h	2012-07-25 22:07:50 UTC (rev 123666)
+++ trunk/Source/WebKit2/Platform/CoreIPC/ArgumentCoders.h	2012-07-25 22:15:05 UTC (rev 123667)
@@ -72,6 +72,29 @@
     }
 };
 
+template<typename KeyType, typename ValueType> struct ArgumentCoder<WTF::KeyValuePair<KeyType, ValueType> > {
+    static void encode(ArgumentEncoder* encoder, const WTF::KeyValuePair<KeyType, ValueType>& pair)
+    {
+        encoder->encode(pair.first);
+        encoder->encode(pair.second);
+    }
+
+    static bool decode(ArgumentDecoder* decoder, WTF::KeyValuePair<KeyType, ValueType>& pair)
+    {
+        KeyType key;
+        if (!decoder->decode(key))
+            return false;
+
+        ValueType value;
+        if (!decoder->decode(value))
+            return false;
+
+        pair.first = key;
+        pair.second = value;
+        return true;
+    }
+};
+
 template<bool fixedSizeElements, typename T> struct VectorArgumentCoder;
 
 template<typename T> struct VectorArgumentCoder<false, T> {
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to