Skip to content

Commit

Permalink
stock/{Map,Multi}: pass std::string_view as name
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Jan 21, 2025
1 parent 5ca7d05 commit e81fd93
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 32 deletions.
4 changes: 4 additions & 0 deletions src/stock/BasicStock.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ public:
return cls;
}

std::string_view GetNameView() const noexcept {
return name;
}

const char *GetName() const noexcept override {
return name.c_str();
}
Expand Down
17 changes: 6 additions & 11 deletions src/stock/MapStock.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "MapStock.hxx"
#include "util/djb_hash.hxx"
#include "util/DeleteDisposer.hxx"
#include "util/StringAPI.hxx"
#include "util/SpanCast.hxx"

void
StockMap::Item::OnDeferredEmpty() noexcept
Expand All @@ -15,20 +15,15 @@ StockMap::Item::OnDeferredEmpty() noexcept
}

inline size_t
StockMap::Item::Hash::operator()(const char *key) const noexcept
StockMap::Item::Hash::operator()(std::string_view key) const noexcept
{
assert(key != nullptr);

return djb_hash_string(key);
return djb_hash(AsBytes(key));
}

inline bool
StockMap::Item::Equal::operator()(const char *a, const char *b) const noexcept
StockMap::Item::Equal::operator()(std::string_view a, std::string_view b) const noexcept
{
assert(a != nullptr);
assert(b != nullptr);

return StringIsEqual(a, b);
return a == b;
}

StockMap::StockMap(EventLoop &_event_loop, StockClass &_cls,
Expand All @@ -53,7 +48,7 @@ StockMap::Erase(Item &item) noexcept
}

Stock &
StockMap::GetStock(const char *uri, const void *request) noexcept
StockMap::GetStock(std::string_view uri, const void *request) noexcept
{
auto [position, inserted] = map.insert_check(uri);
if (inserted) {
Expand Down
12 changes: 6 additions & 6 deletions src/stock/MapStock.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ class StockMap {
public:
struct Hash {
[[gnu::pure]]
size_t operator()(const char *key) const noexcept;
size_t operator()(std::string_view key) const noexcept;
};

struct Equal {
[[gnu::pure]]
bool operator()(const char *a, const char *b) const noexcept;
bool operator()(std::string_view a, std::string_view b) const noexcept;
};

struct GetKeyFunction {
[[gnu::pure]]
const char *operator()(const Item &item) const noexcept {
std::string_view operator()(const Item &item) const noexcept {
return item.GetName();
}
};
Expand Down Expand Up @@ -126,15 +126,15 @@ public:
}

[[gnu::pure]]
Stock &GetStock(const char *uri, const void *request) noexcept;
Stock &GetStock(std::string_view uri, const void *request) noexcept;

/**
* Set the "sticky" flag. Sticky stocks will not be deleted
* when they become empty.
*/
void SetSticky(Stock &stock, bool sticky) noexcept;

void Get(const char *uri, StockRequest &&request,
void Get(std::string_view uri, StockRequest &&request,
StockGetHandler &handler,
CancellablePointer &cancel_ptr) noexcept {
Stock &stock = GetStock(uri, request.get());
Expand All @@ -148,7 +148,7 @@ public:
*
* Throws exception on error.
*/
StockItem *GetNow(const char *uri, StockRequest &&request) {
StockItem *GetNow(std::string_view uri, StockRequest &&request) {
Stock &stock = GetStock(uri, request.get());
return stock.GetNow(std::move(request));
}
Expand Down
16 changes: 7 additions & 9 deletions src/stock/MultiStock.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "Item.hxx"
#include "util/djb_hash.hxx"
#include "util/DeleteDisposer.hxx"
#include "util/StringAPI.hxx"
#include "util/SpanCast.hxx"

#include <cassert>

Expand Down Expand Up @@ -585,17 +585,15 @@ MultiStock::MapItem::OnLeaseReleased(OuterItem &item) noexcept
}

inline std::size_t
MultiStock::MapItem::Hash::operator()(const char *key) const noexcept
MultiStock::MapItem::Hash::operator()(std::string_view key) const noexcept
{
assert(key != nullptr);

return djb_hash_string(key);
return djb_hash(AsBytes(key));
}

inline bool
MultiStock::MapItem::Equal::operator()(const char *a, const char *b) const noexcept
MultiStock::MapItem::Equal::operator()(std::string_view a, std::string_view b) const noexcept
{
return StringIsEqual(a, b);
return a == b;
}

MultiStock::MultiStock(EventLoop &_event_loop, StockClass &_outer_cls,
Expand Down Expand Up @@ -653,7 +651,7 @@ MultiStock::DiscardOldestIdle(std::size_t n_requested) noexcept
}

inline MultiStock::MapItem &
MultiStock::MakeMapItem(const char *uri, const void *request) noexcept
MultiStock::MakeMapItem(std::string_view uri, const void *request) noexcept
{
auto [i, inserted] = map.insert_check(uri);
if (inserted) {
Expand All @@ -675,7 +673,7 @@ MultiStock::MakeMapItem(const char *uri, const void *request) noexcept
}

void
MultiStock::Get(const char *uri, StockRequest request,
MultiStock::Get(std::string_view uri, StockRequest request,
std::size_t concurrency,
StockGetHandler &handler,
CancellablePointer &cancel_ptr) noexcept
Expand Down
12 changes: 6 additions & 6 deletions src/stock/MultiStock.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -306,18 +306,18 @@ class MultiStock {
public:
struct Hash {
[[gnu::pure]]
std::size_t operator()(const char *key) const noexcept;
std::size_t operator()(std::string_view key) const noexcept;
};

struct Equal {
[[gnu::pure]]
bool operator()(const char *a, const char *b) const noexcept;
bool operator()(std::string_view a, std::string_view b) const noexcept;
};

struct GetKey {
[[gnu::pure]]
const char *operator()(const MapItem &item) const noexcept {
return item.GetName();
std::string_view operator()(const MapItem &item) const noexcept {
return item.name;
}
};
};
Expand Down Expand Up @@ -401,12 +401,12 @@ public:
});
}

void Get(const char *uri, StockRequest request,
void Get(std::string_view uri, StockRequest request,
std::size_t concurrency,
StockGetHandler &handler,
CancellablePointer &cancel_ptr) noexcept;

private:
[[gnu::pure]]
MapItem &MakeMapItem(const char *uri, const void *request) noexcept;
MapItem &MakeMapItem(std::string_view uri, const void *request) noexcept;
};

0 comments on commit e81fd93

Please sign in to comment.