diff --git a/core/scollectd.cc b/core/scollectd.cc index c25d544b09..35b9069b81 100644 --- a/core/scollectd.cc +++ b/core/scollectd.cc @@ -12,6 +12,7 @@ #include #include #include +#include #include "scollectd.hh" #include "core/shared_ptr.hh" @@ -154,6 +155,8 @@ private: buffer_type _buf; mark_type _pos; + std::unordered_map _cache; + cpwriter() : _pos(_buf.begin()) { } @@ -171,6 +174,7 @@ private: } void clear() { reset(_buf.begin()); + _cache.clear(); } const char * data() const { return &_buf.at(0); @@ -211,6 +215,14 @@ private: write(s); // include \0 return *this; } + cpwriter & put_cached(part_type type, const std::string & s) { + auto & cached = _cache[uint16_t(type)]; + if (cached != s) { + put(type, s); + cached = s; + } + return *this; + } template typename std::enable_if::value, cpwriter &>::type put( part_type type, T & t) { @@ -237,23 +249,18 @@ private: std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()).count(); - put(part_type::Host, host); + put_cached(part_type::Host, host); // TODO: we're only sending hi-res time stamps. // Is this a problem? put(part_type::TimeHr, ts); - put(part_type::Plugin, id.plugin()); + put_cached(part_type::Plugin, id.plugin()); // Optional - if (!id.plugin_instance().empty()) { - put(part_type::PluginInst, - id.plugin_instance() == per_cpu_plugin_instance ? - std::to_string(engine._id) : - id.plugin_instance()); - } - put(part_type::Type, id.type()); + put_cached(part_type::PluginInst, + id.plugin_instance() == per_cpu_plugin_instance ? + std::to_string(engine._id) : id.plugin_instance()); + put_cached(part_type::Type, id.type()); // Optional - if (!id.type_instance().empty()) { - put(part_type::TypeInst, id.type_instance()); - } + put_cached(part_type::TypeInst, id.type_instance()); return *this; } cpwriter & put(const std::string & host, @@ -298,6 +305,8 @@ private: auto & values = std::get(*ctxt); auto & out = std::get(*ctxt); + out.clear(); + /* process potential de-regs already. if we did a send, someone might have de-regged * a value in the set meanwhile */