]> git-server-git.apps.pok.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>
Fri, 4 May 2018 03:53:21 +0000 (11:53 +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>
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 c78e215ce96015f5f52383948b2ac4994aa19a76..85899b8c9f9a3c528968e4445271d9ece6dc5701 100644 (file)
@@ -15,7 +15,6 @@
 #ifndef CEPH_COMPRESSOR_H
 #define CEPH_COMPRESSOR_H
 
-#include <map>
 #include <memory>
 #include <string>
 #include <string_view>
@@ -47,7 +46,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 },
@@ -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<CompressionAlgorithm> get_comp_alg_type(const std::string &s);
 
   static const char *get_comp_mode_name(int m);