]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/LogEntry: make LogEntryKey opaque and cache its hash value
authorSage Weil <sage@redhat.com>
Tue, 6 Jun 2017 18:50:46 +0000 (14:50 -0400)
committerSage Weil <sage@redhat.com>
Tue, 6 Jun 2017 18:50:46 +0000 (14:50 -0400)
This avoids recalculating.  We eliminate the default ctor at the same
time and make the type totally opaque (so that its fields cannot be
modified except by the ctor and decode).

Signed-off-by: Sage Weil <sage@redhat.com>
src/common/LogEntry.cc
src/common/LogEntry.h

index 52af1eb878d745abcfa2230b5bfbc50fc2b485e5..40bdbea2b3ac72e929d6515ecabb684d0bf66b86 100644 (file)
@@ -20,6 +20,7 @@ void LogEntryKey::decode(bufferlist::iterator& bl)
   ::decode(who, bl);
   ::decode(stamp, bl);
   ::decode(seq, bl);
+  _calc_hash();
 }
 
 void LogEntryKey::dump(Formatter *f) const
index 5d00119d78055aecb977cc63883d3aa99e5544a9..7d6446a66ea45f4c5c634c8a71dd3586e8e6e673 100644 (file)
@@ -54,28 +54,44 @@ string clog_type_to_string(clog_type t);
 
 
 struct LogEntryKey {
+private:
+  uint64_t _hash;
+
+  void _calc_hash() {
+    hash<entity_inst_t> h;
+    _hash = seq + h(who);
+  }
+
   entity_inst_t who;
   utime_t stamp;
-  uint64_t seq;
+  uint64_t seq = 0;
+
+public:
+  LogEntryKey() {}
+  LogEntryKey(const entity_inst_t& w, utime_t t, uint64_t s)
+    : who(w), stamp(t), seq(s) {
+    _calc_hash();
+  }
 
-  LogEntryKey() : seq(0) {}
-  LogEntryKey(const entity_inst_t& w, utime_t t, uint64_t s) : who(w), stamp(t), seq(s) {}
+  uint64_t get_hash() const {
+    return _hash;
+  }
 
   void encode(bufferlist& bl, uint64_t features) const;
   void decode(bufferlist::iterator& bl);
   void dump(Formatter *f) const;
   static void generate_test_instances(list<LogEntryKey*>& o);
+
+  friend bool operator==(const LogEntryKey& l, const LogEntryKey& r) {
+    return l.who == r.who && l.stamp == r.stamp && l.seq == r.seq;
+  }
 };
 WRITE_CLASS_ENCODER_FEATURES(LogEntryKey)
 
-static inline bool operator==(const LogEntryKey& l, const LogEntryKey& r) {
-  return l.who == r.who && l.stamp == r.stamp && l.seq == r.seq;
-}
 namespace std {
   template<> struct hash<LogEntryKey> {
     size_t operator()(const LogEntryKey& r) const {
-      hash<entity_inst_t> h;
-      return r.seq + h(r.who);
+      return r.get_hash();
     }
   };
 } // namespace std