Commit b721f595 authored by Alexei Filippov's avatar Alexei Filippov Committed by Commit Bot

[sampling heap profiler] Refactor: Use std::unique_ptr to store nodes.

Change-Id: I8843796be4a9da39fad96150593837189e452c07
Reviewed-on: https://chromium-review.googlesource.com/c/1262997Reviewed-by: 's avatarAli Ijaz Sheikh <ofrobots@google.com>
Commit-Queue: Alexei Filippov <alph@chromium.org>
Cr-Commit-Position: refs/heads/master@{#56450}
parent 58e996db
...@@ -125,7 +125,6 @@ void SamplingHeapProfiler::OnWeakCallback( ...@@ -125,7 +125,6 @@ void SamplingHeapProfiler::OnWeakCallback(
AllocationNode::FunctionId id = AllocationNode::function_id( AllocationNode::FunctionId id = AllocationNode::function_id(
node->script_id_, node->script_position_, node->name_); node->script_id_, node->script_position_, node->name_);
parent->children_.erase(id); parent->children_.erase(id);
delete node;
node = parent; node = parent;
} }
} }
...@@ -141,11 +140,11 @@ SamplingHeapProfiler::AllocationNode::FindOrAddChildNode(const char* name, ...@@ -141,11 +140,11 @@ SamplingHeapProfiler::AllocationNode::FindOrAddChildNode(const char* name,
auto it = children_.find(id); auto it = children_.find(id);
if (it != children_.end()) { if (it != children_.end()) {
DCHECK_EQ(strcmp(it->second->name_, name), 0); DCHECK_EQ(strcmp(it->second->name_, name), 0);
return it->second; return it->second.get();
} }
auto child = new AllocationNode(this, name, script_id, start_position); auto child =
children_.insert(std::make_pair(id, child)); base::make_unique<AllocationNode>(this, name, script_id, start_position);
return child; return children_.emplace(id, std::move(child)).first->second.get();
} }
SamplingHeapProfiler::AllocationNode* SamplingHeapProfiler::AddStack() { SamplingHeapProfiler::AllocationNode* SamplingHeapProfiler::AddStack() {
...@@ -256,19 +255,19 @@ v8::AllocationProfile::Node* SamplingHeapProfiler::TranslateAllocationNode( ...@@ -256,19 +255,19 @@ v8::AllocationProfile::Node* SamplingHeapProfiler::TranslateAllocationNode(
allocations.push_back(ScaleSample(alloc.first, alloc.second)); allocations.push_back(ScaleSample(alloc.first, alloc.second));
} }
profile->nodes().push_back(v8::AllocationProfile::Node( profile->nodes().push_back(v8::AllocationProfile::Node{
{ToApiHandle<v8::String>( ToApiHandle<v8::String>(
isolate_->factory()->InternalizeUtf8String(node->name_)), isolate_->factory()->InternalizeUtf8String(node->name_)),
script_name, node->script_id_, node->script_position_, line, column, script_name, node->script_id_, node->script_position_, line, column,
std::vector<v8::AllocationProfile::Node*>(), allocations})); std::vector<v8::AllocationProfile::Node*>(), allocations});
v8::AllocationProfile::Node* current = &profile->nodes().back(); v8::AllocationProfile::Node* current = &profile->nodes().back();
// The children map may have nodes inserted into it during translation // The |children_| map may have nodes inserted into it during translation
// because the translation may allocate strings on the JS heap that have // because the translation may allocate strings on the JS heap that have
// the potential to be sampled. That's ok since map iterators are not // the potential to be sampled. That's ok since map iterators are not
// invalidated upon std::map insertion. // invalidated upon std::map insertion.
for (auto it : node->children_) { for (const auto& it : node->children_) {
current->children.push_back( current->children.push_back(
TranslateAllocationNode(profile, it.second, scripts)); TranslateAllocationNode(profile, it.second.get(), scripts));
} }
node->pinned_ = false; node->pinned_ = false;
return current; return current;
...@@ -293,6 +292,5 @@ v8::AllocationProfile* SamplingHeapProfiler::GetAllocationProfile() { ...@@ -293,6 +292,5 @@ v8::AllocationProfile* SamplingHeapProfiler::GetAllocationProfile() {
return profile; return profile;
} }
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
...@@ -77,13 +77,7 @@ class SamplingHeapProfiler { ...@@ -77,13 +77,7 @@ class SamplingHeapProfiler {
: parent_(parent), : parent_(parent),
script_id_(script_id), script_id_(script_id),
script_position_(start_position), script_position_(start_position),
name_(name), name_(name) {}
pinned_(false) {}
~AllocationNode() {
for (auto child : children_) {
delete child.second;
}
}
private: private:
typedef uint64_t FunctionId; typedef uint64_t FunctionId;
...@@ -107,12 +101,12 @@ class SamplingHeapProfiler { ...@@ -107,12 +101,12 @@ class SamplingHeapProfiler {
// TODO(alph): make use of unordered_map's here. Pay attention to // TODO(alph): make use of unordered_map's here. Pay attention to
// iterator invalidation during TranslateAllocationNode. // iterator invalidation during TranslateAllocationNode.
std::map<size_t, unsigned int> allocations_; std::map<size_t, unsigned int> allocations_;
std::map<FunctionId, AllocationNode*> children_; std::map<FunctionId, std::unique_ptr<AllocationNode>> children_;
AllocationNode* const parent_; AllocationNode* const parent_;
const int script_id_; const int script_id_;
const int script_position_; const int script_position_;
const char* const name_; const char* const name_;
bool pinned_; bool pinned_ = false;
friend class SamplingHeapProfiler; friend class SamplingHeapProfiler;
......
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