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.h b/common/prefs.h
index d6ef668..93477dd 100644
--- a/common/prefs.h
+++ b/common/prefs.h
@@ -19,6 +19,7 @@
#include <map>
#include <string>
+#include <string_view>
#include <vector>
#include <base/files/file_path.h>
@@ -49,7 +50,7 @@
// Set the value of the key named |key| to |value| regardless of the
// previous value. Returns whether the operation succeeded.
- virtual bool SetKey(const std::string& key, const std::string& value) = 0;
+ virtual bool SetKey(const std::string& key, std::string_view value) = 0;
// Returns whether the key named |key| exists.
virtual bool KeyExists(const std::string& key) const = 0;
@@ -66,7 +67,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;
@@ -123,7 +124,7 @@
bool GetKey(const std::string& key, std::string* value) const override;
bool GetSubKeys(const std::string& ns,
std::vector<std::string>* keys) const override;
- bool SetKey(const std::string& key, const std::string& value) override;
+ bool SetKey(const std::string& key, std::string_view value) override;
bool KeyExists(const std::string& key) const override;
bool DeleteKey(const std::string& key) override;
@@ -163,7 +164,7 @@
bool GetKey(const std::string& key, std::string* value) const override;
bool GetSubKeys(const std::string& ns,
std::vector<std::string>* keys) const override;
- bool SetKey(const std::string& key, const std::string& value) override;
+ bool SetKey(const std::string& key, std::string_view value) override;
bool KeyExists(const std::string& key) const override;
bool DeleteKey(const std::string& key) override;