From 49bd43e685e0e66753aa9ba1094a790ebfdeebf9 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 5 Feb 2023 18:46:20 -0800 Subject: [PATCH] ircd::util: Optimize instance_map w/ allocator::node. --- include/ircd/util/instance_map.h | 76 +++++++++++++++++++++++--------- ircd/resource.cc | 11 ++++- 2 files changed, 64 insertions(+), 23 deletions(-) diff --git a/include/ircd/util/instance_map.h b/include/ircd/util/instance_map.h index 8e5ee7f7f..3c01354f0 100644 --- a/include/ircd/util/instance_map.h +++ b/include/ircd/util/instance_map.h @@ -26,13 +26,24 @@ template struct ircd::util::instance_map { - static std::map map; + using value_type = std::pair; + using allocator_state_type = ircd::allocator::node; + using allocator_scope = typename allocator_state_type::scope; + using allocator_type = typename allocator_state_type::allocator; + using map_type = std::map; + using node_type = std::pair; + using iterator_type = typename map_type::iterator; + using const_iterator_type = typename map_type::const_iterator; + + static allocator_state_type allocator; + static map_type map; protected: - typename decltype(map)::iterator it; + node_type node; + iterator_type it; template - instance_map(const typename decltype(map)::const_iterator &hint, Key&&); + instance_map(const const_iterator_type &hint, Key&&); template instance_map(Key&&); @@ -51,6 +62,11 @@ template inline ircd::util::instance_map::instance_map(Key&& key) { + const allocator_scope alloca + { + map, this->node + }; + auto [it, ok] { map.emplace(std::forward(key), static_cast(this)) @@ -70,9 +86,14 @@ template template inline -ircd::util::instance_map::instance_map(const typename decltype(map)::const_iterator &hint, +ircd::util::instance_map::instance_map(const const_iterator_type &hint, Key&& key) { + const allocator_scope alloca + { + map, this->node + }; + auto [it, ok] { map.emplace_hint(hint, std::forward(key), static_cast(this)) @@ -93,16 +114,15 @@ template::instance_map(instance_map &&other) noexcept -:it { - std::move(other.it) -} -{ - if(it != end(map)) + const allocator_scope alloca { - it->second = static_cast(this); - other.it = end(map); - } + map, this->node + }; + + it = likely(other.it != end(map))? + map.emplace_hint(other.it, other.it->first, static_cast(this)): + end(map); } template inline ircd::util::instance_map::instance_map(const instance_map &other) -:it { - other.it != end(map)? + const allocator_scope alloca + { + map, this->node + }; + + it = likely(other.it != end(map))? map.emplace_hint(other.it, other.it->first, static_cast(this)): - end(map) + end(map); } -{} template::operator=(instance_map &&other) noexcept { this->~instance_map(); - it = std::move(other.it); - if(it != end(map)) - it->second = static_cast(this); - other.it = end(map); + const allocator_scope alloca + { + map, this->node + }; + + it = likely(other.it != end(map))? + map.emplace_hint(other.it, other.it->first, static_cast(this)): + end(map); + return *this; } @@ -141,7 +169,13 @@ inline ircd::util::instance_map & ircd::util::instance_map::operator=(const instance_map &other) { this->~instance_map(); - it = other.it != end(map)? + + const allocator_scope alloca + { + map, this->node + }; + + it = likely(other.it != end(map))? map.emplace_hint(other.it, other.it->first, static_cast(this)): end(map); diff --git a/ircd/resource.cc b/ircd/resource.cc index 8020e416e..9e46e1bff 100644 --- a/ircd/resource.cc +++ b/ircd/resource.cc @@ -15,10 +15,17 @@ ircd::resource::log }; template<> -decltype(ircd::util::instance_map::map) -ircd::util::instance_map::map +decltype(ircd::resource::allocator) +ircd::util::instance_map::allocator {}; +template<> +decltype(ircd::resource::map) +ircd::util::instance_map::map +{ + allocator +}; + ircd::resource & ircd::resource::find(const string_view &path_) {