]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/LogEntry: make LogSummary::contains() efficient
authorSage Weil <sage@redhat.com>
Mon, 5 Jun 2017 19:31:05 +0000 (15:31 -0400)
committerSage Weil <sage@redhat.com>
Mon, 5 Jun 2017 19:31:05 +0000 (15:31 -0400)
A linear search here is dumb.  Use an unordered_set.

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

index d97b0480d70720969670451cecd051ad676bdd2f..52af1eb878d745abcfa2230b5bfbc50fc2b485e5 100644 (file)
@@ -250,6 +250,10 @@ void LogSummary::decode(bufferlist::iterator& bl)
   ::decode(version, bl);
   ::decode(tail, bl);
   DECODE_FINISH(bl);
+  keys.clear();
+  for (auto& p : tail) {
+    keys.insert(p.key());
+  }
 }
 
 void LogSummary::dump(Formatter *f) const
index 790b8662b131f0914fc3afb71d4ba49930444a67..5d00119d78055aecb977cc63883d3aa99e5544a9 100644 (file)
@@ -71,6 +71,14 @@ 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);
+    }
+  };
+} // namespace std
 
 struct LogEntry {
   entity_inst_t who;
@@ -97,24 +105,22 @@ WRITE_CLASS_ENCODER_FEATURES(LogEntry)
 struct LogSummary {
   version_t version;
   list<LogEntry> tail;
+  ceph::unordered_set<LogEntryKey> keys;
 
   LogSummary() : version(0) {}
 
   void add(const LogEntry& e) {
     tail.push_back(e);
+    keys.insert(tail.back().key());
   }
   void prune(size_t max) {
     while (tail.size() > max) {
+      keys.erase(tail.front().key());
       tail.pop_front();
     }
   }
   bool contains(const LogEntryKey& k) const {
-    for (list<LogEntry>::const_iterator p = tail.begin();
-        p != tail.end();
-        ++p)
-      if (p->key() == k)
-       return true;
-    return false;
+    return keys.count(k);
   }
 
   void encode(bufferlist& bl, uint64_t features) const;