Skip to content

Commit

Permalink
Bug 1477626 - Move js::Hash{Set,Map} into MFBT. r=Waldo
Browse files Browse the repository at this point in the history
The main change is that the patch copies js/public/HashTable.h to
mfbt/HashTable.h, and then changes it as follows.

- Changes `js` namespaces to `mozilla` (and removes some now-unnecessary
  `mozilla::` qualifiers).

- Changes the default AllocPolicy from the SpiderMonkey-specific
  `TempAllocPolicy` to the generic `MallocAllocPolicy`.

- Adds `#include "AllocPolicy.h"` (like mfbt/Vector.h).

- Changes `JS_DEBUG` use to `DEBUG`.

- Minor comment updates, as necessary.

js/public/HashTable.h is now tiny, holding just a few renamings of things from
the `mozilla` namespace into the `js` namespace to minimize churn elsewhere.
(Those renamings keep `TempAllocPolicy` as the default AllocPolicy for
js::Hash{Set,Map}.)

Also, various template specializations had to be moved from the `js` namespace
to the `mozilla` namespace to avoid compile errors.

MozReview-Commit-ID: GS9Qn9YeYDA

--HG--
rename : js/public/HashTable.h => mfbt/HashTable.h
  • Loading branch information
nnethercote committed Jul 26, 2018
1 parent be01a29 commit b85493f
Show file tree
Hide file tree
Showing 14 changed files with 2,042 additions and 1,977 deletions.
1,959 changes: 12 additions & 1,947 deletions js/public/HashTable.h

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions js/public/RootingAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -766,18 +766,22 @@ struct JS_PUBLIC_API(MovableCellHasher<JS::Heap<T>>)
static void rekey(Key& k, const Key& newKey) { k.unsafeSet(newKey); }
};

} // namespace js

namespace mozilla {

template <typename T>
struct FallibleHashMethods<MovableCellHasher<T>>
struct FallibleHashMethods<js::MovableCellHasher<T>>
{
template <typename Lookup> static bool hasHash(Lookup&& l) {
return MovableCellHasher<T>::hasHash(std::forward<Lookup>(l));
return js::MovableCellHasher<T>::hasHash(std::forward<Lookup>(l));
}
template <typename Lookup> static bool ensureHash(Lookup&& l) {
return MovableCellHasher<T>::ensureHash(std::forward<Lookup>(l));
return js::MovableCellHasher<T>::ensureHash(std::forward<Lookup>(l));
}
};

} /* namespace js */
} // namespace mozilla

namespace js {

Expand Down
4 changes: 2 additions & 2 deletions js/public/UbiNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -1185,12 +1185,12 @@ void SetConstructUbiNodeForDOMObjectCallback(JSContext* cx, void (*callback)(voi
} // namespace ubi
} // namespace JS

namespace js {
namespace mozilla {

// Make ubi::Node::HashPolicy the default hash policy for ubi::Node.
template<> struct DefaultHasher<JS::ubi::Node> : JS::ubi::Node::HashPolicy { };
template<> struct DefaultHasher<JS::ubi::StackFrame> : JS::ubi::StackFrame::HashPolicy { };

} // namespace js
} // namespace mozilla

#endif // js_UbiNode_h
2 changes: 2 additions & 0 deletions js/src/ds/OrderedHashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#include "mozilla/HashFunctions.h"
#include "mozilla/Move.h"

#include "js/HashTable.h"

namespace js {

namespace detail {
Expand Down
24 changes: 16 additions & 8 deletions js/src/gc/Barrier.h
Original file line number Diff line number Diff line change
Expand Up @@ -871,10 +871,6 @@ struct GCPtrHasher
static void rekey(Key& k, const Key& newKey) { k.unsafeSet(newKey); }
};

/* Specialized hashing policy for GCPtrs. */
template <class T>
struct DefaultHasher<GCPtr<T>> : GCPtrHasher<T> {};

template <class T>
struct PreBarrieredHasher
{
Expand All @@ -886,9 +882,6 @@ struct PreBarrieredHasher
static void rekey(Key& k, const Key& newKey) { k.unsafeSet(newKey); }
};

template <class T>
struct DefaultHasher<PreBarriered<T>> : PreBarrieredHasher<T> { };

/* Useful for hashtables with a ReadBarriered as key. */
template <class T>
struct ReadBarrieredHasher
Expand All @@ -901,9 +894,24 @@ struct ReadBarrieredHasher
static void rekey(Key& k, const Key& newKey) { k.set(newKey.unbarrieredGet()); }
};

} // namespace js

namespace mozilla {

/* Specialized hashing policy for GCPtrs. */
template <class T>
struct DefaultHasher<js::GCPtr<T>> : js::GCPtrHasher<T> {};

template <class T>
struct DefaultHasher<js::PreBarriered<T>> : js::PreBarrieredHasher<T> { };

/* Specialized hashing policy for ReadBarriereds. */
template <class T>
struct DefaultHasher<ReadBarriered<T>> : ReadBarrieredHasher<T> { };
struct DefaultHasher<js::ReadBarriered<T>> : js::ReadBarrieredHasher<T> { };

} // namespace mozilla

namespace js {

class ArrayObject;
class ArrayBufferObject;
Expand Down
4 changes: 2 additions & 2 deletions js/src/jsapi-tests/testUbiNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ struct ExpectedEdge
{ }
};

namespace js {
namespace mozilla {

template <>
struct DefaultHasher<ExpectedEdge>
Expand All @@ -327,7 +327,7 @@ struct DefaultHasher<ExpectedEdge>
}
};

} // namespace js
} // namespace mozilla

BEGIN_TEST(test_ubiPostOrder)
{
Expand Down
4 changes: 2 additions & 2 deletions js/src/vm/ObjectGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ struct ObjectGroupRealm::NewEntry
}
};

namespace js {
namespace mozilla {
template <>
struct FallibleHashMethods<ObjectGroupRealm::NewEntry>
{
Expand All @@ -442,7 +442,7 @@ struct FallibleHashMethods<ObjectGroupRealm::NewEntry>
return ObjectGroupRealm::NewEntry::ensureHash(std::forward<Lookup>(l));
}
};
} // namespace js
} // namespace mozilla

class ObjectGroupRealm::NewTable : public JS::WeakCache<js::GCHashSet<NewEntry, NewEntry,
SystemAllocPolicy>>
Expand Down
14 changes: 11 additions & 3 deletions js/src/vm/SavedFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,25 @@ struct SavedFrame::HashPolicy
static void rekey(Key& key, const Key& newKey);
};

} // namespace js

namespace mozilla {

template <>
struct FallibleHashMethods<SavedFrame::HashPolicy>
struct FallibleHashMethods<js::SavedFrame::HashPolicy>
{
template <typename Lookup> static bool hasHash(Lookup&& l) {
return SavedFrame::HashPolicy::hasHash(std::forward<Lookup>(l));
return js::SavedFrame::HashPolicy::hasHash(std::forward<Lookup>(l));
}
template <typename Lookup> static bool ensureHash(Lookup&& l) {
return SavedFrame::HashPolicy::ensureHash(std::forward<Lookup>(l));
return js::SavedFrame::HashPolicy::ensureHash(std::forward<Lookup>(l));
}
};

} // namespace mozilla

namespace js {

// Assert that if the given object is not null, that it must be either a
// SavedFrame object or wrapper (Xray or CCW) around a SavedFrame object.
inline void AssertObjectIsSavedFrameOrWrapper(JSContext* cx, HandleObject stack);
Expand Down
10 changes: 9 additions & 1 deletion js/src/vm/Shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -668,18 +668,26 @@ HashId(jsid id)
return mozilla::HashGeneric(JSID_BITS(id));
}

} // namespace js

namespace mozilla {

template <>
struct DefaultHasher<jsid>
{
typedef jsid Lookup;
static HashNumber hash(jsid id) {
return HashId(id);
return js::HashId(id);
}
static bool match(jsid id1, jsid id2) {
return id1 == id2;
}
};

} // namespace mozilla

namespace js {

using BaseShapeSet = JS::WeakCache<JS::GCHashSet<ReadBarriered<UnownedBaseShape*>,
StackBaseShape,
SystemAllocPolicy>>;
Expand Down
14 changes: 11 additions & 3 deletions js/src/vm/Stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -1053,19 +1053,27 @@ FillArgumentsFromArraylike(JSContext* cx, Args& args, const Arraylike& arraylike
return true;
}

} // namespace js

namespace mozilla {

template <>
struct DefaultHasher<AbstractFramePtr> {
typedef AbstractFramePtr Lookup;
struct DefaultHasher<js::AbstractFramePtr> {
typedef js::AbstractFramePtr Lookup;

static js::HashNumber hash(const Lookup& key) {
return mozilla::HashGeneric(key.raw());
}

static bool match(const AbstractFramePtr& k, const Lookup& l) {
static bool match(const js::AbstractFramePtr& k, const Lookup& l) {
return k == l;
}
};

} // namespace mozilla

namespace js {

/*****************************************************************************/

// SavedFrame caching to minimize stack walking.
Expand Down
9 changes: 5 additions & 4 deletions js/src/vm/UbiNodeCensus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ static int compareEntries(const void* lhsVoid, const void* rhsVoid) {
}

// A hash map mapping from C strings to counts.
using CStringCountMap = HashMap<const char*, CountBasePtr, CStringHasher, SystemAllocPolicy>;
using CStringCountMap =
HashMap<const char*, CountBasePtr, mozilla::CStringHasher, SystemAllocPolicy>;

// Convert a HashMap into an object with each key one of the entries from the
// map and each value the associated count's report. For use during census
Expand Down Expand Up @@ -897,7 +898,7 @@ ByAllocationStack::report(JSContext* cx, CountBase& countBase, MutableHandleValu

#ifdef DEBUG
// Check that nothing rehashes our table while we hold pointers into it.
Generation generation = count.table.generation();
mozilla::Generation generation = count.table.generation();
#endif

// Build a vector of pointers to entries; sort by total; and then use
Expand Down Expand Up @@ -959,11 +960,11 @@ class ByFilename : public CountType {
using Lookup = UniqueCString;

static js::HashNumber hash(const Lookup& lookup) {
return CStringHasher::hash(lookup.get());
return mozilla::CStringHasher::hash(lookup.get());
}

static bool match(const UniqueCString& key, const Lookup& lookup) {
return CStringHasher::match(key.get(), lookup.get());
return mozilla::CStringHasher::match(key.get(), lookup.get());
}
};

Expand Down
2 changes: 1 addition & 1 deletion js/src/wasm/WasmValidate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1763,7 +1763,7 @@ DecodeGlobalSection(Decoder& d, ModuleEnvironment* env)
return d.finishSection(*range, "global");
}

typedef HashSet<const char*, CStringHasher, SystemAllocPolicy> CStringSet;
typedef HashSet<const char*, mozilla::CStringHasher, SystemAllocPolicy> CStringSet;

static UniqueChars
DecodeExportName(Decoder& d, CStringSet* dupSet)
Expand Down
Loading

0 comments on commit b85493f

Please sign in to comment.