Commit 7c574344 authored by Peter Marshall's avatar Peter Marshall Committed by Commit Bot

[cleanup] Change native_groups_ to use unordered map.

Trying to reduce use of our self-baked data structures.

Bug: v8:7570
Change-Id: I419a932b6b8904810844d40a5636e423df832197
Reviewed-on: https://chromium-review.googlesource.com/1032739
Commit-Queue: Peter Marshall <petermarshall@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53033}
parent e14ed48c
......@@ -14,6 +14,7 @@
#include "src/allocation.h"
#include "src/base/platform/time.h"
#include "src/string-hasher.h"
#include "src/utils.h"
#include "src/base/once.h"
......@@ -56,7 +57,7 @@ class CounterCollection {
Counter counters_[kMaxCounters];
};
struct StringHasher {
struct CStringHasher {
std::size_t operator()(const char* name) const {
size_t h = 0;
size_t c;
......@@ -68,13 +69,8 @@ struct StringHasher {
}
};
struct StringEquals {
bool operator()(const char* name1, const char* name2) const {
return strcmp(name1, name2) == 0;
}
};
typedef std::unordered_map<const char*, Counter*, StringHasher, StringEquals>
typedef std::unordered_map<const char*, Counter*, CStringHasher,
i::StringEquals>
CounterMap;
class SourceGroup {
......
......@@ -2141,13 +2141,38 @@ HeapEntry* EmbedderGraphEntriesAllocator::AllocateEntry(HeapThing ptr) {
static_cast<int>(size), 0);
}
class NativeGroupRetainedObjectInfo : public v8::RetainedObjectInfo {
public:
explicit NativeGroupRetainedObjectInfo(const char* label)
: disposed_(false),
hash_(reinterpret_cast<intptr_t>(label)),
label_(label) {}
virtual ~NativeGroupRetainedObjectInfo() {}
virtual void Dispose() {
CHECK(!disposed_);
disposed_ = true;
delete this;
}
virtual bool IsEquivalent(RetainedObjectInfo* other) {
return hash_ == other->GetHash() && !strcmp(label_, other->GetLabel());
}
virtual intptr_t GetHash() { return hash_; }
virtual const char* GetLabel() { return label_; }
private:
bool disposed_;
intptr_t hash_;
const char* label_;
};
NativeObjectsExplorer::NativeObjectsExplorer(
HeapSnapshot* snapshot, SnapshottingProgressReportingInterface* progress)
: isolate_(snapshot->profiler()->heap_object_map()->heap()->isolate()),
snapshot_(snapshot),
names_(snapshot_->profiler()->names()),
embedder_queried_(false),
native_groups_(StringsMatch),
native_groups_(0, SeededStringHasher(isolate_->heap()->HashSeed())),
synthetic_entries_allocator_(
new BasicHeapEntriesAllocator(snapshot, HeapEntry::kSynthetic)),
native_entries_allocator_(
......@@ -2163,10 +2188,8 @@ NativeObjectsExplorer::~NativeObjectsExplorer() {
std::vector<HeapObject*>* objects = map_entry.second;
delete objects;
}
for (base::HashMap::Entry* p = native_groups_.Start(); p != nullptr;
p = native_groups_.Next(p)) {
v8::RetainedObjectInfo* info =
reinterpret_cast<v8::RetainedObjectInfo*>(p->value);
for (auto map_entry : native_groups_) {
NativeGroupRetainedObjectInfo* info = map_entry.second;
info->Dispose();
}
}
......@@ -2319,50 +2342,16 @@ bool NativeObjectsExplorer::IterateAndExtractReferences(
return true;
}
class NativeGroupRetainedObjectInfo : public v8::RetainedObjectInfo {
public:
explicit NativeGroupRetainedObjectInfo(const char* label)
: disposed_(false),
hash_(reinterpret_cast<intptr_t>(label)),
label_(label) {
}
virtual ~NativeGroupRetainedObjectInfo() {}
virtual void Dispose() {
CHECK(!disposed_);
disposed_ = true;
delete this;
}
virtual bool IsEquivalent(RetainedObjectInfo* other) {
return hash_ == other->GetHash() && !strcmp(label_, other->GetLabel());
}
virtual intptr_t GetHash() { return hash_; }
virtual const char* GetLabel() { return label_; }
private:
bool disposed_;
intptr_t hash_;
const char* label_;
};
NativeGroupRetainedObjectInfo* NativeObjectsExplorer::FindOrAddGroupInfo(
const char* label) {
const char* label_copy = names_->GetCopy(label);
uint32_t hash = StringHasher::HashSequentialString(
label_copy,
static_cast<int>(strlen(label_copy)),
isolate_->heap()->HashSeed());
base::HashMap::Entry* entry =
native_groups_.LookupOrInsert(const_cast<char*>(label_copy), hash);
if (entry->value == nullptr) {
entry->value = new NativeGroupRetainedObjectInfo(label);
auto map_entry = native_groups_.find(label_copy);
if (map_entry == native_groups_.end()) {
native_groups_[label_copy] = new NativeGroupRetainedObjectInfo(label);
}
return static_cast<NativeGroupRetainedObjectInfo*>(entry->value);
return native_groups_[label_copy];
}
void NativeObjectsExplorer::SetNativeRootReference(
v8::RetainedObjectInfo* info) {
HeapEntry* child_entry =
......@@ -2398,10 +2387,8 @@ void NativeObjectsExplorer::SetWrapperNativeReferences(
void NativeObjectsExplorer::SetRootNativeRootsReference() {
for (base::HashMap::Entry* entry = native_groups_.Start(); entry;
entry = native_groups_.Next(entry)) {
NativeGroupRetainedObjectInfo* group_info =
static_cast<NativeGroupRetainedObjectInfo*>(entry->value);
for (auto map_entry : native_groups_) {
NativeGroupRetainedObjectInfo* group_info = map_entry.second;
HeapEntry* group_entry =
filler_->FindOrAddEntry(group_info, native_entries_allocator_.get());
DCHECK_NOT_NULL(group_entry);
......
......@@ -518,10 +518,6 @@ class NativeObjectsExplorer {
return info1 == info2 || info1->IsEquivalent(info2);
}
};
INLINE(static bool StringsMatch(void* key1, void* key2)) {
return strcmp(reinterpret_cast<char*>(key1),
reinterpret_cast<char*>(key2)) == 0;
}
NativeGroupRetainedObjectInfo* FindOrAddGroupInfo(const char* label);
......@@ -535,7 +531,9 @@ class NativeObjectsExplorer {
std::unordered_map<v8::RetainedObjectInfo*, std::vector<HeapObject*>*,
RetainedInfoHasher, RetainedInfoEquals>
objects_by_info_;
base::CustomMatcherHashMap native_groups_;
std::unordered_map<const char*, NativeGroupRetainedObjectInfo*,
SeededStringHasher, StringEquals>
native_groups_;
std::unique_ptr<HeapEntriesAllocator> synthetic_entries_allocator_;
std::unique_ptr<HeapEntriesAllocator> native_entries_allocator_;
std::unique_ptr<HeapEntriesAllocator> embedder_graph_entries_allocator_;
......
......@@ -142,6 +142,11 @@ void IteratingStringHasher::VisitTwoByteString(const uint16_t* chars,
AddCharacters(chars, length);
}
std::size_t SeededStringHasher::operator()(const char* name) const {
return StringHasher::HashSequentialString(
name, static_cast<int>(strlen(name)), hashseed_);
}
} // namespace internal
} // namespace v8
......
......@@ -84,6 +84,21 @@ class IteratingStringHasher : public StringHasher {
DISALLOW_COPY_AND_ASSIGN(IteratingStringHasher);
};
// Useful for std containers that require something ()'able.
struct SeededStringHasher {
explicit SeededStringHasher(uint32_t hashseed) : hashseed_(hashseed) {}
inline std::size_t operator()(const char* name) const;
uint32_t hashseed_;
};
// Useful for std containers that require something ()'able.
struct StringEquals {
bool operator()(const char* name1, const char* name2) const {
return strcmp(name1, name2) == 0;
}
};
} // namespace internal
} // namespace v8
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment