Use string_view for pref interface to reduce copy
If you pass in a static string literal like "Hello World!", then with
parameter type of const string& you need to construct a new string
object, requiring a copy. It will also require a copy if your data is in
another container, for example std::vector<char> . In update_engine, we
store manifest bytes in std::vector, and sometimes we want to save that
manifest to disk. This CL can help us reduce copy of the manifest(up to
2MB).
Test: treehugger
Change-Id: I70feb4c0673c174fd47f02c4bd41994f74cda743
diff --git a/common/prefs.cc b/common/prefs.cc
index 84fe536..1e06be4 100644
--- a/common/prefs.cc
+++ b/common/prefs.cc
@@ -55,7 +55,7 @@
return storage_->GetKey(key, value);
}
-bool PrefsBase::SetString(const string& key, const string& value) {
+bool PrefsBase::SetString(const string& key, std::string_view value) {
TEST_AND_RETURN_FALSE(storage_->SetKey(key, value));
const auto observers_for_key = observers_.find(key);
if (observers_for_key != observers_.end()) {
@@ -192,7 +192,7 @@
return true;
}
-bool Prefs::FileStorage::SetKey(const string& key, const string& value) {
+bool Prefs::FileStorage::SetKey(const string& key, std::string_view value) {
base::FilePath filename;
TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
if (!base::DirectoryExists(filename.DirName())) {
@@ -263,7 +263,7 @@
}
bool MemoryPrefs::MemoryStorage::SetKey(const string& key,
- const string& value) {
+ std::string_view value) {
values_[key] = value;
return true;
}