Commit 35985ce6 authored by Peter Marshall's avatar Peter Marshall Committed by Commit Bot

[cpu-profiler] Use std::unordered_map for hashmaps.

There doesn't seem to be any reason to use our custom hashmap here,
which has a more complicated interface.

Change-Id: Ib08c2e400a3cb402a5984b925034aac29750c2ec
Reviewed-on: https://chromium-review.googlesource.com/1019445Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Commit-Queue: Peter Marshall <petermarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#52724}
parent 384a51da
......@@ -35,10 +35,8 @@ ProfileNode::ProfileNode(ProfileTree* tree, CodeEntry* entry,
: tree_(tree),
entry_(entry),
self_ticks_(0),
children_(CodeEntriesMatch),
parent_(parent),
id_(tree->next_node_id()),
line_ticks_(LineTickMatch) {
id_(tree->next_node_id()) {
tree_->EnqueueNode(this);
}
......
......@@ -98,8 +98,7 @@ uint32_t CodeEntry::GetHash() const {
return hash;
}
bool CodeEntry::IsSameFunctionAs(CodeEntry* entry) const {
bool CodeEntry::IsSameFunctionAs(const CodeEntry* entry) const {
if (this == entry) return true;
if (script_id_ != v8::UnboundScript::kNoScriptId) {
return script_id_ == entry->script_id_ && position_ == entry->position_;
......@@ -173,23 +172,21 @@ void ProfileNode::CollectDeoptInfo(CodeEntry* entry) {
ProfileNode* ProfileNode::FindChild(CodeEntry* entry) {
base::HashMap::Entry* map_entry =
children_.Lookup(entry, CodeEntryHash(entry));
return map_entry != nullptr ? reinterpret_cast<ProfileNode*>(map_entry->value)
: nullptr;
auto map_entry = children_.find(entry);
return map_entry != children_.end() ? map_entry->second : nullptr;
}
ProfileNode* ProfileNode::FindOrAddChild(CodeEntry* entry) {
base::HashMap::Entry* map_entry =
children_.LookupOrInsert(entry, CodeEntryHash(entry));
ProfileNode* node = reinterpret_cast<ProfileNode*>(map_entry->value);
if (!node) {
node = new ProfileNode(tree_, entry, this);
map_entry->value = node;
auto map_entry = children_.find(entry);
if (map_entry == children_.end()) {
ProfileNode* node = new ProfileNode(tree_, entry, this);
children_[entry] = node;
children_list_.push_back(node);
return node;
} else {
return map_entry->second;
}
return node;
}
......@@ -197,10 +194,12 @@ void ProfileNode::IncrementLineTicks(int src_line) {
if (src_line == v8::CpuProfileNode::kNoLineNumberInfo) return;
// Increment a hit counter of a certain source line.
// Add a new source line if not found.
base::HashMap::Entry* e =
line_ticks_.LookupOrInsert(reinterpret_cast<void*>(src_line), src_line);
DCHECK(e);
e->value = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(e->value) + 1);
auto map_entry = line_ticks_.find(src_line);
if (map_entry == line_ticks_.end()) {
line_ticks_[src_line] = 1;
} else {
line_ticks_[src_line]++;
}
}
......@@ -208,19 +207,16 @@ bool ProfileNode::GetLineTicks(v8::CpuProfileNode::LineTick* entries,
unsigned int length) const {
if (entries == nullptr || length == 0) return false;
unsigned line_count = line_ticks_.occupancy();
unsigned line_count = static_cast<unsigned>(line_ticks_.size());
if (line_count == 0) return true;
if (length < line_count) return false;
v8::CpuProfileNode::LineTick* entry = entries;
for (base::HashMap::Entry *p = line_ticks_.Start(); p != nullptr;
p = line_ticks_.Next(p), entry++) {
entry->line =
static_cast<unsigned int>(reinterpret_cast<uintptr_t>(p->key));
entry->hit_count =
static_cast<unsigned int>(reinterpret_cast<uintptr_t>(p->value));
for (auto p = line_ticks_.begin(); p != line_ticks_.end(); p++, entry++) {
entry->line = p->first;
entry->hit_count = p->second;
}
return true;
......@@ -253,9 +249,8 @@ void ProfileNode::Print(int indent) {
base::OS::Print("%*s bailed out due to '%s'\n", indent + 10, "",
bailout_reason);
}
for (base::HashMap::Entry* p = children_.Start(); p != nullptr;
p = children_.Next(p)) {
reinterpret_cast<ProfileNode*>(p->value)->Print(indent + 2);
for (auto child : children_) {
child.second->Print(indent + 2);
}
}
......@@ -276,8 +271,7 @@ ProfileTree::ProfileTree(Isolate* isolate)
next_node_id_(1),
root_(new ProfileNode(this, &root_entry_, nullptr)),
isolate_(isolate),
next_function_id_(1),
function_ids_(ProfileNode::CodeEntriesMatch) {}
next_function_id_(1) {}
ProfileTree::~ProfileTree() {
DeleteNodesCallback cb;
......@@ -287,12 +281,11 @@ ProfileTree::~ProfileTree() {
unsigned ProfileTree::GetFunctionId(const ProfileNode* node) {
CodeEntry* code_entry = node->entry();
base::HashMap::Entry* entry =
function_ids_.LookupOrInsert(code_entry, code_entry->GetHash());
if (!entry->value) {
entry->value = reinterpret_cast<void*>(next_function_id_++);
auto map_entry = function_ids_.find(code_entry);
if (map_entry == function_ids_.end()) {
return function_ids_[code_entry] = next_function_id_++;
}
return static_cast<unsigned>(reinterpret_cast<uintptr_t>(entry->value));
return function_ids_[code_entry];
}
ProfileNode* ProfileTree::AddPathFromEnd(const std::vector<CodeEntry*>& path,
......
......@@ -6,11 +6,11 @@
#define V8_PROFILER_PROFILE_GENERATOR_H_
#include <map>
#include <unordered_map>
#include <vector>
#include "include/v8-profiler.h"
#include "src/allocation.h"
#include "src/base/hashmap.h"
#include "src/log.h"
#include "src/profiler/strings-storage.h"
#include "src/source-position.h"
......@@ -85,7 +85,7 @@ class CodeEntry {
}
uint32_t GetHash() const;
bool IsSameFunctionAs(CodeEntry* entry) const;
bool IsSameFunctionAs(const CodeEntry* entry) const;
int GetSourceLine(int pc_offset) const;
......@@ -188,7 +188,9 @@ class ProfileNode {
unsigned id() const { return id_; }
unsigned function_id() const;
ProfileNode* parent() const { return parent_; }
unsigned int GetHitLineCount() const { return line_ticks_.occupancy(); }
unsigned int GetHitLineCount() const {
return static_cast<unsigned int>(line_ticks_.size());
}
bool GetLineTicks(v8::CpuProfileNode::LineTick* entries,
unsigned int length) const;
void CollectDeoptInfo(CodeEntry* entry);
......@@ -199,25 +201,26 @@ class ProfileNode {
void Print(int indent);
static bool CodeEntriesMatch(void* entry1, void* entry2) {
return reinterpret_cast<CodeEntry*>(entry1)
->IsSameFunctionAs(reinterpret_cast<CodeEntry*>(entry2));
}
private:
static uint32_t CodeEntryHash(CodeEntry* entry) { return entry->GetHash(); }
static bool LineTickMatch(void* a, void* b) { return a == b; }
struct CodeEntryEqual {
bool operator()(CodeEntry* entry1, CodeEntry* entry2) const {
return entry1 == entry2 || entry1->IsSameFunctionAs(entry2);
}
};
struct CodeEntryHash {
std::size_t operator()(CodeEntry* entry) const { return entry->GetHash(); }
};
ProfileTree* tree_;
CodeEntry* entry_;
unsigned self_ticks_;
// Mapping from CodeEntry* to ProfileNode*
base::CustomMatcherHashMap children_;
std::unordered_map<CodeEntry*, ProfileNode*, CodeEntryHash, CodeEntryEqual>
children_;
std::vector<ProfileNode*> children_list_;
ProfileNode* parent_;
unsigned id_;
base::CustomMatcherHashMap line_ticks_;
// maps line number --> number of ticks
std::unordered_map<int, int> line_ticks_;
std::vector<CpuProfileDeoptInfo> deopt_infos_;
......@@ -262,7 +265,7 @@ class ProfileTree {
Isolate* isolate_;
unsigned next_function_id_;
base::CustomMatcherHashMap function_ids_;
std::unordered_map<CodeEntry*, unsigned> function_ids_;
DISALLOW_COPY_AND_ASSIGN(ProfileTree);
};
......
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