tag/Pool: use std::string_view instead of StringView

This commit is contained in:
Max Kellermann 2022-06-29 16:49:31 +02:00
parent 4cb5c3782b
commit 2da847dd30
2 changed files with 12 additions and 13 deletions

View File

@ -21,7 +21,6 @@
#include "Item.hxx" #include "Item.hxx"
#include "util/Cast.hxx" #include "util/Cast.hxx"
#include "util/VarSize.hxx" #include "util/VarSize.hxx"
#include "util/StringView.hxx"
#include <cassert> #include <cassert>
#include <cstdint> #include <cstdint>
@ -42,24 +41,23 @@ struct TagPoolSlot {
static constexpr unsigned MAX_REF = std::numeric_limits<decltype(ref)>::max(); static constexpr unsigned MAX_REF = std::numeric_limits<decltype(ref)>::max();
TagPoolSlot(TagPoolSlot *_next, TagType type, TagPoolSlot(TagPoolSlot *_next, TagType type,
StringView value) noexcept std::string_view value) noexcept
:next(_next) { :next(_next) {
item.type = type; item.type = type;
memcpy(item.value, value.data, value.size); *std::copy(value.begin(), value.end(), item.value) = 0;
item.value[value.size] = 0;
} }
static TagPoolSlot *Create(TagPoolSlot *_next, TagType type, static TagPoolSlot *Create(TagPoolSlot *_next, TagType type,
StringView value) noexcept; std::string_view value) noexcept;
}; };
TagPoolSlot * TagPoolSlot *
TagPoolSlot::Create(TagPoolSlot *_next, TagType type, TagPoolSlot::Create(TagPoolSlot *_next, TagType type,
StringView value) noexcept std::string_view value) noexcept
{ {
TagPoolSlot *dummy; TagPoolSlot *dummy;
return NewVarSize<TagPoolSlot>(sizeof(dummy->item.value), return NewVarSize<TagPoolSlot>(sizeof(dummy->item.value),
value.size + 1, value.size() + 1,
_next, type, _next, type,
value); value);
} }
@ -67,7 +65,7 @@ TagPoolSlot::Create(TagPoolSlot *_next, TagType type,
static TagPoolSlot *slots[NUM_SLOTS]; static TagPoolSlot *slots[NUM_SLOTS];
static inline unsigned static inline unsigned
calc_hash(TagType type, StringView p) noexcept calc_hash(TagType type, std::string_view p) noexcept
{ {
unsigned hash = 5381; unsigned hash = 5381;
@ -97,7 +95,7 @@ tag_item_to_slot(TagItem *item) noexcept
} }
static inline TagPoolSlot ** static inline TagPoolSlot **
tag_value_slot_p(TagType type, StringView value) noexcept tag_value_slot_p(TagType type, std::string_view value) noexcept
{ {
return &slots[calc_hash(type, value) % NUM_SLOTS]; return &slots[calc_hash(type, value) % NUM_SLOTS];
} }
@ -109,12 +107,12 @@ tag_value_slot_p(TagType type, const char *value) noexcept
} }
TagItem * TagItem *
tag_pool_get_item(TagType type, StringView value) noexcept tag_pool_get_item(TagType type, std::string_view value) noexcept
{ {
auto slot_p = tag_value_slot_p(type, value); auto slot_p = tag_value_slot_p(type, value);
for (auto slot = *slot_p; slot != nullptr; slot = slot->next) { for (auto slot = *slot_p; slot != nullptr; slot = slot->next) {
if (slot->item.type == type && if (slot->item.type == type &&
value.Equals(slot->item.value) && value == slot->item.value &&
slot->ref < TagPoolSlot::MAX_REF) { slot->ref < TagPoolSlot::MAX_REF) {
assert(slot->ref > 0); assert(slot->ref > 0);
++slot->ref; ++slot->ref;

View File

@ -23,14 +23,15 @@
#include "Type.h" #include "Type.h"
#include "thread/Mutex.hxx" #include "thread/Mutex.hxx"
#include <string_view>
extern Mutex tag_pool_lock; extern Mutex tag_pool_lock;
struct TagItem; struct TagItem;
struct StringView;
[[nodiscard]] [[nodiscard]]
TagItem * TagItem *
tag_pool_get_item(TagType type, StringView value) noexcept; tag_pool_get_item(TagType type, std::string_view value) noexcept;
[[nodiscard]] [[nodiscard]]
TagItem * TagItem *