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 @@ ...@@ -14,6 +14,7 @@
#include "src/allocation.h" #include "src/allocation.h"
#include "src/base/platform/time.h" #include "src/base/platform/time.h"
#include "src/string-hasher.h"
#include "src/utils.h" #include "src/utils.h"
#include "src/base/once.h" #include "src/base/once.h"
...@@ -56,7 +57,7 @@ class CounterCollection { ...@@ -56,7 +57,7 @@ class CounterCollection {
Counter counters_[kMaxCounters]; Counter counters_[kMaxCounters];
}; };
struct StringHasher { struct CStringHasher {
std::size_t operator()(const char* name) const { std::size_t operator()(const char* name) const {
size_t h = 0; size_t h = 0;
size_t c; size_t c;
...@@ -68,13 +69,8 @@ struct StringHasher { ...@@ -68,13 +69,8 @@ struct StringHasher {
} }
}; };
struct StringEquals { typedef std::unordered_map<const char*, Counter*, CStringHasher,
bool operator()(const char* name1, const char* name2) const { i::StringEquals>
return strcmp(name1, name2) == 0;
}
};
typedef std::unordered_map<const char*, Counter*, StringHasher, StringEquals>
CounterMap; CounterMap;
class SourceGroup { class SourceGroup {
......
...@@ -2141,13 +2141,38 @@ HeapEntry* EmbedderGraphEntriesAllocator::AllocateEntry(HeapThing ptr) { ...@@ -2141,13 +2141,38 @@ HeapEntry* EmbedderGraphEntriesAllocator::AllocateEntry(HeapThing ptr) {
static_cast<int>(size), 0); 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( NativeObjectsExplorer::NativeObjectsExplorer(
HeapSnapshot* snapshot, SnapshottingProgressReportingInterface* progress) HeapSnapshot* snapshot, SnapshottingProgressReportingInterface* progress)
: isolate_(snapshot->profiler()->heap_object_map()->heap()->isolate()), : isolate_(snapshot->profiler()->heap_object_map()->heap()->isolate()),
snapshot_(snapshot), snapshot_(snapshot),
names_(snapshot_->profiler()->names()), names_(snapshot_->profiler()->names()),
embedder_queried_(false), embedder_queried_(false),
native_groups_(StringsMatch), native_groups_(0, SeededStringHasher(isolate_->heap()->HashSeed())),
synthetic_entries_allocator_( synthetic_entries_allocator_(
new BasicHeapEntriesAllocator(snapshot, HeapEntry::kSynthetic)), new BasicHeapEntriesAllocator(snapshot, HeapEntry::kSynthetic)),
native_entries_allocator_( native_entries_allocator_(
...@@ -2163,10 +2188,8 @@ NativeObjectsExplorer::~NativeObjectsExplorer() { ...@@ -2163,10 +2188,8 @@ NativeObjectsExplorer::~NativeObjectsExplorer() {
std::vector<HeapObject*>* objects = map_entry.second; std::vector<HeapObject*>* objects = map_entry.second;
delete objects; delete objects;
} }
for (base::HashMap::Entry* p = native_groups_.Start(); p != nullptr; for (auto map_entry : native_groups_) {
p = native_groups_.Next(p)) { NativeGroupRetainedObjectInfo* info = map_entry.second;
v8::RetainedObjectInfo* info =
reinterpret_cast<v8::RetainedObjectInfo*>(p->value);
info->Dispose(); info->Dispose();
} }
} }
...@@ -2319,50 +2342,16 @@ bool NativeObjectsExplorer::IterateAndExtractReferences( ...@@ -2319,50 +2342,16 @@ bool NativeObjectsExplorer::IterateAndExtractReferences(
return true; 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( NativeGroupRetainedObjectInfo* NativeObjectsExplorer::FindOrAddGroupInfo(
const char* label) { const char* label) {
const char* label_copy = names_->GetCopy(label); const char* label_copy = names_->GetCopy(label);
uint32_t hash = StringHasher::HashSequentialString( auto map_entry = native_groups_.find(label_copy);
label_copy, if (map_entry == native_groups_.end()) {
static_cast<int>(strlen(label_copy)), native_groups_[label_copy] = new NativeGroupRetainedObjectInfo(label);
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);
} }
return static_cast<NativeGroupRetainedObjectInfo*>(entry->value); return native_groups_[label_copy];
} }
void NativeObjectsExplorer::SetNativeRootReference( void NativeObjectsExplorer::SetNativeRootReference(
v8::RetainedObjectInfo* info) { v8::RetainedObjectInfo* info) {
HeapEntry* child_entry = HeapEntry* child_entry =
...@@ -2398,10 +2387,8 @@ void NativeObjectsExplorer::SetWrapperNativeReferences( ...@@ -2398,10 +2387,8 @@ void NativeObjectsExplorer::SetWrapperNativeReferences(
void NativeObjectsExplorer::SetRootNativeRootsReference() { void NativeObjectsExplorer::SetRootNativeRootsReference() {
for (base::HashMap::Entry* entry = native_groups_.Start(); entry; for (auto map_entry : native_groups_) {
entry = native_groups_.Next(entry)) { NativeGroupRetainedObjectInfo* group_info = map_entry.second;
NativeGroupRetainedObjectInfo* group_info =
static_cast<NativeGroupRetainedObjectInfo*>(entry->value);
HeapEntry* group_entry = HeapEntry* group_entry =
filler_->FindOrAddEntry(group_info, native_entries_allocator_.get()); filler_->FindOrAddEntry(group_info, native_entries_allocator_.get());
DCHECK_NOT_NULL(group_entry); DCHECK_NOT_NULL(group_entry);
......
...@@ -518,10 +518,6 @@ class NativeObjectsExplorer { ...@@ -518,10 +518,6 @@ class NativeObjectsExplorer {
return info1 == info2 || info1->IsEquivalent(info2); 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); NativeGroupRetainedObjectInfo* FindOrAddGroupInfo(const char* label);
...@@ -535,7 +531,9 @@ class NativeObjectsExplorer { ...@@ -535,7 +531,9 @@ class NativeObjectsExplorer {
std::unordered_map<v8::RetainedObjectInfo*, std::vector<HeapObject*>*, std::unordered_map<v8::RetainedObjectInfo*, std::vector<HeapObject*>*,
RetainedInfoHasher, RetainedInfoEquals> RetainedInfoHasher, RetainedInfoEquals>
objects_by_info_; 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> synthetic_entries_allocator_;
std::unique_ptr<HeapEntriesAllocator> native_entries_allocator_; std::unique_ptr<HeapEntriesAllocator> native_entries_allocator_;
std::unique_ptr<HeapEntriesAllocator> embedder_graph_entries_allocator_; std::unique_ptr<HeapEntriesAllocator> embedder_graph_entries_allocator_;
......
...@@ -142,6 +142,11 @@ void IteratingStringHasher::VisitTwoByteString(const uint16_t* chars, ...@@ -142,6 +142,11 @@ void IteratingStringHasher::VisitTwoByteString(const uint16_t* chars,
AddCharacters(chars, length); 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 internal
} // namespace v8 } // namespace v8
......
...@@ -84,6 +84,21 @@ class IteratingStringHasher : public StringHasher { ...@@ -84,6 +84,21 @@ class IteratingStringHasher : public StringHasher {
DISALLOW_COPY_AND_ASSIGN(IteratingStringHasher); 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 internal
} // namespace v8 } // 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