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/fake_prefs.cc b/common/fake_prefs.cc
index 275667e..ea6ea60 100644
--- a/common/fake_prefs.cc
+++ b/common/fake_prefs.cc
@@ -17,6 +17,7 @@
#include "update_engine/common/fake_prefs.h"
#include <algorithm>
+#include <utility>
#include <gtest/gtest.h>
@@ -66,8 +67,8 @@
return GetValue(key, value);
}
-bool FakePrefs::SetString(const string& key, const string& value) {
- SetValue(key, value);
+bool FakePrefs::SetString(const string& key, std::string_view value) {
+ SetValue(key, std::string(value));
return true;
}
@@ -149,10 +150,10 @@
}
template <typename T>
-void FakePrefs::SetValue(const string& key, const T& value) {
+void FakePrefs::SetValue(const string& key, T value) {
CheckKeyType(key, PrefConsts<T>::type);
values_[key].type = PrefConsts<T>::type;
- values_[key].value.*(PrefConsts<T>::member) = value;
+ values_[key].value.*(PrefConsts<T>::member) = std::move(value);
const auto observers_for_key = observers_.find(key);
if (observers_for_key != observers_.end()) {
std::vector<ObserverInterface*> copy_observers(observers_for_key->second);