diff options
author | asvitkine <asvitkine@chromium.org> | 2015-06-23 11:22:52 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-06-23 18:23:18 +0000 |
commit | dbd26533ed86e8813de53c5e1b42028fc9ddc2a5 (patch) | |
tree | 183a4a26c1842d99b2170813364546b49d5299ee | |
parent | 30000aaf91775a7a441f0fc6f99ec349159e455e (diff) | |
download | chromium_src-dbd26533ed86e8813de53c5e1b42028fc9ddc2a5.zip chromium_src-dbd26533ed86e8813de53c5e1b42028fc9ddc2a5.tar.gz chromium_src-dbd26533ed86e8813de53c5e1b42028fc9ddc2a5.tar.bz2 |
Optimize DictionaryValue::Get() by avoiding temp string copies.
ChromeOS-Wide Profiling revealed that this function is hot.
Optimize it by avoiding many temporary string copies and resizes
by changing temporary strings to a StringPiece.
One string conversion is left per call to Get(), which is done when
calling GetWithoutPathExpansion(). We can't use StringPiece there,
since it does a std::map lookup that's keyed by std::string - which
requires actually having a std::string.
BUG=503243
Review URL: https://codereview.chromium.org/1198193007
Cr-Commit-Position: refs/heads/master@{#335707}
-rw-r--r-- | base/values.cc | 18 | ||||
-rw-r--r-- | base/values.h | 9 |
2 files changed, 15 insertions, 12 deletions
diff --git a/base/values.cc b/base/values.cc index 4534d27..55947a4 100644 --- a/base/values.cc +++ b/base/values.cc @@ -482,27 +482,29 @@ void DictionaryValue::SetStringWithoutPathExpansion( SetWithoutPathExpansion(path, new StringValue(in_value)); } -bool DictionaryValue::Get(const std::string& path, +bool DictionaryValue::Get(StringPiece path, const Value** out_value) const { DCHECK(IsStringUTF8(path)); - std::string current_path(path); + StringPiece current_path(path); const DictionaryValue* current_dictionary = this; for (size_t delimiter_position = current_path.find('.'); delimiter_position != std::string::npos; delimiter_position = current_path.find('.')) { const DictionaryValue* child_dictionary = NULL; if (!current_dictionary->GetDictionary( - current_path.substr(0, delimiter_position), &child_dictionary)) + current_path.substr(0, delimiter_position), &child_dictionary)) { return false; + } current_dictionary = child_dictionary; - current_path.erase(0, delimiter_position + 1); + current_path = current_path.substr(delimiter_position + 1); } - return current_dictionary->GetWithoutPathExpansion(current_path, out_value); + return current_dictionary->GetWithoutPathExpansion(current_path.as_string(), + out_value); } -bool DictionaryValue::Get(const std::string& path, Value** out_value) { +bool DictionaryValue::Get(StringPiece path, Value** out_value) { return static_cast<const DictionaryValue&>(*this).Get( path, const_cast<const Value**>(out_value)); @@ -588,7 +590,7 @@ bool DictionaryValue::GetBinary(const std::string& path, const_cast<const BinaryValue**>(out_value)); } -bool DictionaryValue::GetDictionary(const std::string& path, +bool DictionaryValue::GetDictionary(StringPiece path, const DictionaryValue** out_value) const { const Value* value; bool result = Get(path, &value); @@ -601,7 +603,7 @@ bool DictionaryValue::GetDictionary(const std::string& path, return true; } -bool DictionaryValue::GetDictionary(const std::string& path, +bool DictionaryValue::GetDictionary(StringPiece path, DictionaryValue** out_value) { return static_cast<const DictionaryValue&>(*this).GetDictionary( path, diff --git a/base/values.h b/base/values.h index 7feef9d..8756deb 100644 --- a/base/values.h +++ b/base/values.h @@ -30,6 +30,7 @@ #include "base/compiler_specific.h" #include "base/memory/scoped_ptr.h" #include "base/strings/string16.h" +#include "base/strings/string_piece.h" namespace base { @@ -270,8 +271,8 @@ class BASE_EXPORT DictionaryValue : public Value { // Otherwise, it will return false and |out_value| will be untouched. // Note that the dictionary always owns the value that's returned. // |out_value| is optional and will only be set if non-NULL. - bool Get(const std::string& path, const Value** out_value) const; - bool Get(const std::string& path, Value** out_value); + bool Get(StringPiece path, const Value** out_value) const; + bool Get(StringPiece path, Value** out_value); // These are convenience forms of Get(). The value will be retrieved // and the return value will be true if the path is valid and the value at @@ -287,9 +288,9 @@ class BASE_EXPORT DictionaryValue : public Value { bool GetStringASCII(const std::string& path, std::string* out_value) const; bool GetBinary(const std::string& path, const BinaryValue** out_value) const; bool GetBinary(const std::string& path, BinaryValue** out_value); - bool GetDictionary(const std::string& path, + bool GetDictionary(StringPiece path, const DictionaryValue** out_value) const; - bool GetDictionary(const std::string& path, DictionaryValue** out_value); + bool GetDictionary(StringPiece path, DictionaryValue** out_value); bool GetList(const std::string& path, const ListValue** out_value) const; bool GetList(const std::string& path, ListValue** out_value); |