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(
AllocationNode::FunctionId id = AllocationNode::function_id(
node->script_id_, node->script_position_, node->name_);
parent->children_.erase(id);
delete node;
node = parent;
}
}
......@@ -141,11 +140,11 @@ SamplingHeapProfiler::AllocationNode::FindOrAddChildNode(const char* name,
auto it = children_.find(id);
if (it != children_.end()) {
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);
children_.insert(std::make_pair(id, child));
return child;
auto child =
base::make_unique<AllocationNode>(this, name, script_id, start_position);
return children_.emplace(id, std::move(child)).first->second.get();
}
SamplingHeapProfiler::AllocationNode* SamplingHeapProfiler::AddStack() {
......@@ -256,19 +255,19 @@ v8::AllocationProfile::Node* SamplingHeapProfiler::TranslateAllocationNode(
allocations.push_back(ScaleSample(alloc.first, alloc.second));
}
profile->nodes().push_back(v8::AllocationProfile::Node(
{ToApiHandle<v8::String>(
profile->nodes().push_back(v8::AllocationProfile::Node{
ToApiHandle<v8::String>(
isolate_->factory()->InternalizeUtf8String(node->name_)),
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();
// 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
// the potential to be sampled. That's ok since map iterators are not
// invalidated upon std::map insertion.
for (auto it : node->children_) {
for (const auto& it : node->children_) {
current->children.push_back(
TranslateAllocationNode(profile, it.second, scripts));
TranslateAllocationNode(profile, it.second.get(), scripts));
}
node->pinned_ = false;
return current;
......@@ -293,6 +292,5 @@ v8::AllocationProfile* SamplingHeapProfiler::GetAllocationProfile() {
return profile;
}
} // namespace internal
} // namespace v8
......@@ -77,13 +77,7 @@ class SamplingHeapProfiler {
: parent_(parent),
script_id_(script_id),
script_position_(start_position),
name_(name),
pinned_(false) {}
~AllocationNode() {
for (auto child : children_) {
delete child.second;
}
}
name_(name) {}
private:
typedef uint64_t FunctionId;
......@@ -107,12 +101,12 @@ class SamplingHeapProfiler {
// TODO(alph): make use of unordered_map's here. Pay attention to
// iterator invalidation during TranslateAllocationNode.
std::map<size_t, unsigned int> allocations_;
std::map<FunctionId, AllocationNode*> children_;
std::map<FunctionId, std::unique_ptr<AllocationNode>> children_;
AllocationNode* const parent_;
const int script_id_;
const int script_position_;
const char* const name_;
bool pinned_;
bool pinned_ = false;
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