From ad5d1c0731669ca278ed58ccbeeb3fb3ef8ba6b7 Mon Sep 17 00:00:00 2001 From: Jon Coppeard Date: Thu, 11 Jul 2024 08:23:35 +0000 Subject: [PATCH] Bug 1907070 - Part 2: Make SweepingAPI.h a public header (backout of changeset 2592881b3f6d) r=sfink a=RyanVM Differential Revision: https://phabricator.services.mozilla.com/D216167 --- js/public/GCHashTable.h | 409 +++++++++++++++++++ js/public/SweepingAPI.h | 125 ++++++ js/src/gc/GCRuntime.h | 1 - js/src/gc/SweepingAPI.h | 530 ------------------------- js/src/jit/CacheIRCompiler.cpp | 2 +- js/src/jsapi-tests/testGCWeakCache.cpp | 2 +- js/src/moz.build | 1 + js/src/shell/js.cpp | 1 + js/src/vm/InvalidatingFuse.h | 2 +- js/src/vm/RegExpShared.h | 1 - js/src/vm/ShapeZone.h | 1 - js/src/wasm/WasmJS.h | 2 +- 12 files changed, 540 insertions(+), 537 deletions(-) create mode 100644 js/public/SweepingAPI.h delete mode 100644 js/src/gc/SweepingAPI.h diff --git a/js/public/GCHashTable.h b/js/public/GCHashTable.h index ec8fe10c251d..5d9b0e9c7710 100644 --- a/js/public/GCHashTable.h +++ b/js/public/GCHashTable.h @@ -12,6 +12,7 @@ #include "js/GCPolicyAPI.h" #include "js/HashTable.h" #include "js/RootingAPI.h" +#include "js/SweepingAPI.h" #include "js/TypeDecls.h" class JSTracer; @@ -394,4 +395,412 @@ class MutableWrappedPtrOperations, Wrapper> } /* namespace js */ +namespace JS { + +// Specialize WeakCache for GCHashMap to provide a barriered map that does not +// need to be swept immediately. +template +class WeakCache< + GCHashMap> + final : protected detail::WeakCacheBase { + using Map = GCHashMap; + using Self = WeakCache; + + Map map; + JSTracer* barrierTracer = nullptr; + + public: + template + explicit WeakCache(Zone* zone, Args&&... args) + : WeakCacheBase(zone), map(std::forward(args)...) {} + template + explicit WeakCache(JSRuntime* rt, Args&&... args) + : WeakCacheBase(rt), map(std::forward(args)...) {} + ~WeakCache() { MOZ_ASSERT(!barrierTracer); } + + bool empty() override { return map.empty(); } + + size_t traceWeak(JSTracer* trc, NeedsLock needsLock) override { + size_t steps = map.count(); + + // Create an Enum and sweep the table entries. + mozilla::Maybe e; + e.emplace(map); + map.traceWeakEntries(trc, e.ref()); + + // Potentially take a lock while the Enum's destructor is called as this can + // rehash/resize the table and access the store buffer. + mozilla::Maybe lock; + if (needsLock) { + lock.emplace(trc->runtime()); + } + e.reset(); + + return steps; + } + + bool setIncrementalBarrierTracer(JSTracer* trc) override { + MOZ_ASSERT(bool(barrierTracer) != bool(trc)); + barrierTracer = trc; + return true; + } + + bool needsIncrementalBarrier() const override { return barrierTracer; } + + private: + using Entry = typename Map::Entry; + + static bool entryNeedsSweep(JSTracer* barrierTracer, const Entry& entry) { + return MapEntryGCPolicy::needsSweep(barrierTracer, &entry.key(), + &entry.value()); + } + + public: + using Lookup = typename Map::Lookup; + using Ptr = typename Map::Ptr; + using AddPtr = typename Map::AddPtr; + + // Iterator over the whole collection. + struct Range { + explicit Range(Self& self) : cache(self), range(self.map.all()) { + settle(); + } + Range() = default; + + bool empty() const { return range.empty(); } + const Entry& front() const { return range.front(); } + + void popFront() { + range.popFront(); + settle(); + } + + private: + Self& cache; + typename Map::Range range; + + void settle() { + if (JSTracer* trc = cache.barrierTracer) { + while (!empty() && entryNeedsSweep(trc, front())) { + popFront(); + } + } + } + }; + + struct Enum : public Map::Enum { + explicit Enum(Self& cache) : Map::Enum(cache.map) { + // This operation is not allowed while barriers are in place as we + // may also need to enumerate the set for sweeping. + MOZ_ASSERT(!cache.barrierTracer); + } + }; + + Ptr lookup(const Lookup& l) const { + Ptr ptr = map.lookup(l); + if (barrierTracer && ptr && entryNeedsSweep(barrierTracer, *ptr)) { + const_cast(map).remove(ptr); + return Ptr(); + } + return ptr; + } + + AddPtr lookupForAdd(const Lookup& l) { + AddPtr ptr = map.lookupForAdd(l); + if (barrierTracer && ptr && entryNeedsSweep(barrierTracer, *ptr)) { + const_cast(map).remove(ptr); + return map.lookupForAdd(l); + } + return ptr; + } + + Range all() const { return Range(*const_cast(this)); } + + bool empty() const { + // This operation is not currently allowed while barriers are in place + // as it would require iterating the map and the caller expects a + // constant time operation. + MOZ_ASSERT(!barrierTracer); + return map.empty(); + } + + uint32_t count() const { + // This operation is not currently allowed while barriers are in place + // as it would require iterating the set and the caller expects a + // constant time operation. + MOZ_ASSERT(!barrierTracer); + return map.count(); + } + + size_t capacity() const { return map.capacity(); } + + bool has(const Lookup& l) const { return lookup(l).found(); } + + size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const { + return map.sizeOfExcludingThis(mallocSizeOf); + } + size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const { + return mallocSizeOf(this) + map.shallowSizeOfExcludingThis(mallocSizeOf); + } + + void clear() { + // This operation is not currently allowed while barriers are in place + // since it doesn't make sense to clear a cache while it is being swept. + MOZ_ASSERT(!barrierTracer); + map.clear(); + } + + void clearAndCompact() { + // This operation is not currently allowed while barriers are in place + // since it doesn't make sense to clear a cache while it is being swept. + MOZ_ASSERT(!barrierTracer); + map.clearAndCompact(); + } + + void remove(Ptr p) { + // This currently supports removing entries during incremental + // sweeping. If we allow these tables to be swept incrementally this may + // no longer be possible. + map.remove(p); + } + + void remove(const Lookup& l) { + Ptr p = lookup(l); + if (p) { + remove(p); + } + } + + template + bool add(AddPtr& p, KeyInput&& k, ValueInput&& v) { + return map.add(p, std::forward(k), std::forward(v)); + } + + template + bool relookupOrAdd(AddPtr& p, KeyInput&& k, ValueInput&& v) { + return map.relookupOrAdd(p, std::forward(k), + std::forward(v)); + } + + template + bool put(KeyInput&& k, ValueInput&& v) { + return map.put(std::forward(k), std::forward(v)); + } + + template + bool putNew(KeyInput&& k, ValueInput&& v) { + return map.putNew(std::forward(k), std::forward(v)); + } +} JS_HAZ_NON_GC_POINTER; + +// Specialize WeakCache for GCHashSet to provide a barriered set that does not +// need to be swept immediately. +template +class WeakCache> final + : protected detail::WeakCacheBase { + using Set = GCHashSet; + using Self = WeakCache; + + Set set; + JSTracer* barrierTracer = nullptr; + + public: + using Entry = typename Set::Entry; + + template + explicit WeakCache(Zone* zone, Args&&... args) + : WeakCacheBase(zone), set(std::forward(args)...) {} + template + explicit WeakCache(JSRuntime* rt, Args&&... args) + : WeakCacheBase(rt), set(std::forward(args)...) {} + + size_t traceWeak(JSTracer* trc, NeedsLock needsLock) override { + size_t steps = set.count(); + + // Create an Enum and sweep the table entries. It's not necessary to take + // the store buffer lock yet. + mozilla::Maybe e; + e.emplace(set); + set.traceWeakEntries(trc, e.ref()); + + // Destroy the Enum, potentially rehashing or resizing the table. Since this + // can access the store buffer, we need to take a lock for this if we're + // called off main thread. + mozilla::Maybe lock; + if (needsLock) { + lock.emplace(trc->runtime()); + } + e.reset(); + + return steps; + } + + bool empty() override { return set.empty(); } + + bool setIncrementalBarrierTracer(JSTracer* trc) override { + MOZ_ASSERT(bool(barrierTracer) != bool(trc)); + barrierTracer = trc; + return true; + } + + bool needsIncrementalBarrier() const override { return barrierTracer; } + + private: + static bool entryNeedsSweep(JSTracer* barrierTracer, const Entry& prior) { + Entry entry(prior); + bool needsSweep = !GCPolicy::traceWeak(barrierTracer, &entry); + MOZ_ASSERT_IF(!needsSweep, prior == entry); // We shouldn't update here. + return needsSweep; + } + + public: + using Lookup = typename Set::Lookup; + using Ptr = typename Set::Ptr; + using AddPtr = typename Set::AddPtr; + + // Iterator over the whole collection. + struct Range { + explicit Range(Self& self) : cache(self), range(self.set.all()) { + settle(); + } + Range() = default; + + bool empty() const { return range.empty(); } + const Entry& front() const { return range.front(); } + + void popFront() { + range.popFront(); + settle(); + } + + private: + Self& cache; + typename Set::Range range; + + void settle() { + if (JSTracer* trc = cache.barrierTracer) { + while (!empty() && entryNeedsSweep(trc, front())) { + popFront(); + } + } + } + }; + + struct Enum : public Set::Enum { + explicit Enum(Self& cache) : Set::Enum(cache.set) { + // This operation is not allowed while barriers are in place as we + // may also need to enumerate the set for sweeping. + MOZ_ASSERT(!cache.barrierTracer); + } + }; + + Ptr lookup(const Lookup& l) const { + Ptr ptr = set.lookup(l); + if (barrierTracer && ptr && entryNeedsSweep(barrierTracer, *ptr)) { + const_cast(set).remove(ptr); + return Ptr(); + } + return ptr; + } + + AddPtr lookupForAdd(const Lookup& l) { + AddPtr ptr = set.lookupForAdd(l); + if (barrierTracer && ptr && entryNeedsSweep(barrierTracer, *ptr)) { + const_cast(set).remove(ptr); + return set.lookupForAdd(l); + } + return ptr; + } + + Range all() const { return Range(*const_cast(this)); } + + bool empty() const { + // This operation is not currently allowed while barriers are in place + // as it would require iterating the set and the caller expects a + // constant time operation. + MOZ_ASSERT(!barrierTracer); + return set.empty(); + } + + uint32_t count() const { + // This operation is not currently allowed while barriers are in place + // as it would require iterating the set and the caller expects a + // constant time operation. + MOZ_ASSERT(!barrierTracer); + return set.count(); + } + + size_t capacity() const { return set.capacity(); } + + bool has(const Lookup& l) const { return lookup(l).found(); } + + size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const { + return set.shallowSizeOfExcludingThis(mallocSizeOf); + } + size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const { + return mallocSizeOf(this) + set.shallowSizeOfExcludingThis(mallocSizeOf); + } + + void clear() { + // This operation is not currently allowed while barriers are in place + // since it doesn't make sense to clear a cache while it is being swept. + MOZ_ASSERT(!barrierTracer); + set.clear(); + } + + void clearAndCompact() { + // This operation is not currently allowed while barriers are in place + // since it doesn't make sense to clear a cache while it is being swept. + MOZ_ASSERT(!barrierTracer); + set.clearAndCompact(); + } + + void remove(Ptr p) { + // This currently supports removing entries during incremental + // sweeping. If we allow these tables to be swept incrementally this may + // no longer be possible. + set.remove(p); + } + + void remove(const Lookup& l) { + Ptr p = lookup(l); + if (p) { + remove(p); + } + } + + template + void replaceKey(Ptr p, const Lookup& l, TInput&& newValue) { + set.replaceKey(p, l, std::forward(newValue)); + } + + template + bool add(AddPtr& p, TInput&& t) { + return set.add(p, std::forward(t)); + } + + template + bool relookupOrAdd(AddPtr& p, const Lookup& l, TInput&& t) { + return set.relookupOrAdd(p, l, std::forward(t)); + } + + template + bool put(TInput&& t) { + return set.put(std::forward(t)); + } + + template + bool putNew(TInput&& t) { + return set.putNew(std::forward(t)); + } + + template + bool putNew(const Lookup& l, TInput&& t) { + return set.putNew(l, std::forward(t)); + } +} JS_HAZ_NON_GC_POINTER; + +} // namespace JS + #endif /* GCHashTable_h */ diff --git a/js/public/SweepingAPI.h b/js/public/SweepingAPI.h new file mode 100644 index 000000000000..4dc440b40bfe --- /dev/null +++ b/js/public/SweepingAPI.h @@ -0,0 +1,125 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * vim: set ts=8 sts=2 et sw=2 tw=80: + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef js_SweepingAPI_h +#define js_SweepingAPI_h + +#include "mozilla/LinkedList.h" +#include "mozilla/Maybe.h" + +#include "jstypes.h" + +#include "js/GCAnnotations.h" +#include "js/GCPolicyAPI.h" +#include "js/RootingAPI.h" + +namespace js { +namespace gc { + +JS_PUBLIC_API void LockStoreBuffer(JSRuntime* runtime); +JS_PUBLIC_API void UnlockStoreBuffer(JSRuntime* runtim); + +class AutoLockStoreBuffer { + JSRuntime* runtime; + + public: + explicit AutoLockStoreBuffer(JSRuntime* runtime) : runtime(runtime) { + LockStoreBuffer(runtime); + } + ~AutoLockStoreBuffer() { UnlockStoreBuffer(runtime); } +}; + +} // namespace gc +} // namespace js + +namespace JS { +namespace detail { +class WeakCacheBase; +} // namespace detail + +namespace shadow { +JS_PUBLIC_API void RegisterWeakCache(JS::Zone* zone, + JS::detail::WeakCacheBase* cachep); +JS_PUBLIC_API void RegisterWeakCache(JSRuntime* rt, + JS::detail::WeakCacheBase* cachep); +} // namespace shadow + +namespace detail { + +class WeakCacheBase : public mozilla::LinkedListElement { + WeakCacheBase() = delete; + explicit WeakCacheBase(const WeakCacheBase&) = delete; + + public: + enum NeedsLock : bool { LockStoreBuffer = true, DontLockStoreBuffer = false }; + + explicit WeakCacheBase(JS::Zone* zone) { + shadow::RegisterWeakCache(zone, this); + } + explicit WeakCacheBase(JSRuntime* rt) { shadow::RegisterWeakCache(rt, this); } + WeakCacheBase(WeakCacheBase&& other) = default; + virtual ~WeakCacheBase() = default; + + virtual size_t traceWeak(JSTracer* trc, NeedsLock needLock) = 0; + + // Sweeping will be skipped if the cache is empty already. + virtual bool empty() = 0; + + // Enable/disable read barrier during incremental sweeping and set the tracer + // to use. + virtual bool setIncrementalBarrierTracer(JSTracer* trc) { + // Derived classes do not support incremental barriers by default. + return false; + } + virtual bool needsIncrementalBarrier() const { + // Derived classes do not support incremental barriers by default. + return false; + } +}; + +} // namespace detail + +// A WeakCache stores the given Sweepable container and links itself into a +// list of such caches that are swept during each GC. A WeakCache can be +// specific to a zone, or across a whole runtime, depending on which +// constructor is used. +template +class WeakCache : protected detail::WeakCacheBase, + public js::MutableWrappedPtrOperations> { + T cache; + + public: + using Type = T; + + template + explicit WeakCache(Zone* zone, Args&&... args) + : WeakCacheBase(zone), cache(std::forward(args)...) {} + template + explicit WeakCache(JSRuntime* rt, Args&&... args) + : WeakCacheBase(rt), cache(std::forward(args)...) {} + + const T& get() const { return cache; } + T& get() { return cache; } + + size_t traceWeak(JSTracer* trc, NeedsLock needsLock) override { + // Take the store buffer lock in case sweeping triggers any generational + // post barriers. This is not always required and WeakCache specializations + // may delay or skip taking the lock as appropriate. + mozilla::Maybe lock; + if (needsLock) { + lock.emplace(trc->runtime()); + } + + GCPolicy::traceWeak(trc, &cache); + return 0; + } + + bool empty() override { return cache.empty(); } +} JS_HAZ_NON_GC_POINTER; + +} // namespace JS + +#endif // js_SweepingAPI_h diff --git a/js/src/gc/GCRuntime.h b/js/src/gc/GCRuntime.h index 2a7bf2b5bf5b..11a14f2dafc3 100644 --- a/js/src/gc/GCRuntime.h +++ b/js/src/gc/GCRuntime.h @@ -23,7 +23,6 @@ #include "gc/Scheduling.h" #include "gc/Statistics.h" #include "gc/StoreBuffer.h" -#include "gc/SweepingAPI.h" #include "js/friend/PerformanceHint.h" #include "js/GCAnnotations.h" #include "js/UniquePtr.h" diff --git a/js/src/gc/SweepingAPI.h b/js/src/gc/SweepingAPI.h deleted file mode 100644 index 4f2f1d813fd3..000000000000 --- a/js/src/gc/SweepingAPI.h +++ /dev/null @@ -1,530 +0,0 @@ -/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * vim: set ts=8 sts=2 et sw=2 tw=80: - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#ifndef js_SweepingAPI_h -#define js_SweepingAPI_h - -#include "mozilla/LinkedList.h" -#include "mozilla/Maybe.h" - -#include "jstypes.h" - -#include "js/GCAnnotations.h" -#include "js/GCHashTable.h" -#include "js/GCPolicyAPI.h" -#include "js/RootingAPI.h" - -namespace js { -namespace gc { - -JS_PUBLIC_API void LockStoreBuffer(JSRuntime* runtime); -JS_PUBLIC_API void UnlockStoreBuffer(JSRuntime* runtim); - -class AutoLockStoreBuffer { - JSRuntime* runtime; - - public: - explicit AutoLockStoreBuffer(JSRuntime* runtime) : runtime(runtime) { - LockStoreBuffer(runtime); - } - ~AutoLockStoreBuffer() { UnlockStoreBuffer(runtime); } -}; - -} // namespace gc -} // namespace js - -namespace JS { -namespace detail { -class WeakCacheBase; -} // namespace detail - -namespace shadow { -JS_PUBLIC_API void RegisterWeakCache(JS::Zone* zone, - JS::detail::WeakCacheBase* cachep); -JS_PUBLIC_API void RegisterWeakCache(JSRuntime* rt, - JS::detail::WeakCacheBase* cachep); -} // namespace shadow - -namespace detail { - -class WeakCacheBase : public mozilla::LinkedListElement { - WeakCacheBase() = delete; - explicit WeakCacheBase(const WeakCacheBase&) = delete; - - public: - enum NeedsLock : bool { LockStoreBuffer = true, DontLockStoreBuffer = false }; - - explicit WeakCacheBase(JS::Zone* zone) { - shadow::RegisterWeakCache(zone, this); - } - explicit WeakCacheBase(JSRuntime* rt) { shadow::RegisterWeakCache(rt, this); } - WeakCacheBase(WeakCacheBase&& other) = default; - virtual ~WeakCacheBase() = default; - - virtual size_t traceWeak(JSTracer* trc, NeedsLock needLock) = 0; - - // Sweeping will be skipped if the cache is empty already. - virtual bool empty() = 0; - - // Enable/disable read barrier during incremental sweeping and set the tracer - // to use. - virtual bool setIncrementalBarrierTracer(JSTracer* trc) { - // Derived classes do not support incremental barriers by default. - return false; - } - virtual bool needsIncrementalBarrier() const { - // Derived classes do not support incremental barriers by default. - return false; - } -}; - -} // namespace detail - -// A WeakCache stores the given Sweepable container and links itself into a -// list of such caches that are swept during each GC. A WeakCache can be -// specific to a zone, or across a whole runtime, depending on which -// constructor is used. -template -class WeakCache : protected detail::WeakCacheBase, - public js::MutableWrappedPtrOperations> { - T cache; - - public: - using Type = T; - - template - explicit WeakCache(Zone* zone, Args&&... args) - : WeakCacheBase(zone), cache(std::forward(args)...) {} - template - explicit WeakCache(JSRuntime* rt, Args&&... args) - : WeakCacheBase(rt), cache(std::forward(args)...) {} - - const T& get() const { return cache; } - T& get() { return cache; } - - size_t traceWeak(JSTracer* trc, NeedsLock needsLock) override { - // Take the store buffer lock in case sweeping triggers any generational - // post barriers. This is not always required and WeakCache specializations - // may delay or skip taking the lock as appropriate. - mozilla::Maybe lock; - if (needsLock) { - lock.emplace(trc->runtime()); - } - - GCPolicy::traceWeak(trc, &cache); - return 0; - } - - bool empty() override { return cache.empty(); } -} JS_HAZ_NON_GC_POINTER; - -// Specialize WeakCache for GCHashMap to provide a barriered map that does not -// need to be swept immediately. -template -class WeakCache< - GCHashMap> - final : protected detail::WeakCacheBase { - using Map = GCHashMap; - using Self = WeakCache; - - Map map; - JSTracer* barrierTracer = nullptr; - - public: - template - explicit WeakCache(Zone* zone, Args&&... args) - : WeakCacheBase(zone), map(std::forward(args)...) {} - template - explicit WeakCache(JSRuntime* rt, Args&&... args) - : WeakCacheBase(rt), map(std::forward(args)...) {} - ~WeakCache() { MOZ_ASSERT(!barrierTracer); } - - bool empty() override { return map.empty(); } - - size_t traceWeak(JSTracer* trc, NeedsLock needsLock) override { - size_t steps = map.count(); - - // Create an Enum and sweep the table entries. - mozilla::Maybe e; - e.emplace(map); - map.traceWeakEntries(trc, e.ref()); - - // Potentially take a lock while the Enum's destructor is called as this can - // rehash/resize the table and access the store buffer. - mozilla::Maybe lock; - if (needsLock) { - lock.emplace(trc->runtime()); - } - e.reset(); - - return steps; - } - - bool setIncrementalBarrierTracer(JSTracer* trc) override { - MOZ_ASSERT(bool(barrierTracer) != bool(trc)); - barrierTracer = trc; - return true; - } - - bool needsIncrementalBarrier() const override { return barrierTracer; } - - private: - using Entry = typename Map::Entry; - - static bool entryNeedsSweep(JSTracer* barrierTracer, const Entry& entry) { - return MapEntryGCPolicy::needsSweep(barrierTracer, &entry.key(), - &entry.value()); - } - - public: - using Lookup = typename Map::Lookup; - using Ptr = typename Map::Ptr; - using AddPtr = typename Map::AddPtr; - - // Iterator over the whole collection. - struct Range { - explicit Range(Self& self) : cache(self), range(self.map.all()) { - settle(); - } - Range() = default; - - bool empty() const { return range.empty(); } - const Entry& front() const { return range.front(); } - - void popFront() { - range.popFront(); - settle(); - } - - private: - Self& cache; - typename Map::Range range; - - void settle() { - if (JSTracer* trc = cache.barrierTracer) { - while (!empty() && entryNeedsSweep(trc, front())) { - popFront(); - } - } - } - }; - - struct Enum : public Map::Enum { - explicit Enum(Self& cache) : Map::Enum(cache.map) { - // This operation is not allowed while barriers are in place as we - // may also need to enumerate the set for sweeping. - MOZ_ASSERT(!cache.barrierTracer); - } - }; - - Ptr lookup(const Lookup& l) const { - Ptr ptr = map.lookup(l); - if (barrierTracer && ptr && entryNeedsSweep(barrierTracer, *ptr)) { - const_cast(map).remove(ptr); - return Ptr(); - } - return ptr; - } - - AddPtr lookupForAdd(const Lookup& l) { - AddPtr ptr = map.lookupForAdd(l); - if (barrierTracer && ptr && entryNeedsSweep(barrierTracer, *ptr)) { - const_cast(map).remove(ptr); - return map.lookupForAdd(l); - } - return ptr; - } - - Range all() const { return Range(*const_cast(this)); } - - bool empty() const { - // This operation is not currently allowed while barriers are in place - // as it would require iterating the map and the caller expects a - // constant time operation. - MOZ_ASSERT(!barrierTracer); - return map.empty(); - } - - uint32_t count() const { - // This operation is not currently allowed while barriers are in place - // as it would require iterating the set and the caller expects a - // constant time operation. - MOZ_ASSERT(!barrierTracer); - return map.count(); - } - - size_t capacity() const { return map.capacity(); } - - bool has(const Lookup& l) const { return lookup(l).found(); } - - size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const { - return map.sizeOfExcludingThis(mallocSizeOf); - } - size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const { - return mallocSizeOf(this) + map.shallowSizeOfExcludingThis(mallocSizeOf); - } - - void clear() { - // This operation is not currently allowed while barriers are in place - // since it doesn't make sense to clear a cache while it is being swept. - MOZ_ASSERT(!barrierTracer); - map.clear(); - } - - void clearAndCompact() { - // This operation is not currently allowed while barriers are in place - // since it doesn't make sense to clear a cache while it is being swept. - MOZ_ASSERT(!barrierTracer); - map.clearAndCompact(); - } - - void remove(Ptr p) { - // This currently supports removing entries during incremental - // sweeping. If we allow these tables to be swept incrementally this may - // no longer be possible. - map.remove(p); - } - - void remove(const Lookup& l) { - Ptr p = lookup(l); - if (p) { - remove(p); - } - } - - template - bool add(AddPtr& p, KeyInput&& k, ValueInput&& v) { - return map.add(p, std::forward(k), std::forward(v)); - } - - template - bool relookupOrAdd(AddPtr& p, KeyInput&& k, ValueInput&& v) { - return map.relookupOrAdd(p, std::forward(k), - std::forward(v)); - } - - template - bool put(KeyInput&& k, ValueInput&& v) { - return map.put(std::forward(k), std::forward(v)); - } - - template - bool putNew(KeyInput&& k, ValueInput&& v) { - return map.putNew(std::forward(k), std::forward(v)); - } -} JS_HAZ_NON_GC_POINTER; - -// Specialize WeakCache for GCHashSet to provide a barriered set that does not -// need to be swept immediately. -template -class WeakCache> final - : protected detail::WeakCacheBase { - using Set = GCHashSet; - using Self = WeakCache; - - Set set; - JSTracer* barrierTracer = nullptr; - - public: - using Entry = typename Set::Entry; - - template - explicit WeakCache(Zone* zone, Args&&... args) - : WeakCacheBase(zone), set(std::forward(args)...) {} - template - explicit WeakCache(JSRuntime* rt, Args&&... args) - : WeakCacheBase(rt), set(std::forward(args)...) {} - - size_t traceWeak(JSTracer* trc, NeedsLock needsLock) override { - size_t steps = set.count(); - - // Create an Enum and sweep the table entries. It's not necessary to take - // the store buffer lock yet. - mozilla::Maybe e; - e.emplace(set); - set.traceWeakEntries(trc, e.ref()); - - // Destroy the Enum, potentially rehashing or resizing the table. Since this - // can access the store buffer, we need to take a lock for this if we're - // called off main thread. - mozilla::Maybe lock; - if (needsLock) { - lock.emplace(trc->runtime()); - } - e.reset(); - - return steps; - } - - bool empty() override { return set.empty(); } - - bool setIncrementalBarrierTracer(JSTracer* trc) override { - MOZ_ASSERT(bool(barrierTracer) != bool(trc)); - barrierTracer = trc; - return true; - } - - bool needsIncrementalBarrier() const override { return barrierTracer; } - - private: - static bool entryNeedsSweep(JSTracer* barrierTracer, const Entry& prior) { - Entry entry(prior); - bool needsSweep = !GCPolicy::traceWeak(barrierTracer, &entry); - MOZ_ASSERT_IF(!needsSweep, prior == entry); // We shouldn't update here. - return needsSweep; - } - - public: - using Lookup = typename Set::Lookup; - using Ptr = typename Set::Ptr; - using AddPtr = typename Set::AddPtr; - - // Iterator over the whole collection. - struct Range { - explicit Range(Self& self) : cache(self), range(self.set.all()) { - settle(); - } - Range() = default; - - bool empty() const { return range.empty(); } - const Entry& front() const { return range.front(); } - - void popFront() { - range.popFront(); - settle(); - } - - private: - Self& cache; - typename Set::Range range; - - void settle() { - if (JSTracer* trc = cache.barrierTracer) { - while (!empty() && entryNeedsSweep(trc, front())) { - popFront(); - } - } - } - }; - - struct Enum : public Set::Enum { - explicit Enum(Self& cache) : Set::Enum(cache.set) { - // This operation is not allowed while barriers are in place as we - // may also need to enumerate the set for sweeping. - MOZ_ASSERT(!cache.barrierTracer); - } - }; - - Ptr lookup(const Lookup& l) const { - Ptr ptr = set.lookup(l); - if (barrierTracer && ptr && entryNeedsSweep(barrierTracer, *ptr)) { - const_cast(set).remove(ptr); - return Ptr(); - } - return ptr; - } - - AddPtr lookupForAdd(const Lookup& l) { - AddPtr ptr = set.lookupForAdd(l); - if (barrierTracer && ptr && entryNeedsSweep(barrierTracer, *ptr)) { - const_cast(set).remove(ptr); - return set.lookupForAdd(l); - } - return ptr; - } - - Range all() const { return Range(*const_cast(this)); } - - bool empty() const { - // This operation is not currently allowed while barriers are in place - // as it would require iterating the set and the caller expects a - // constant time operation. - MOZ_ASSERT(!barrierTracer); - return set.empty(); - } - - uint32_t count() const { - // This operation is not currently allowed while barriers are in place - // as it would require iterating the set and the caller expects a - // constant time operation. - MOZ_ASSERT(!barrierTracer); - return set.count(); - } - - size_t capacity() const { return set.capacity(); } - - bool has(const Lookup& l) const { return lookup(l).found(); } - - size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const { - return set.shallowSizeOfExcludingThis(mallocSizeOf); - } - size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const { - return mallocSizeOf(this) + set.shallowSizeOfExcludingThis(mallocSizeOf); - } - - void clear() { - // This operation is not currently allowed while barriers are in place - // since it doesn't make sense to clear a cache while it is being swept. - MOZ_ASSERT(!barrierTracer); - set.clear(); - } - - void clearAndCompact() { - // This operation is not currently allowed while barriers are in place - // since it doesn't make sense to clear a cache while it is being swept. - MOZ_ASSERT(!barrierTracer); - set.clearAndCompact(); - } - - void remove(Ptr p) { - // This currently supports removing entries during incremental - // sweeping. If we allow these tables to be swept incrementally this may - // no longer be possible. - set.remove(p); - } - - void remove(const Lookup& l) { - Ptr p = lookup(l); - if (p) { - remove(p); - } - } - - template - void replaceKey(Ptr p, const Lookup& l, TInput&& newValue) { - set.replaceKey(p, l, std::forward(newValue)); - } - - template - bool add(AddPtr& p, TInput&& t) { - return set.add(p, std::forward(t)); - } - - template - bool relookupOrAdd(AddPtr& p, const Lookup& l, TInput&& t) { - return set.relookupOrAdd(p, l, std::forward(t)); - } - - template - bool put(TInput&& t) { - return set.put(std::forward(t)); - } - - template - bool putNew(TInput&& t) { - return set.putNew(std::forward(t)); - } - - template - bool putNew(const Lookup& l, TInput&& t) { - return set.putNew(l, std::forward(t)); - } -} JS_HAZ_NON_GC_POINTER; - -} // namespace JS - -#endif // js_SweepingAPI_h diff --git a/js/src/jit/CacheIRCompiler.cpp b/js/src/jit/CacheIRCompiler.cpp index ba3c8ed09ec1..402365867c8e 100644 --- a/js/src/jit/CacheIRCompiler.cpp +++ b/js/src/jit/CacheIRCompiler.cpp @@ -20,7 +20,6 @@ #include "builtin/DataViewObject.h" #include "builtin/Object.h" #include "gc/GCEnum.h" -#include "gc/SweepingAPI.h" // js::gc::AutoLockStoreBuffer #include "jit/BaselineCacheIRCompiler.h" #include "jit/CacheIRGenerator.h" #include "jit/IonCacheIRCompiler.h" @@ -33,6 +32,7 @@ #include "js/friend/DOMProxy.h" // JS::ExpandoAndGeneration #include "js/friend/XrayJitInfo.h" // js::jit::GetXrayJitInfo #include "js/ScalarType.h" // js::Scalar::Type +#include "js/SweepingAPI.h" #include "proxy/DOMProxy.h" #include "proxy/Proxy.h" #include "proxy/ScriptedProxyHandler.h" diff --git a/js/src/jsapi-tests/testGCWeakCache.cpp b/js/src/jsapi-tests/testGCWeakCache.cpp index 831f5c0e4580..b90c216b5ec4 100644 --- a/js/src/jsapi-tests/testGCWeakCache.cpp +++ b/js/src/jsapi-tests/testGCWeakCache.cpp @@ -6,10 +6,10 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "gc/Policy.h" -#include "gc/SweepingAPI.h" #include "gc/Zone.h" #include "js/GCHashTable.h" #include "js/RootingAPI.h" +#include "js/SweepingAPI.h" #include "jsapi-tests/tests.h" diff --git a/js/src/moz.build b/js/src/moz.build index 90dca3e7f7a9..b2befdb35004 100644 --- a/js/src/moz.build +++ b/js/src/moz.build @@ -204,6 +204,7 @@ EXPORTS.js += [ "../public/StreamConsumer.h", "../public/String.h", "../public/StructuredClone.h", + "../public/SweepingAPI.h", "../public/Symbol.h", "../public/TelemetryTimers.h", "../public/TraceKind.h", diff --git a/js/src/shell/js.cpp b/js/src/shell/js.cpp index 9b849a9b70d9..f8e26bdaaaba 100644 --- a/js/src/shell/js.cpp +++ b/js/src/shell/js.cpp @@ -163,6 +163,7 @@ #include "js/Stack.h" #include "js/StreamConsumer.h" #include "js/StructuredClone.h" +#include "js/SweepingAPI.h" #include "js/Transcoding.h" // JS::TranscodeBuffer, JS::TranscodeRange, JS::IsTranscodeFailureResult #include "js/Warnings.h" // JS::SetWarningReporter #include "js/WasmModule.h" // JS::WasmModule diff --git a/js/src/vm/InvalidatingFuse.h b/js/src/vm/InvalidatingFuse.h index 918f3cf111a0..103b026500f5 100644 --- a/js/src/vm/InvalidatingFuse.h +++ b/js/src/vm/InvalidatingFuse.h @@ -8,7 +8,7 @@ #define vm_InvalidatingFuse_h #include "gc/Barrier.h" -#include "gc/SweepingAPI.h" +#include "js/SweepingAPI.h" #include "vm/GuardFuse.h" class JSScript; diff --git a/js/src/vm/RegExpShared.h b/js/src/vm/RegExpShared.h index bff9c460a046..9cb6cc9e216f 100644 --- a/js/src/vm/RegExpShared.h +++ b/js/src/vm/RegExpShared.h @@ -18,7 +18,6 @@ #include "gc/Barrier.h" #include "gc/Policy.h" -#include "gc/SweepingAPI.h" #include "gc/ZoneAllocator.h" #include "irregexp/RegExpTypes.h" #include "jit/JitCode.h" diff --git a/js/src/vm/ShapeZone.h b/js/src/vm/ShapeZone.h index 04ccad855b90..43421e7d52ee 100644 --- a/js/src/vm/ShapeZone.h +++ b/js/src/vm/ShapeZone.h @@ -10,7 +10,6 @@ #include "mozilla/MemoryReporting.h" #include "gc/Barrier.h" -#include "gc/SweepingAPI.h" #include "js/GCHashTable.h" #include "vm/PropertyKey.h" #include "vm/PropMap.h" diff --git a/js/src/wasm/WasmJS.h b/js/src/wasm/WasmJS.h index a7204996708b..668e282a4656 100644 --- a/js/src/wasm/WasmJS.h +++ b/js/src/wasm/WasmJS.h @@ -25,7 +25,6 @@ #include // int32_t, int64_t, uint32_t #include "gc/Barrier.h" // HeapPtr -#include "gc/SweepingAPI.h" // JS::WeakCache #include "gc/ZoneAllocator.h" // ZoneAllocPolicy #include "js/AllocPolicy.h" // SystemAllocPolicy #include "js/Class.h" // JSClassOps, ClassSpec @@ -33,6 +32,7 @@ #include "js/GCVector.h" // GCVector #include "js/PropertySpec.h" // JSPropertySpec, JSFunctionSpec #include "js/RootingAPI.h" // StableCellHasher +#include "js/SweepingAPI.h" // JS::WeakCache #include "js/TypeDecls.h" // HandleValue, HandleObject, MutableHandleObject, MutableHandleFunction #include "js/Vector.h" // JS::Vector #include "js/WasmFeatures.h"