Commit 8e76f75e authored by Orin Jaworski's avatar Orin Jaworski Committed by Chromium LUCI CQ

[omnibox] Rework pedals memory estimators to use base::trace_events

This CL changes several EstimateMemoryUsage implementations to
use existing estimator functions in base::trace_events. This simplifies
the code, makes it more maintainable, and reduces the chance of error.

Bug: 1157612
Change-Id: I6fc6ee5b697013f820903ae673a1677cf02dd7e2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2596059Reviewed-by: default avatarAngela Yoeurng <yoangela@chromium.org>
Commit-Queue: Orin Jaworski <orinj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#838923}
parent c3b7ece4
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <numeric> #include <numeric>
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/trace_event/memory_usage_estimator.h"
#include "components/omnibox/browser/omnibox_client.h" #include "components/omnibox/browser/omnibox_client.h"
#include "components/omnibox/browser/omnibox_edit_controller.h" #include "components/omnibox/browser/omnibox_edit_controller.h"
#include "components/omnibox/browser/omnibox_field_trial.h" #include "components/omnibox/browser/omnibox_field_trial.h"
...@@ -98,13 +99,23 @@ void OmniboxPedal::SynonymGroup::AddSynonym(OmniboxPedal::Tokens&& synonym) { ...@@ -98,13 +99,23 @@ void OmniboxPedal::SynonymGroup::AddSynonym(OmniboxPedal::Tokens&& synonym) {
} }
size_t OmniboxPedal::SynonymGroup::EstimateMemoryUsage() const { size_t OmniboxPedal::SynonymGroup::EstimateMemoryUsage() const {
size_t total = sizeof(*this); return base::trace_event::EstimateMemoryUsage(synonyms_);
total += std::accumulate(synonyms_.begin(), synonyms_.end(), 0, }
[](size_t sum, auto& tokens) {
return sum + tokens.capacity() * sizeof(tokens[0]); // =============================================================================
});
namespace base {
namespace trace_event {
size_t EstimateMemoryUsage(const OmniboxPedal::LabelStrings& self) {
size_t total = 0;
total += base::trace_event::EstimateMemoryUsage(self.hint);
total += base::trace_event::EstimateMemoryUsage(self.hint_short);
total += base::trace_event::EstimateMemoryUsage(self.suggestion_contents);
total += base::trace_event::EstimateMemoryUsage(self.accessibility_hint);
return total; return total;
} }
} // namespace trace_event
} // namespace base
// ============================================================================= // =============================================================================
...@@ -151,19 +162,10 @@ void OmniboxPedal::AddSynonymGroup(SynonymGroup&& group) { ...@@ -151,19 +162,10 @@ void OmniboxPedal::AddSynonymGroup(SynonymGroup&& group) {
} }
size_t OmniboxPedal::EstimateMemoryUsage() const { size_t OmniboxPedal::EstimateMemoryUsage() const {
size_t total = sizeof(*this); size_t total = 0;
total += url_.EstimateMemoryUsage(); total += base::trace_event::EstimateMemoryUsage(url_);
total += std::accumulate( total += base::trace_event::EstimateMemoryUsage(strings_);
synonym_groups_.begin(), synonym_groups_.end(), 0, total += base::trace_event::EstimateMemoryUsage(synonym_groups_);
[](size_t sum, auto& s) { return sum + s.EstimateMemoryUsage(); });
total += (synonym_groups_.capacity() - synonym_groups_.size()) *
sizeof(synonym_groups_[0]);
total += strings_.hint.size() * sizeof(strings_.hint[0]);
total += strings_.hint_short.size() * sizeof(strings_.hint_short[0]);
total += strings_.suggestion_contents.size() *
sizeof(strings_.suggestion_contents[0]);
total += strings_.accessibility_hint.size() *
sizeof(strings_.accessibility_hint[0]);
return total; return total;
} }
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/strings/string_tokenizer.h" #include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/trace_event/memory_usage_estimator.h"
#include "components/omnibox/browser/autocomplete_input.h" #include "components/omnibox/browser/autocomplete_input.h"
#include "components/omnibox/browser/autocomplete_provider_client.h" #include "components/omnibox/browser/autocomplete_provider_client.h"
#include "components/omnibox/browser/omnibox_field_trial.h" #include "components/omnibox/browser/omnibox_field_trial.h"
...@@ -67,17 +68,11 @@ void OmniboxPedalProvider::ResetSession() { ...@@ -67,17 +68,11 @@ void OmniboxPedalProvider::ResetSession() {
} }
size_t OmniboxPedalProvider::EstimateMemoryUsage() const { size_t OmniboxPedalProvider::EstimateMemoryUsage() const {
size_t total = sizeof(*this); size_t total = 0;
total += std::accumulate(dictionary_.begin(), dictionary_.end(), 0, total += base::trace_event::EstimateMemoryUsage(dictionary_);
[](size_t sum, auto& s) { total += base::trace_event::EstimateMemoryUsage(ignore_group_);
return sum + s.first.size() * sizeof(s.first[0]); total += base::trace_event::EstimateMemoryUsage(pedals_);
}); total += base::trace_event::EstimateMemoryUsage(tokenize_characters_);
total += dictionary_.size() * (sizeof(int) + sizeof(base::string16));
total += ignore_group_.EstimateMemoryUsage();
total += std::accumulate(pedals_.begin(), pedals_.end(), 0,
[](size_t sum, auto& s) {
return sum + s.second->EstimateMemoryUsage();
});
return total; return total;
} }
......
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