Ignore:
Timestamp:
Apr 3, 2012, 10:28:13 PM (13 years ago)
Author:
[email protected]
Message:

First step toward incremental Weak<T> finalization
https://bugs.webkit.org/show_bug.cgi?id=82670

Reviewed by Filip Pizlo.

Source/JavaScriptCore:

This patch implements a Weak<T> heap that is compatible with incremental
finalization, while making as few behavior changes as possible. The behavior
changes it makes are:

(*) Weak<T>'s raw JSValue no longer reverts to JSValue() automatically --
instead, a separate flag indicates that the JSValue is no longer valid.
(This is required so that the JSValue can be preserved for later finalization.)
Objects dealing with WeakImpls directly must change to check the flag.

(*) Weak<T> is no longer a subclass of Handle<T>.

(*) DOM GC performance is different -- 9% faster in the geometric mean,
but 15% slower in one specific case:

gc-dom1.html: 6% faster
gc-dom2.html: 23% faster
gc-dom3.html: 17% faster
gc-dom4.html: 15% *slower*

The key features of this new heap are:

(*) Each block knows its own state, independent of any other blocks.

(*) Each block caches its own sweep result.

(*) The heap visits dead Weak<T>s at the end of GC. (It doesn't
mark them yet, since that would be a behavior change.)

  • API/JSCallbackObject.cpp:

(JSC::JSCallbackObjectData::finalize):

  • API/JSCallbackObjectFunctions.h:

(JSC::::init): Updated to use the new WeakHeap API.

  • CMakeLists.txt:
  • GNUmakefile.list.am:
  • JavaScriptCore.gypi:
  • JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj:
  • JavaScriptCore.xcodeproj/project.pbxproj:
  • Target.pri: Paid the build system tax since I added some new files.
  • heap/Handle.h: Made WeakBlock a friend and exposed slot() as public,

so we can keep passing a Handle<T> to finalizers, to avoid more surface
area change in this patch. A follow-up patch should change the type we
pass to finalizers.

  • heap/HandleHeap.cpp:

(JSC):
(JSC::HandleHeap::writeBarrier):
(JSC::HandleHeap::isLiveNode):

  • heap/HandleHeap.h:

(JSC):
(HandleHeap):
(Node):
(JSC::HandleHeap::Node::Node): Removed all code related to Weak<T>, since
we have a separate WeakHeap now.

  • heap/Heap.cpp:

(JSC::Heap::Heap): Removed m_extraCost because extra cost is accounted
for through our watermark now. Removed m_waterMark because it was unused.

(JSC::Heap::destroy): Updated for addition of WeakHeap.

(JSC::Heap::reportExtraMemoryCostSlowCase): Changed from using its own
variable to participating in the watermark strategy. I wanted to standardize
WeakHeap and all other Heap clients on this strategy, to make sure it's
accurate.

(JSC::Heap::markRoots): Updated for addition of WeakHeap. Added WeakHeap
dead visit pass, as explained above.

(JSC::Heap::collect):
(JSC::Heap::resetAllocators): Updated for addition of WeakHeap.

(JSC::Heap::addFinalizer):
(JSC::Heap::FinalizerOwner::finalize): Updated for new Weak<T> API.

  • heap/Heap.h:

(JSC::Heap::weakHeap):
(Heap):
(JSC::Heap::addToWaterMark): Added a way to participate in the watermarking
strategy, since this is the best way for WeakHeap to report its memory
cost. (I plan to update this in a follow-up patch to make it more accurate,
but for now it is not less accurate than it used to be.)

  • heap/MarkedSpace.cpp:

(JSC::MarkedSpace::MarkedSpace):
(JSC::MarkedSpace::resetAllocators):

  • heap/MarkedSpace.h:

(MarkedSpace):
(JSC::MarkedSpace::addToWaterMark):
(JSC::MarkedSpace::didConsumeFreeList): Removed m_nurseryWaterMark because
it was unused, and I didn't want to update WeakHeap to keep an usused
variable working. Added API for above.

  • heap/PassWeak.h:

(JSC):
(WeakImplAccessor):
(PassWeak):
(JSC::::operator):
(JSC::::get):
(JSC::::was):
(JSC::::PassWeak):
(JSC::::~PassWeak):
(JSC::UnspecifiedBoolType):
(JSC::::leakImpl):
(JSC::adoptWeak):

  • heap/Strong.h:

(JSC::Strong::operator!):
(Strong):
(JSC::Strong::operator UnspecifiedBoolType*):
(JSC::Strong::get):

  • heap/Weak.h:

(Weak):
(JSC::::Weak):
(JSC):
(JSC::::isHashTableDeletedValue):
(JSC::::~Weak):
(JSC::::swap):
(JSC::=):
(JSC::::operator):
(JSC::UnspecifiedBoolType):
(JSC::::release):
(JSC::::clear):
(JSC::::hashTableDeletedValue): Lots of code changes here, but they boil
down to two things:

(*) Allocate WeakImpls from the WeakHeap instead of Handles from the HandleHeap.

(*) Explicitly check WeakImpl::state() for non-liveness before returning
a value (explained above).

These files implement the new Weak<T> heap behavior described above:

  • heap/WeakBlock.cpp: Added.
  • heap/WeakBlock.h: Added.
  • heap/WeakHandleOwner.cpp: Added.
  • heap/WeakHandleOwner.h: Added.
  • heap/WeakHeap.cpp: Added.
  • heap/WeakHeap.h: Added.
  • heap/WeakImpl.h: Added.

One interesting difference from the old heap is that we don't allow
clients to overwrite a WeakImpl after allocating it, and we don't recycle
WeakImpls prior to garbage collection. This is required for lazy finalization,
but it will also help us esablish a useful invariant in the future: allocating
a WeakImpl will be a binding contract to run a finalizer at some point in the
future, even if the WeakImpl is later deallocated.

  • jit/JITStubs.cpp:

(JSC::JITThunks::hostFunctionStub): Check the Weak<T> for ! instead of
its JSValue, since that's our API contract now, and the JSValue might
be stale.

  • runtime/JSCell.h:

(JSC::jsCast): Allow casting NULL pointers because it's useful and harmless.

  • runtime/Structure.cpp:

(JSC::StructureTransitionTable::add): I can't remember why I did this.

  • runtime/StructureTransitionTable.h:
  • runtime/WeakGCMap.h: I had to update these classes because they allocate

and deallocate weak pointers manually. They should probably stop doing that.

Source/WebCore:

Updated WebCore for Weak<T> API changes.

  • bindings/js/DOMWrapperWorld.cpp:

(WebCore::JSStringOwner::finalize): We're not allowed to get() a dead Weak<T>
anymore, so use the debug-only was() helper function instead.

  • bindings/js/JSDOMBinding.h:

(WebCore::uncacheWrapper): Ditto.

  • bindings/js/JSNodeCustom.h:

(WebCore::setInlineCachedWrapper):
(WebCore::clearInlineCachedWrapper): We're not allowed to get() a dead
Weak<T>, so I had to push down these ASSERTs into ScriptWrappable.

  • bindings/js/JSNodeFilterCondition.cpp:

(WebCore::JSNodeFilterCondition::acceptNode): Updated for non-Handle-ness
of Weak<T>.

  • bindings/js/ScriptWrappable.h:

(WebCore::ScriptWrappable::setWrapper):
(WebCore::ScriptWrappable::clearWrapper): Use was(), as above.

Source/WebKit2:

Updated for API change.

  • WebProcess/Plugins/Netscape/NPRuntimeObjectMap.cpp:

(WebKit::NPRuntimeObjectMap::finalize):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/heap/PassWeak.h

    r110033 r113141  
    2727#define PassWeak_h
    2828
     29#include "JSCell.h"
    2930#include <wtf/Assertions.h>
    30 #include "Handle.h"
    3131#include <wtf/NullPtr.h>
    3232#include <wtf/TypeTraits.h>
     
    3636template<typename T> class Weak;
    3737template<typename T> class PassWeak;
    38 template<typename T> PassWeak<T> adoptWeak(HandleSlot);
    39 
    40 template<typename T> class PassWeak : public Handle<T> {
    41     using Handle<T>::slot;
    42     using Handle<T>::setSlot;
    43 
     38template<typename T> PassWeak<T> adoptWeak(WeakImpl*);
     39
     40template<typename Base, typename T> class WeakImplAccessor {
    4441public:
    45     typedef typename Handle<T>::ExternalType ExternalType;
    46 
    47     PassWeak() : Handle<T>() { }
    48     PassWeak(std::nullptr_t) : Handle<T>() { }
    49 
    50     PassWeak(JSGlobalData& globalData, ExternalType externalType = ExternalType(), WeakHandleOwner* weakOwner = 0, void* context = 0)
    51         : Handle<T>(globalData.heap.handleHeap()->allocate())
    52     {
    53         HandleHeap::heapFor(slot())->makeWeak(slot(), weakOwner, context);
    54         JSValue value = HandleTypes<T>::toJSValue(externalType);
    55         HandleHeap::heapFor(slot())->writeBarrier(slot(), value);
    56         *slot() = value;
    57     }
     42    typedef T* GetType;
     43
     44    T* operator->() const;
     45    T& operator*() const;
     46    GetType get() const;
     47
     48#if !ASSERT_DISABLED
     49    bool was(GetType) const;
     50#endif
     51};
     52
     53template<typename Base> class WeakImplAccessor<Base, Unknown> {
     54public:
     55    typedef JSValue GetType;
     56
     57    const JSValue* operator->() const;
     58    const JSValue& operator*() const;
     59    GetType get() const;
     60};
     61
     62template<typename T> class PassWeak : public WeakImplAccessor<PassWeak<T>, T> {
     63public:
     64    friend class WeakImplAccessor<PassWeak<T>, T>;
     65    typedef typename WeakImplAccessor<PassWeak<T>, T>::GetType GetType;
     66
     67    PassWeak();
     68    PassWeak(std::nullptr_t);
     69    PassWeak(JSGlobalData&, GetType = GetType(), WeakHandleOwner* = 0, void* context = 0);
    5870
    5971    // It somewhat breaks the type system to allow transfer of ownership out of
    6072    // a const PassWeak. However, it makes it much easier to work with PassWeak
    6173    // temporaries, and we don't have a need to use real const PassWeaks anyway.
    62     PassWeak(const PassWeak& o) : Handle<T>(o.leakHandle()) { }
    63     template<typename U> PassWeak(const PassWeak<U>& o) : Handle<T>(o.leakHandle()) { }
    64 
    65     ~PassWeak()
    66     {
    67         if (!slot())
    68             return;
    69         HandleHeap::heapFor(slot())->deallocate(slot());
    70         setSlot(0);
    71     }
    72 
    73     ExternalType get() const { return  HandleTypes<T>::getFromSlot(slot()); }
    74 
    75     HandleSlot leakHandle() const WARN_UNUSED_RETURN;
     74    PassWeak(const PassWeak&);
     75    template<typename U> PassWeak(const PassWeak<U>&);
     76
     77    ~PassWeak();
     78
     79    bool operator!() const;
     80
     81    // This conversion operator allows implicit conversion to bool but not to other integer types.
     82    typedef JSValue (PassWeak::*UnspecifiedBoolType);
     83    operator UnspecifiedBoolType*() const;
     84
     85    WeakImpl* leakImpl() const WARN_UNUSED_RETURN;
    7686
    7787private:
    78     friend PassWeak adoptWeak<T>(HandleSlot);
    79 
    80     explicit PassWeak(HandleSlot slot) : Handle<T>(slot) { }
     88    friend PassWeak adoptWeak<T>(WeakImpl*);
     89    explicit PassWeak(WeakImpl*);
     90
     91    WeakImpl* m_impl;
    8192};
    8293
    83 template<typename T> inline HandleSlot PassWeak<T>::leakHandle() const
    84 {
    85     HandleSlot slot = this->slot();
    86     const_cast<PassWeak<T>*>(this)->setSlot(0);
    87     return slot;
    88 }
    89 
    90 template<typename T> PassWeak<T> adoptWeak(HandleSlot slot)
    91 {
    92     return PassWeak<T>(slot);
     94template<typename Base, typename T> inline T* WeakImplAccessor<Base, T>::operator->() const
     95{
     96    ASSERT(static_cast<const Base*>(this)->m_impl && static_cast<const Base*>(this)->m_impl->state() == WeakImpl::Live);
     97    return jsCast<T*>(static_cast<const Base*>(this)->m_impl->jsValue().asCell());
     98}
     99
     100template<typename Base, typename T> inline T& WeakImplAccessor<Base, T>::operator*() const
     101{
     102    ASSERT(static_cast<const Base*>(this)->m_impl && static_cast<const Base*>(this)->m_impl->state() == WeakImpl::Live);
     103    return *jsCast<T*>(static_cast<const Base*>(this)->m_impl->jsValue().asCell());
     104}
     105
     106template<typename Base, typename T> inline typename WeakImplAccessor<Base, T>::GetType WeakImplAccessor<Base, T>::get() const
     107{
     108    if (!static_cast<const Base*>(this)->m_impl || static_cast<const Base*>(this)->m_impl->state() != WeakImpl::Live)
     109        return GetType();
     110    return jsCast<T*>(static_cast<const Base*>(this)->m_impl->jsValue().asCell());
     111}
     112
     113#if !ASSERT_DISABLED
     114template<typename Base, typename T> inline bool WeakImplAccessor<Base, T>::was(typename WeakImplAccessor<Base, T>::GetType other) const
     115{
     116    return jsCast<T*>(static_cast<const Base*>(this)->m_impl->jsValue().asCell()) == other;
     117}
     118#endif
     119
     120template<typename Base> inline const JSValue* WeakImplAccessor<Base, Unknown>::operator->() const
     121{
     122    ASSERT(static_cast<const Base*>(this)->m_impl && static_cast<const Base*>(this)->m_impl->state() == WeakImpl::Live);
     123    return &static_cast<const Base*>(this)->m_impl->jsValue();
     124}
     125
     126template<typename Base> inline const JSValue& WeakImplAccessor<Base, Unknown>::operator*() const
     127{
     128    ASSERT(static_cast<const Base*>(this)->m_impl && static_cast<const Base*>(this)->m_impl->state() == WeakImpl::Live);
     129    return static_cast<const Base*>(this)->m_impl->jsValue();
     130}
     131
     132template<typename Base> inline typename WeakImplAccessor<Base, Unknown>::GetType WeakImplAccessor<Base, Unknown>::get() const
     133{
     134    if (!static_cast<const Base*>(this)->m_impl || static_cast<const Base*>(this)->m_impl->state() != WeakImpl::Live)
     135        return GetType();
     136    return static_cast<const Base*>(this)->m_impl->jsValue();
     137}
     138
     139template<typename T> inline PassWeak<T>::PassWeak()
     140    : m_impl(0)
     141{
     142}
     143
     144template<typename T> inline PassWeak<T>::PassWeak(std::nullptr_t)
     145    : m_impl(0)
     146{
     147}
     148
     149template<typename T> inline PassWeak<T>::PassWeak(JSGlobalData& globalData, typename PassWeak<T>::GetType getType, WeakHandleOwner* weakOwner, void* context)
     150    : m_impl(globalData.heap.weakHeap()->allocate(getType, weakOwner, context))
     151{
     152}
     153
     154template<typename T> inline PassWeak<T>::PassWeak(const PassWeak& o)
     155    : m_impl(o.leakImpl())
     156{
     157}
     158
     159template<typename T> template<typename U> inline PassWeak<T>::PassWeak(const PassWeak<U>& o)
     160    : m_impl(o.leakImpl())
     161{
     162}
     163
     164template<typename T> inline PassWeak<T>::~PassWeak()
     165{
     166    if (!m_impl)
     167        return;
     168    WeakHeap::deallocate(m_impl);
     169}
     170
     171template<typename T> inline bool PassWeak<T>::operator!() const
     172{
     173    return !m_impl || m_impl->state() != WeakImpl::Live || !m_impl->jsValue();
     174}
     175
     176template<typename T> inline PassWeak<T>::operator UnspecifiedBoolType*() const
     177{
     178    return reinterpret_cast<UnspecifiedBoolType*>(!!*this);
     179}
     180
     181template<typename T> inline PassWeak<T>::PassWeak(WeakImpl* impl)
     182: m_impl(impl)
     183{
     184}
     185
     186template<typename T> inline WeakImpl* PassWeak<T>::leakImpl() const
     187{
     188    WeakImpl* tmp = 0;
     189    std::swap(tmp, const_cast<WeakImpl*&>(m_impl));
     190    return tmp;
     191}
     192
     193template<typename T> PassWeak<T> inline adoptWeak(WeakImpl* impl)
     194{
     195    return PassWeak<T>(impl);
    93196}
    94197
Note: See TracChangeset for help on using the changeset viewer.