Chromium Code Reviews| Index: base/trace_event/heap_profiler_string_deduplicator.cc |
| diff --git a/base/trace_event/heap_profiler_string_deduplicator.cc b/base/trace_event/heap_profiler_string_deduplicator.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1c0f1d5b5d5bee76e6e958747721ffb022dfc100 |
| --- /dev/null |
| +++ b/base/trace_event/heap_profiler_string_deduplicator.cc |
| @@ -0,0 +1,54 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/trace_event/heap_profiler_string_deduplicator.h" |
| + |
| +#include "base/trace_event/memory_usage_estimator.h" |
| +#include "base/trace_event/trace_event.h" |
| +#include "base/trace_event/trace_event_argument.h" |
| +#include "base/trace_event/trace_event_memory_overhead.h" |
| + |
| +namespace base { |
| +namespace trace_event { |
| + |
| +StringDeduplicator::StringDeduplicator() : last_exported_index_(0) { |
| + // Add implicit entry for id 0 (NULL strings). |
| + strings_.push_back("[null]"); |
| +} |
| + |
| +StringDeduplicator::~StringDeduplicator() {} |
| + |
| +int StringDeduplicator::Insert(StringPiece string) { |
| + if (!string.data()) { |
|
Primiano Tucci (use gerrit)
2017/03/09 11:47:45
should this also be + || string.empty() to reuse t
DmitrySkiba
2017/03/14 22:12:47
NULL is the only value that means "no type name",
|
| + // NULL strings are mapped to id 0. |
| + return 0; |
| + } |
| + int next_id = static_cast<int>(strings_.size()); |
| + auto iter_and_flag = string_ids_.insert({string, next_id}); |
| + if (iter_and_flag.second) { |
| + // Inserted a new mapping |
| + strings_.push_back(string.as_string()); |
|
Primiano Tucci (use gerrit)
2017/03/09 11:47:45
I think that if you do
strings_.emplace_back(strin
DmitrySkiba
2017/03/14 22:12:47
push_back() has && variant, which is used in this
|
| + } |
| + return iter_and_flag.first->second; |
| +} |
| + |
| +void StringDeduplicator::ExportIncrementally(TracedValue* traced_value) { |
| + for (; last_exported_index_ != strings_.size(); ++last_exported_index_) { |
| + traced_value->BeginDictionary(); |
| + traced_value->SetInteger("id", last_exported_index_); |
| + traced_value->SetString("string", strings_[last_exported_index_]); |
| + traced_value->EndDictionary(); |
| + } |
| +} |
| + |
| +void StringDeduplicator::EstimateTraceMemoryOverhead( |
| + TraceEventMemoryOverhead* overhead) { |
| + size_t memory_usage = |
| + EstimateMemoryUsage(string_ids_) + EstimateMemoryUsage(strings_); |
| + overhead->Add("StringDeduplicator", |
| + sizeof(StringDeduplicator) + memory_usage); |
| +} |
| + |
| +} // namespace trace_event |
| +} // namespace base |