From: Casey Bodley Date: Thu, 3 May 2018 15:19:24 +0000 (-0400) Subject: compressor: use initializer_list for compression_algorithms X-Git-Tag: v14.0.0~127^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=f9b76c62c0c0ddad77007893bbf62bcc2e6ee945;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 --- diff --git a/src/compressor/Compressor.cc b/src/compressor/Compressor.cc index 889f0c2884fe..3e545d48b44e 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 c78e215ce960..85899b8c9f9a 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 @@ -47,7 +46,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 }, @@ -73,7 +73,7 @@ public: QatAccel qat_accel; #endif - 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);