0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-12 13:01:07 +01:00

ircd::util: Optimize instance_map w/ allocator::node.

This commit is contained in:
Jason Volk 2023-02-05 18:46:20 -08:00
parent 4fe55722c9
commit 49bd43e685
2 changed files with 64 additions and 23 deletions

View file

@ -26,13 +26,24 @@ template<class K,
class C>
struct ircd::util::instance_map
{
static std::map<K, T *, C> map;
using value_type = std::pair<const K, T *>;
using allocator_state_type = ircd::allocator::node<value_type>;
using allocator_scope = typename allocator_state_type::scope;
using allocator_type = typename allocator_state_type::allocator;
using map_type = std::map<K, T *, C, allocator_type>;
using node_type = std::pair<typename map_type::node_type, value_type>;
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<class Key>
instance_map(const typename decltype(map)::const_iterator &hint, Key&&);
instance_map(const const_iterator_type &hint, Key&&);
template<class Key>
instance_map(Key&&);
@ -51,6 +62,11 @@ template<class Key>
inline
ircd::util::instance_map<K, T, C>::instance_map(Key&& key)
{
const allocator_scope alloca
{
map, this->node
};
auto [it, ok]
{
map.emplace(std::forward<Key>(key), static_cast<T *>(this))
@ -70,9 +86,14 @@ template<class K,
class C>
template<class Key>
inline
ircd::util::instance_map<K, T, C>::instance_map(const typename decltype(map)::const_iterator &hint,
ircd::util::instance_map<K, T, C>::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>(key), static_cast<T *>(this))
@ -93,16 +114,15 @@ template<class K,
inline
ircd::util::instance_map<K, T, C>::instance_map(instance_map &&other)
noexcept
:it
{
std::move(other.it)
}
const allocator_scope alloca
{
if(it != end(map))
{
it->second = static_cast<T *>(this);
other.it = end(map);
}
map, this->node
};
it = likely(other.it != end(map))?
map.emplace_hint(other.it, other.it->first, static_cast<T *>(this)):
end(map);
}
template<class K,
@ -110,13 +130,16 @@ template<class K,
class C>
inline
ircd::util::instance_map<K, T, C>::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<T *>(this)):
end(map)
end(map);
}
{}
template<class K,
class T,
@ -126,11 +149,16 @@ ircd::util::instance_map<K, T, C>::operator=(instance_map &&other)
noexcept
{
this->~instance_map();
it = std::move(other.it);
if(it != end(map))
it->second = static_cast<T *>(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<T *>(this)):
end(map);
return *this;
}
@ -141,7 +169,13 @@ inline ircd::util::instance_map<K, T, C> &
ircd::util::instance_map<K, T, C>::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<T *>(this)):
end(map);

View file

@ -15,10 +15,17 @@ ircd::resource::log
};
template<>
decltype(ircd::util::instance_map<ircd::string_view, ircd::resource, ircd::iless>::map)
ircd::util::instance_map<ircd::string_view, ircd::resource, ircd::iless>::map
decltype(ircd::resource::allocator)
ircd::util::instance_map<ircd::string_view, ircd::resource, ircd::iless>::allocator
{};
template<>
decltype(ircd::resource::map)
ircd::util::instance_map<ircd::string_view, ircd::resource, ircd::iless>::map
{
allocator
};
ircd::resource &
ircd::resource::find(const string_view &path_)
{