Title: [173807] trunk/Source/WTF
Revision
173807
Author
[email protected]
Date
2014-09-22 00:20:31 -0700 (Mon, 22 Sep 2014)

Log Message

Eliminate redundant PtrHash specializations
https://bugs.webkit.org/show_bug.cgi?id=136990

Patch by Sam Weinig <[email protected]> on 2014-09-22
Reviewed by Darin Adler.

Now that we have IsSmartPtr, we can eliminate all the specializations
of PtrHash that we had (for RefPtr, OwnPtr, RetainPtr, and std::unique_ptr)
and instead just have one that uses GetPtrHelper.

No changes of behavior intended.

* wtf/GetPtr.h:
Remove unnecessary const_cast that Darin noticed.

* wtf/HashFunctions.h:
(WTF::PtrHash::hash): Deleted.
(WTF::PtrHash::equal): Deleted.
(WTF::PtrHash<RefPtr<P>>::hash): Deleted.
(WTF::PtrHash<RefPtr<P>>::equal): Deleted.
Specialize PtrHash based on whether the type is a smart pointer, and use GetPtrHelper
to implement the smart pointer specialized variant.

* wtf/HashMap.h:
Remove include of GetPtr.h that is now included by HashFunctions.h

* wtf/OwnPtr.h:
(WTF::PtrHash<OwnPtr<P>>::hash): Deleted.
(WTF::PtrHash<OwnPtr<P>>::equal): Deleted.
Remove unnecessary specialization of PtrHash.

* wtf/RetainPtr.h:
(WTF::PtrHash<RetainPtr<P>>::hash): Deleted.
(WTF::PtrHash<RetainPtr<P>>::equal): Deleted.
Specialize IsSmartPointer for RetainPtr and remove the then unnecessary specialization of PtrHash.
(This is already tested in the API test WTF/cf/RetainPtrHashing).

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (173806 => 173807)


--- trunk/Source/WTF/ChangeLog	2014-09-22 07:08:42 UTC (rev 173806)
+++ trunk/Source/WTF/ChangeLog	2014-09-22 07:20:31 UTC (rev 173807)
@@ -1,3 +1,41 @@
+2014-09-22  Sam Weinig  <[email protected]>
+
+        Eliminate redundant PtrHash specializations
+        https://bugs.webkit.org/show_bug.cgi?id=136990
+
+        Reviewed by Darin Adler.
+
+        Now that we have IsSmartPtr, we can eliminate all the specializations
+        of PtrHash that we had (for RefPtr, OwnPtr, RetainPtr, and std::unique_ptr)
+        and instead just have one that uses GetPtrHelper.
+
+        No changes of behavior intended.
+
+        * wtf/GetPtr.h:
+        Remove unnecessary const_cast that Darin noticed.
+
+        * wtf/HashFunctions.h:
+        (WTF::PtrHash::hash): Deleted.
+        (WTF::PtrHash::equal): Deleted.
+        (WTF::PtrHash<RefPtr<P>>::hash): Deleted.
+        (WTF::PtrHash<RefPtr<P>>::equal): Deleted.
+        Specialize PtrHash based on whether the type is a smart pointer, and use GetPtrHelper
+        to implement the smart pointer specialized variant.
+
+        * wtf/HashMap.h:
+        Remove include of GetPtr.h that is now included by HashFunctions.h
+
+        * wtf/OwnPtr.h:
+        (WTF::PtrHash<OwnPtr<P>>::hash): Deleted.
+        (WTF::PtrHash<OwnPtr<P>>::equal): Deleted.
+        Remove unnecessary specialization of PtrHash.
+
+        * wtf/RetainPtr.h:
+        (WTF::PtrHash<RetainPtr<P>>::hash): Deleted.
+        (WTF::PtrHash<RetainPtr<P>>::equal): Deleted.
+        Specialize IsSmartPointer for RetainPtr and remove the then unnecessary specialization of PtrHash.
+        (This is already tested in the API test WTF/cf/RetainPtrHashing).
+
 2014-09-20  Sam Weinig  <[email protected]>
 
         Make possible HashSet<std::unique_ptr<>>

Modified: trunk/Source/WTF/wtf/GetPtr.h (173806 => 173807)


--- trunk/Source/WTF/wtf/GetPtr.h	2014-09-22 07:08:42 UTC (rev 173806)
+++ trunk/Source/WTF/wtf/GetPtr.h	2014-09-22 07:20:31 UTC (rev 173807)
@@ -71,7 +71,7 @@
 template <typename T, typename Deleter>
 struct GetPtrHelper<std::unique_ptr<T, Deleter>> {
     typedef T* PtrType;
-    static T* getPtr(const std::unique_ptr<T, Deleter>& p) { return const_cast<T*>(p.get()); }
+    static T* getPtr(const std::unique_ptr<T, Deleter>& p) { return p.get(); }
 };
 
 } // namespace WTF

Modified: trunk/Source/WTF/wtf/HashFunctions.h (173806 => 173807)


--- trunk/Source/WTF/wtf/HashFunctions.h	2014-09-22 07:08:42 UTC (rev 173806)
+++ trunk/Source/WTF/wtf/HashFunctions.h	2014-09-22 07:20:31 UTC (rev 173807)
@@ -21,8 +21,9 @@
 #ifndef WTF_HashFunctions_h
 #define WTF_HashFunctions_h
 
+#include <stdint.h>
+#include <wtf/GetPtr.h>
 #include <wtf/RefPtr.h>
-#include <stdint.h>
 
 namespace WTF {
 
@@ -119,31 +120,33 @@
 
     // pointer identity hash function
 
-    template<typename T> struct PtrHash {
-        static unsigned hash(T key)
-        {
-            return IntHash<uintptr_t>::hash(reinterpret_cast<uintptr_t>(key));
-        }
-        static bool equal(T a, T b) { return a == b; }
+    template<typename T, bool isSmartPointer> 
+    struct PtrHashBase;
+
+    template <typename T>
+    struct PtrHashBase<T, false /* isSmartPtr */> {
+        typedef T PtrType; 
+
+        static unsigned hash(PtrType key) { return IntHash<uintptr_t>::hash(reinterpret_cast<uintptr_t>(key)); }
+        static bool equal(PtrType a, PtrType b) { return a == b; }
         static const bool safeToCompareToEmptyOrDeleted = true;
     };
 
-    template<typename P> struct PtrHash<RefPtr<P>> : PtrHash<P*> {
-        using PtrHash<P*>::hash;
-        static unsigned hash(const RefPtr<P>& key) { return hash(key.get()); }
-        using PtrHash<P*>::equal;
-        static bool equal(const RefPtr<P>& a, const RefPtr<P>& b) { return a == b; }
-        static bool equal(P* a, const RefPtr<P>& b) { return a == b; }
-        static bool equal(const RefPtr<P>& a, P* b) { return a == b; }
+    template <typename T>
+    struct PtrHashBase<T, true /* isSmartPtr */> {
+        typedef typename GetPtrHelper<T>::PtrType PtrType; 
+
+        static unsigned hash(PtrType key) { return IntHash<uintptr_t>::hash(reinterpret_cast<uintptr_t>(key)); }
+        static bool equal(PtrType a, PtrType b) { return a == b; }
+        static const bool safeToCompareToEmptyOrDeleted = true;
+
+        static unsigned hash(const T& key) { return hash(getPtr(key)); }
+        static bool equal(const T& a, const T& b) { return getPtr(a) == getPtr(b); }
+        static bool equal(PtrType a, const T& b) { return a == getPtr(b); }
+        static bool equal(const T& a, PtrType b) { return getPtr(a) == b; }
     };
 
-    template<typename P, typename Deleter> struct PtrHash<std::unique_ptr<P, Deleter>> : PtrHash<P*> {
-        using PtrHash<P*>::hash;
-        static unsigned hash(const std::unique_ptr<P, Deleter>& key) { return hash(key.get()); }
-        using PtrHash<P*>::equal;
-        static bool equal(const std::unique_ptr<P, Deleter>& a, const std::unique_ptr<P, Deleter>& b) { return a.get() == b.get(); }
-        static bool equal(P* a, const std::unique_ptr<P, Deleter>& b) { return a == b.get(); }
-        static bool equal(const std::unique_ptr<P, Deleter>& a, P* b) { return a.get() == b; }
+    template<typename T> struct PtrHash : PtrHashBase<T, IsSmartPtr<T>::value> {
     };
 
     // default hash function for each type
@@ -159,8 +162,7 @@
         {
             return DefaultHash<T>::Hash::equal(a.first, b.first) && DefaultHash<U>::Hash::equal(a.second, b.second);
         }
-        static const bool safeToCompareToEmptyOrDeleted = DefaultHash<T>::Hash::safeToCompareToEmptyOrDeleted 
-                                                            && DefaultHash<U>::Hash::safeToCompareToEmptyOrDeleted;
+        static const bool safeToCompareToEmptyOrDeleted = DefaultHash<T>::Hash::safeToCompareToEmptyOrDeleted && DefaultHash<U>::Hash::safeToCompareToEmptyOrDeleted;
     };
 
     template<typename T, typename U> struct IntPairHash {

Modified: trunk/Source/WTF/wtf/HashMap.h (173806 => 173807)


--- trunk/Source/WTF/wtf/HashMap.h	2014-09-22 07:08:42 UTC (rev 173806)
+++ trunk/Source/WTF/wtf/HashMap.h	2014-09-22 07:20:31 UTC (rev 173807)
@@ -22,7 +22,6 @@
 #define WTF_HashMap_h
 
 #include <initializer_list>
-#include <wtf/GetPtr.h>
 #include <wtf/HashTable.h>
 #include <wtf/IteratorRange.h>
 

Modified: trunk/Source/WTF/wtf/OwnPtr.h (173806 => 173807)


--- trunk/Source/WTF/wtf/OwnPtr.h	2014-09-22 07:08:42 UTC (rev 173806)
+++ trunk/Source/WTF/wtf/OwnPtr.h	2014-09-22 07:20:31 UTC (rev 173807)
@@ -201,10 +201,6 @@
         return a != b.get(); 
     }
 
-    template <typename T> struct IsSmartPtr<OwnPtr<T>> {
-        static const bool value = true;
-    };
-
     template<typename T> template<typename... Args> inline void OwnPtr<T>::createTransactionally(Args... args)
     {
         if (m_ptr) {
@@ -227,16 +223,13 @@
 #endif
     }
 
-    template<typename P> struct PtrHash<OwnPtr<P>> : PtrHash<P*> {
-        using PtrHash<P*>::hash;
-        static unsigned hash(const OwnPtr<P>& key) { return hash(key.get()); }
-        using PtrHash<P*>::equal;
-        static bool equal(const OwnPtr<P>& a, const OwnPtr<P>& b) { return a.get() == b.get(); }
-        static bool equal(P* a, const OwnPtr<P>& b) { return a == b.get(); }
-        static bool equal(const OwnPtr<P>& a, P* b) { return a.get() == b; }
+    template <typename T> struct IsSmartPtr<OwnPtr<T>> {
+        static const bool value = true;
     };
 
-    template<typename P> struct DefaultHash<OwnPtr<P>> { typedef PtrHash<OwnPtr<P>> Hash; };
+    template<typename P> struct DefaultHash<OwnPtr<P>> {
+        typedef PtrHash<OwnPtr<P>> Hash;
+    };
 
 } // namespace WTF
 

Modified: trunk/Source/WTF/wtf/RetainPtr.h (173806 => 173807)


--- trunk/Source/WTF/wtf/RetainPtr.h	2014-09-22 07:08:42 UTC (rev 173806)
+++ trunk/Source/WTF/wtf/RetainPtr.h	2014-09-22 07:20:31 UTC (rev 173807)
@@ -277,18 +277,16 @@
         return ptr;
     }
 
-    template<typename P> struct HashTraits<RetainPtr<P>> : SimpleClassHashTraits<RetainPtr<P>> { };
-    
-    template<typename P> struct PtrHash<RetainPtr<P>> : PtrHash<typename RetainPtr<P>::PtrType> {
-        using PtrHash<typename RetainPtr<P>::PtrType>::hash;
-        static unsigned hash(const RetainPtr<P>& key) { return hash(key.get()); }
-        using PtrHash<typename RetainPtr<P>::PtrType>::equal;
-        static bool equal(const RetainPtr<P>& a, const RetainPtr<P>& b) { return a == b; }
-        static bool equal(typename RetainPtr<P>::PtrType a, const RetainPtr<P>& b) { return a == b; }
-        static bool equal(const RetainPtr<P>& a, typename RetainPtr<P>::PtrType b) { return a == b; }
+    template <typename T> struct IsSmartPtr<RetainPtr<T>> {
+        static const bool value = true;
     };
+
+    template<typename P> struct HashTraits<RetainPtr<P>> : SimpleClassHashTraits<RetainPtr<P>> {
+    };
     
-    template<typename P> struct DefaultHash<RetainPtr<P>> { typedef PtrHash<RetainPtr<P>> Hash; };
+    template<typename P> struct DefaultHash<RetainPtr<P>> {
+        typedef PtrHash<RetainPtr<P>> Hash;
+    };
 
     template <typename P>
     struct RetainPtrObjectHashTraits : SimpleClassHashTraits<RetainPtr<P>> {
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to