Title: [222752] trunk/Source/WTF
Revision
222752
Author
[email protected]
Date
2017-10-02 15:21:12 -0700 (Mon, 02 Oct 2017)

Log Message

NULL WeakPtr should not malloc!
https://bugs.webkit.org/show_bug.cgi?id=177773

Reviewed by Antti Koivisto.

Translating NULL into malloc is... inefficient.

* wtf/WeakPtr.h:
(WTF::WeakPtr::WeakPtr):
(WTF::WeakPtr::operator=):
(WTF::WeakPtr::clear): Make m_ref lazy so that a NULL m_ref can represent
a NULL pointer. Normal dereference is no slower because we can rely on
the fact that dereference of NULL should crash. operator bool() and get()
incur an extra branch. That's probably worth it to avoid malloc
for NULL.

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (222751 => 222752)


--- trunk/Source/WTF/ChangeLog	2017-10-02 22:10:43 UTC (rev 222751)
+++ trunk/Source/WTF/ChangeLog	2017-10-02 22:21:12 UTC (rev 222752)
@@ -1,3 +1,21 @@
+2017-10-02  Geoffrey Garen  <[email protected]>
+
+        NULL WeakPtr should not malloc!
+        https://bugs.webkit.org/show_bug.cgi?id=177773
+
+        Reviewed by Antti Koivisto.
+
+        Translating NULL into malloc is... inefficient.
+
+        * wtf/WeakPtr.h:
+        (WTF::WeakPtr::WeakPtr):
+        (WTF::WeakPtr::operator=):
+        (WTF::WeakPtr::clear): Make m_ref lazy so that a NULL m_ref can represent
+        a NULL pointer. Normal dereference is no slower because we can rely on
+        the fact that dereference of NULL should crash. operator bool() and get()
+        incur an extra branch. That's probably worth it to avoid malloc
+        for NULL.
+
 2017-10-02  Antti Koivisto  <[email protected]>
 
         Add makeWeakPtr variant that takes pointer

Modified: trunk/Source/WTF/wtf/WeakPtr.h (222751 => 222752)


--- trunk/Source/WTF/wtf/WeakPtr.h	2017-10-02 22:10:43 UTC (rev 222751)
+++ trunk/Source/WTF/wtf/WeakPtr.h	2017-10-02 22:21:12 UTC (rev 222752)
@@ -65,24 +65,24 @@
 class WeakPtr {
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    WeakPtr() : m_ref(WeakReference<T>::create(nullptr)) { }
-    WeakPtr(std::nullptr_t) : m_ref(WeakReference<T>::create(nullptr)) { }
-    WeakPtr(const WeakPtr& o) : m_ref(o.m_ref.copyRef()) { }
+    WeakPtr() { }
+    WeakPtr(std::nullptr_t) { }
+    WeakPtr(const WeakPtr& o) : m_ref(o.m_ref) { }
     WeakPtr(Ref<WeakReference<T>>&& ref) : m_ref(std::forward<Ref<WeakReference<T>>>(ref)) { }
 
-    T* get() const { return m_ref->get(); }
-    operator bool() const { return m_ref->get(); }
+    T* get() const { return m_ref ? m_ref->get() : nullptr; }
+    operator bool() const { return m_ref && m_ref->get(); }
 
-    WeakPtr& operator=(const WeakPtr& o) { m_ref = o.m_ref.copyRef(); return *this; }
-    WeakPtr& operator=(std::nullptr_t) { m_ref = WeakReference<T>::create(nullptr); return *this; }
+    WeakPtr& operator=(const WeakPtr& o) { m_ref = o.m_ref; return *this; }
+    WeakPtr& operator=(std::nullptr_t) { m_ref = nullptr; return *this; }
 
-    T* operator->() const { return get(); }
-    T& operator*() const { return *get(); }
+    T* operator->() const { return m_ref->get(); }
+    T& operator*() const { return *m_ref->get(); }
 
-    void clear() { m_ref = WeakReference<T>::create(nullptr); }
+    void clear() { m_ref = nullptr; }
 
 private:
-    Ref<WeakReference<T>> m_ref;
+    RefPtr<WeakReference<T>> m_ref;
 };
 
 template<typename T>
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to