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.h b/common/fake_prefs.h
index 9af2550..430c291 100644
--- a/common/fake_prefs.h
+++ b/common/fake_prefs.h
@@ -19,6 +19,7 @@
#include <map>
#include <string>
+#include <string_view>
#include <vector>
#include <base/macros.h>
@@ -40,7 +41,7 @@
// PrefsInterface methods.
bool GetString(const std::string& key, std::string* value) const override;
- bool SetString(const std::string& key, const std::string& value) override;
+ bool SetString(const std::string& key, std::string_view value) override;
bool GetInt64(const std::string& key, int64_t* value) const override;
bool SetInt64(const std::string& key, const int64_t value) override;
bool GetBoolean(const std::string& key, bool* value) const override;
@@ -96,7 +97,7 @@
// Helper function to set a value of the passed |key|. It sets the type based
// on the template parameter T.
template <typename T>
- void SetValue(const std::string& key, const T& value);
+ void SetValue(const std::string& key, T value);
// Helper function to get a value from the map checking for invalid calls.
// The function fails the test if you attempt to read a value defined as a