From: Kefu Chai Date: Thu, 20 May 2021 02:20:50 +0000 (+0800) Subject: common/cmdparse: use string_view for the key X-Git-Tag: v17.1.0~1853^2~3 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=da58cfbeffe104683933ebdc0e0221412d618384;p=ceph.git common/cmdparse: use string_view for the key for better usability and performance. as the main use case of cmd_getval() and cmd_putval() only uses a literal string for the key. it's a waste to build a std::string out of it and throw it away after looking the cmdmap with it. Signed-off-by: Kefu Chai --- diff --git a/src/common/cmdparse.cc b/src/common/cmdparse.cc index f1756a292a521..1754869eaa719 100644 --- a/src/common/cmdparse.cc +++ b/src/common/cmdparse.cc @@ -638,7 +638,7 @@ bool validate_cmd(CephContext* cct, } bool cmd_getval(const cmdmap_t& cmdmap, - const std::string& k, bool& val) + std::string_view k, bool& val) { /* * Specialized getval for booleans. CephBool didn't exist before Nautilus, @@ -651,7 +651,8 @@ bool cmd_getval(const cmdmap_t& cmdmap, return true; } catch (boost::bad_get&) { try { - std::string expected = "--" + k; + std::string expected{"--"}; + expected += k; std::replace(expected.begin(), expected.end(), '_', '-'); std::string v_str = boost::get(cmdmap.find(k)->second); diff --git a/src/common/cmdparse.h b/src/common/cmdparse.h index 112c249305321..78b6eb623beee 100644 --- a/src/common/cmdparse.h +++ b/src/common/cmdparse.h @@ -48,8 +48,10 @@ std::string cmd_vartype_stringify(const cmd_vartype& v); struct bad_cmd_get : public std::exception { std::string desc; - bad_cmd_get(const std::string& f, const cmdmap_t& cmdmap) { - desc = "bad or missing field '" + f + "'"; + bad_cmd_get(std::string_view f, const cmdmap_t& cmdmap) { + desc += "bad or missing field '"; + desc += f; + desc += "'"; } const char *what() const throw() override { return desc.c_str(); @@ -57,11 +59,11 @@ struct bad_cmd_get : public std::exception { }; bool cmd_getval(const cmdmap_t& cmdmap, - const std::string& k, bool& val); + std::string_view k, bool& val); template bool cmd_getval(const cmdmap_t& cmdmap, - const std::string& k, T& val) + std::string_view k, T& val) { if (cmdmap.count(k)) { try { @@ -78,7 +80,7 @@ bool cmd_getval(const cmdmap_t& cmdmap, template bool cmd_getval( - const cmdmap_t& cmdmap, const std::string& k, + const cmdmap_t& cmdmap, std::string_view k, T& val, const T& defval) { if (cmdmap.count(k)) { @@ -96,9 +98,9 @@ bool cmd_getval( template void -cmd_putval(CephContext *cct, cmdmap_t& cmdmap, const std::string& k, const T& val) +cmd_putval(CephContext *cct, cmdmap_t& cmdmap, std::string_view k, const T& val) { - cmdmap[k] = val; + cmdmap.insert_or_assign(std::string{k}, val); } bool validate_cmd(CephContext* cct, diff --git a/src/mon/MDSMonitor.cc b/src/mon/MDSMonitor.cc index 92ced5a5efc2b..1b927ae358413 100644 --- a/src/mon/MDSMonitor.cc +++ b/src/mon/MDSMonitor.cc @@ -86,19 +86,19 @@ static const string MDS_HEALTH_PREFIX("mds_health"); */ namespace TOPNSPC::common { template<> bool cmd_getval(const cmdmap_t& cmdmap, - const std::string& k, mds_gid_t &val) + std::string_view k, mds_gid_t &val) { return cmd_getval(cmdmap, k, (int64_t&)val); } template<> bool cmd_getval(const cmdmap_t& cmdmap, - const std::string& k, mds_rank_t &val) + std::string_view k, mds_rank_t &val) { return cmd_getval(cmdmap, k, (int64_t&)val); } template<> bool cmd_getval(const cmdmap_t& cmdmap, - const std::string& k, MDSMap::DaemonState &val) + std::string_view k, MDSMap::DaemonState &val) { return cmd_getval(cmdmap, k, (int64_t&)val); }