input/cache/Manager: use IntrusiveHashSet instead of boost::intrusive::set

This commit is contained in:
Max Kellermann 2023-01-23 13:57:31 +01:00
parent 7e7cdf73b9
commit 5844242cfb
4 changed files with 42 additions and 28 deletions

View File

@ -23,8 +23,7 @@
#include "input/BufferingInputStream.hxx"
#include "thread/Mutex.hxx"
#include "util/IntrusiveList.hxx"
#include <boost/intrusive/set_hook.hpp>
#include "util/IntrusiveHashSet.hxx"
#include <string>
@ -40,7 +39,7 @@ class InputCacheLease;
class InputCacheItem final
: public BufferingInputStream,
public AutoUnlinkIntrusiveListHook,
public boost::intrusive::set_base_hook<boost::intrusive::link_mode<boost::intrusive::normal_link>>
public IntrusiveHashSetHook<>
{
const std::string uri;
@ -53,8 +52,8 @@ public:
explicit InputCacheItem(InputStreamPtr _input) noexcept;
~InputCacheItem() noexcept;
const char *GetUri() const noexcept {
return uri.c_str();
const std::string &GetUri() const noexcept {
return uri;
}
using BufferingInputStream::size;

View File

@ -27,25 +27,37 @@
#include <string.h>
inline bool
InputCacheManager::ItemCompare::operator()(const InputCacheItem &a,
const char *b) const noexcept
inline std::size_t
InputCacheManager::ItemHash::operator()(std::string_view uri) const noexcept
{
return strcmp(a.GetUri(), b) < 0;
return std::hash<std::string_view>{}(uri);
}
inline std::size_t
InputCacheManager::ItemHash::operator()(const InputCacheItem &item) const noexcept
{
return std::hash<std::string_view>{}(item.GetUri());
}
inline bool
InputCacheManager::ItemCompare::operator()(const char *a,
const InputCacheItem &b) const noexcept
InputCacheManager::ItemEqual::operator()(const InputCacheItem &a,
std::string_view b) const noexcept
{
return strcmp(a, b.GetUri()) < 0;
return a.GetUri() == b;
}
inline bool
InputCacheManager::ItemCompare::operator()(const InputCacheItem &a,
const InputCacheItem &b) const noexcept
InputCacheManager::ItemEqual::operator()(std::string_view a,
const InputCacheItem &b) const noexcept
{
return strcmp(a.GetUri(), b.GetUri()) < 0;
return a == b.GetUri();
}
inline bool
InputCacheManager::ItemEqual::operator()(const InputCacheItem &a,
const InputCacheItem &b) const noexcept
{
return a.GetUri() == b.GetUri();
}
InputCacheManager::InputCacheManager(const InputCacheConfig &config) noexcept
@ -97,8 +109,7 @@ InputCacheManager::Get(const char *uri, bool create)
if (!PathTraitsUTF8::IsAbsolute(uri))
return {};
auto iter = items_by_uri.find(uri, items_by_uri.key_comp());
if (iter != items_by_uri.end()) {
if (auto iter = items_by_uri.find(uri); iter != items_by_uri.end()) {
auto &item = *iter;
/* refresh */

View File

@ -21,10 +21,9 @@
#define MPD_INPUT_CACHE_MANAGER_HXX
#include "thread/Mutex.hxx"
#include "util/IntrusiveHashSet.hxx"
#include "util/IntrusiveList.hxx"
#include <boost/intrusive/set.hpp>
class InputStream;
class InputCacheItem;
class InputCacheLease;
@ -41,13 +40,21 @@ class InputCacheManager {
size_t total_size = 0;
struct ItemCompare {
struct ItemHash {
[[gnu::pure]]
bool operator()(const InputCacheItem &a,
const char *b) const noexcept;
std::size_t operator()(std::string_view uri) const noexcept;
[[gnu::pure]]
bool operator()(const char *a,
std::size_t operator()(const InputCacheItem &item) const noexcept;
};
struct ItemEqual {
[[gnu::pure]]
bool operator()(const InputCacheItem &a,
std::string_view b) const noexcept;
[[gnu::pure]]
bool operator()(std::string_view a,
const InputCacheItem &b) const noexcept;
[[gnu::pure]]
@ -58,10 +65,7 @@ class InputCacheManager {
IntrusiveList<InputCacheItem> items_by_time;
using UriMap =
boost::intrusive::set<InputCacheItem,
boost::intrusive::base_hook<boost::intrusive::set_base_hook<boost::intrusive::link_mode<boost::intrusive::normal_link>>>,
boost::intrusive::compare<ItemCompare>,
boost::intrusive::constant_time_size<false>>;
IntrusiveHashSet<InputCacheItem, 127, ItemHash, ItemEqual>;
UriMap items_by_uri;

View File

@ -21,7 +21,7 @@
CacheInputStream::CacheInputStream(InputCacheLease _lease,
Mutex &_mutex) noexcept
:InputStream(_lease->GetUri(), _mutex),
:InputStream(_lease->GetUri().c_str(), _mutex),
InputCacheLease(std::move(_lease))
{
const auto &i = GetCacheItem();