]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
compressor: use initializer_list for compression_algorithms
authorCasey Bodley <cbodley@redhat.com>
Thu, 3 May 2018 15:19:24 +0000 (11:19 -0400)
committerKefu Chai <kchai@redhat.com>
Tue, 15 May 2018 13:08:46 +0000 (21:08 +0800)
use a compile-type array instead of heap-allocated tree to associate
compression types with their names

Signed-off-by: Casey Bodley <cbodley@redhat.com>
(cherry picked from commit f9b76c62c0c0ddad77007893bbf62bcc2e6ee945)

src/compressor/Compressor.cc
src/compressor/Compressor.h

index 889f0c2884fe7f9a29947ff63ff7c473659f9a86..3e545d48b44eeb0c1da01434fff028377b681b58 100644 (file)
@@ -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::CompressionAlgorithm> 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<Compressor::CompressionAlgorithm> {};
+  return p->second;
 }
 
 const char *Compressor::get_comp_mode_name(int m) {
index cf3c40d4520ab4df9bcc0979aad2e5c88d738747..15914fb3072d0fdb08580f621b3c1324370bfb2b 100644 (file)
@@ -15,7 +15,6 @@
 #ifndef CEPH_COMPRESSOR_H
 #define CEPH_COMPRESSOR_H
 
-#include <map>
 #include <memory>
 #include <string>
 #include <string_view>
@@ -44,7 +43,8 @@ public:
     COMP_ALG_LAST   //the last value for range checks
   };
 
-  inline static const std::map<const std::string, const CompressionAlgorithm> compression_algorithms {
+  using pair_type = std::pair<const char*, CompressionAlgorithm>;
+  static constexpr std::initializer_list<pair_type> 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<CompressionAlgorithm> get_comp_alg_type(const std::string &s);
 
   static const char *get_comp_mode_name(int m);