Bug 1237445 - Implement GCRekeyableHashMap, r=terrence
authorSteve Fink <sfink@mozilla.com>
Wed, 06 Jan 2016 14:41:12 -0800
changeset 278992 9d0643d45a74538f6b703d665fa03917d1605bbb
parent 278991 39220ff4899b5614e2da063687efd6c8147a0e97
child 278993 e003fb68a47c195dedc39a0ef4aec91f4eadd180
push id69955
push usersfink@mozilla.com
push dateThu, 07 Jan 2016 20:36:58 +0000
treeherdermozilla-inbound@5d93baf4ff9c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersterrence
bugs1237445
milestone46.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1237445 - Implement GCRekeyableHashMap, r=terrence
js/public/GCHashTable.h
--- a/js/public/GCHashTable.h
+++ b/js/public/GCHashTable.h
@@ -87,16 +87,50 @@ class GCHashMap : public HashMap<Key, Va
     }
 
   private:
     // GCHashMap is not copyable or assignable
     GCHashMap(const GCHashMap& hm) = delete;
     GCHashMap& operator=(const GCHashMap& hm) = delete;
 };
 
+// HashMap that supports rekeying.
+template <typename Key,
+          typename Value,
+          typename HashPolicy = DefaultHasher<Key>,
+          typename AllocPolicy = TempAllocPolicy,
+          typename GCPolicy = DefaultMapGCPolicy<Key, Value>>
+class GCRekeyableHashMap : public GCHashMap<Key, Value, HashPolicy, AllocPolicy, GCPolicy>
+{
+    using Base = GCHashMap<Key, Value, HashPolicy, AllocPolicy>;
+
+  public:
+    explicit GCRekeyableHashMap(AllocPolicy a = AllocPolicy()) : Base(a)  {}
+
+    void sweep() {
+        if (!this->initialized())
+            return;
+
+        for (typename Base::Enum e(*this); !e.empty(); e.popFront()) {
+            Key key(e.front().key());
+            if (GCPolicy::needsSweep(&key, &e.front().value()))
+                e.removeFront();
+            else if (!HashPolicy::match(key, e.front().key()))
+                e.rekeyFront(key);
+        }
+    }
+
+    // GCRekeyableHashMap is movable
+    GCRekeyableHashMap(GCRekeyableHashMap&& rhs) : Base(mozilla::Forward<GCRekeyableHashMap>(rhs)) {}
+    void operator=(GCRekeyableHashMap&& rhs) {
+        MOZ_ASSERT(this != &rhs, "self-move assignment is prohibited");
+        Base::operator=(mozilla::Forward<GCRekeyableHashMap>(rhs));
+    }
+};
+
 template <typename Outer, typename... Args>
 class GCHashMapOperations
 {
     using Map = GCHashMap<Args...>;
     using Lookup = typename Map::Lookup;
     using Ptr = typename Map::Ptr;
     using AddPtr = typename Map::AddPtr;
     using Range = typename Map::Range;