From cb1f57c5c3af4e2ece83afbb38a146eb8b966526 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Tue, 6 Jun 2017 14:50:46 -0400 Subject: [PATCH] common/LogEntry: make LogEntryKey opaque and cache its hash value 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 --- src/common/LogEntry.cc | 1 + src/common/LogEntry.h | 32 ++++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/common/LogEntry.cc b/src/common/LogEntry.cc index 52af1eb878d74..40bdbea2b3ac7 100644 --- a/src/common/LogEntry.cc +++ b/src/common/LogEntry.cc @@ -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 diff --git a/src/common/LogEntry.h b/src/common/LogEntry.h index 5d00119d78055..7d6446a66ea45 100644 --- a/src/common/LogEntry.h +++ b/src/common/LogEntry.h @@ -54,28 +54,44 @@ string clog_type_to_string(clog_type t); struct LogEntryKey { +private: + uint64_t _hash; + + void _calc_hash() { + hash 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& 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 { size_t operator()(const LogEntryKey& r) const { - hash h; - return r.seq + h(r.who); + return r.get_hash(); } }; } // namespace std -- 2.39.5