From b248f8903b09f61f0bda1ead6c1ffb242dadd14e Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 6 Oct 2017 18:27:51 +0800 Subject: [PATCH] common/config: return const reference instead of a copy avoid creating temporary Option::value_t instances if possible Signed-off-by: Kefu Chai --- src/common/config.cc | 17 ++++++++++------- src/common/config.h | 4 ++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/common/config.cc b/src/common/config.cc index 2b54a3bf95b..404928f0e36 100644 --- a/src/common/config.cc +++ b/src/common/config.cc @@ -871,18 +871,21 @@ int md_config_t::get_val(const std::string &key, char **buf, int len) const return _get_val(key, buf,len); } -Option::value_t md_config_t::get_val_generic(const std::string &key) const +const Option::value_t& +md_config_t::get_val_generic(const std::string &key) const { Mutex::Locker l(lock); return _get_val_generic(key); } -Option::value_t md_config_t::_get_val_generic(const std::string &key) const +const Option::value_t& +md_config_t::_get_val_generic(const std::string &key) const { assert(lock.is_locked()); + static const Option::value_t empty{boost::blank()}; if (key.empty()) { - return Option::value_t(boost::blank()); + return empty; } // In key names, leading and trailing whitespace are not significant. @@ -894,7 +897,7 @@ Option::value_t md_config_t::_get_val_generic(const std::string &key) const // entries in ::values return values.at(k); } else { - return Option::value_t(boost::blank()); + return empty; } } @@ -902,12 +905,12 @@ int md_config_t::_get_val(const std::string &key, std::string *value) const { assert(lock.is_locked()); std::string normalized_key(ConfFile::normalize_key_name(key)); - Option::value_t config_value = _get_val_generic(normalized_key.c_str()); + auto& config_value = _get_val_generic(normalized_key.c_str()); if (!boost::get(&config_value)) { ostringstream oss; - if (bool *flag = boost::get(&config_value)) { + if (auto *flag = boost::get(&config_value)) { oss << (*flag ? "true" : "false"); - } else if (double *dp = boost::get(&config_value)) { + } else if (auto *dp = boost::get(&config_value)) { oss << std::fixed << *dp; } else { oss << config_value; diff --git a/src/common/config.h b/src/common/config.h index b0ba521afee..2b2509f98a6 100644 --- a/src/common/config.h +++ b/src/common/config.h @@ -160,7 +160,7 @@ public: // No metavariables will be returned (they will have already been expanded) int get_val(const std::string &key, char **buf, int len) const; int _get_val(const std::string &key, char **buf, int len) const; - Option::value_t get_val_generic(const std::string &key) const; + const Option::value_t& get_val_generic(const std::string &key) const; template const T& get_val(const std::string &key) const; template const T& _get_val(const std::string &key) const; @@ -200,7 +200,7 @@ private: void validate_default_settings(); int _get_val(const std::string &key, std::string *value) const; - Option::value_t _get_val_generic(const std::string &key) const; + const Option::value_t& _get_val_generic(const std::string &key) const; void _show_config(std::ostream *out, Formatter *f); void _get_my_sections(std::vector §ions) const; -- 2.39.5