From f7945918c6ffeb2f1757b8f1e6c2103f95267513 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 18 Dec 2020 03:25:16 -0800 Subject: [PATCH] ircd::stats: Use vector for item iteration; add name convenience member. --- include/ircd/stats.h | 3 ++- ircd/stats.cc | 52 +++++++++++++++++++++++++++++--------------- modules/console.cc | 6 ++--- modules/stats.cc | 4 ++-- 4 files changed, 41 insertions(+), 24 deletions(-) diff --git a/include/ircd/stats.h b/include/ircd/stats.h index 54b199571..bcea3a824 100644 --- a/include/ircd/stats.h +++ b/include/ircd/stats.h @@ -55,7 +55,7 @@ namespace ircd::stats template<> struct item; extern const size_t NAME_MAX_LEN; - extern std::map *> items; + extern std::vector *> items; string_view string(const mutable_buffer &, const item &); std::ostream &operator<<(std::ostream &, const item &); @@ -78,6 +78,7 @@ struct ircd::stats::item { std::type_index type {typeid(void)}; json::strung feature; + json::string name; public: // Access features diff --git a/ircd/stats.cc b/ircd/stats.cc index 9f4f95d4d..9adc0b0e5 100644 --- a/ircd/stats.cc +++ b/ircd/stats.cc @@ -95,12 +95,11 @@ ircd::stats::item::item(const std::type_index &type, { opts } +,name +{ + this->operator[]("name") +} { - const json::string name - { - this->operator[]("name") - }; - if(unlikely(!name)) throw invalid { @@ -116,32 +115,49 @@ ircd::stats::item::item(const std::type_index &type, NAME_MAX_LEN }; - if(unlikely(!items.emplace(name, this).second)) + static const auto less + { + [](const auto &a, const auto &b) + { + return a->name < b->name; + } + }; + + const auto exists + { + std::binary_search(begin(items), end(items), this, less) + }; + + if(unlikely(exists)) throw invalid { "Stats item named '%s' already exists", name }; + + if(items.empty()) + items.reserve(4096); + + items.emplace_back(this); + std::sort(begin(items), end(items), less); } ircd::stats::item::~item() noexcept { - const json::string name + if(!name) + return; + + const auto eit { - this->operator[]("name") + std::remove_if(begin(items), end(items), [this] + (const auto &item) + { + return item->name == this->name; + }) }; - if(name) - { - const auto it - { - items.find(name) - }; - - assert(data(it->first) == data(name)); - items.erase(it); - } + items.erase(eit, end(items)); } ircd::string_view diff --git a/modules/console.cc b/modules/console.cc index f140c3305..606d7cd5b 100644 --- a/modules/console.cc +++ b/modules/console.cc @@ -1599,9 +1599,9 @@ console_cmd__stats(opt &out, const string_view &line) param[0] == "-a"? param[1]: param[0] }; - for(const auto &[name_, item] : stats::items) + for(const auto &item : stats::items) { - if(prefix && !startswith(name_, prefix)) + if(prefix && !startswith(item->name, prefix)) continue; assert(item); @@ -1615,7 +1615,7 @@ console_cmd__stats(opt &out, const string_view &line) const fmt::bsprintf<128> name { - "%s ", trunc(name_, name_width) + "%s ", trunc(item->name, name_width) }; out diff --git a/modules/stats.cc b/modules/stats.cc index 245a02bfd..4e93068c2 100644 --- a/modules/stats.cc +++ b/modules/stats.cc @@ -51,12 +51,12 @@ ircd::stats::get_stats(client &client, ircd::time() }; - for(const auto &[name_, item] : items) + for(const auto &item : items) { char buf[256], name[2][128], val[64]; const string_view _name { - replace(name[0], name_, '.', '_') + replace(name[0], item->name, '.', '_') }; const string_view line