From: Casey Bodley Date: Thu, 3 May 2018 15:19:24 +0000 (-0400) Subject: compressor: use initializer_list for compression_algorithms X-Git-Tag: v13.1.1~7^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=ad1a2e82a71b9e2a16ca2354549c92f8a2976b00;p=ceph.git compressor: use initializer_list for compression_algorithms use a compile-type array instead of heap-allocated tree to associate compression types with their names Signed-off-by: Casey Bodley (cherry picked from commit f9b76c62c0c0ddad77007893bbf62bcc2e6ee945) --- diff --git a/src/compressor/Compressor.cc b/src/compressor/Compressor.cc index 889f0c2884f..3e545d48b44 100644 --- a/src/compressor/Compressor.cc +++ b/src/compressor/Compressor.cc @@ -24,7 +24,7 @@ #include "common/debug.h" #include "common/dout.h" -std::string Compressor::get_comp_alg_name(int a) { +const char* Compressor::get_comp_alg_name(int a) { auto p = std::find_if(std::cbegin(compression_algorithms), std::cend(compression_algorithms), [a](const auto& kv) { return kv.second == a; }); @@ -32,15 +32,17 @@ std::string Compressor::get_comp_alg_name(int a) { if (std::cend(compression_algorithms) == p) return "???"; // It would be nice to revise this... - return std::string { p->first }; + return p->first; } boost::optional Compressor::get_comp_alg_type(const std::string &s) { - if (auto pos = compression_algorithms.find(s.c_str()); std::end(compression_algorithms) != pos) - return pos->second; + auto p = std::find_if(std::cbegin(compression_algorithms), std::cend(compression_algorithms), + [&s](const auto& kv) { return kv.first == s; }); + if (std::cend(compression_algorithms) == p) + return {}; - return boost::optional {}; + return p->second; } const char *Compressor::get_comp_mode_name(int m) { diff --git a/src/compressor/Compressor.h b/src/compressor/Compressor.h index cf3c40d4520..15914fb3072 100644 --- a/src/compressor/Compressor.h +++ b/src/compressor/Compressor.h @@ -15,7 +15,6 @@ #ifndef CEPH_COMPRESSOR_H #define CEPH_COMPRESSOR_H -#include #include #include #include @@ -44,7 +43,8 @@ public: COMP_ALG_LAST //the last value for range checks }; - inline static const std::map compression_algorithms { + using pair_type = std::pair; + static constexpr std::initializer_list compression_algorithms { { "none", COMP_ALG_NONE }, { "snappy", COMP_ALG_SNAPPY }, { "zlib", COMP_ALG_ZLIB }, @@ -65,7 +65,7 @@ public: COMP_FORCE ///< compress always }; - static std::string get_comp_alg_name(int a); + static const char* get_comp_alg_name(int a); static boost::optional get_comp_alg_type(const std::string &s); static const char *get_comp_mode_name(int m);