From 419ab461726a65cca8400d4588c5ac088c166f92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Tue, 20 Jan 2026 14:09:16 +0100 Subject: [PATCH] Avoid needing std::string as cache keys in IconCache --- Common/UI/IconCache.cpp | 33 +++++++++++++++++++++------------ Common/UI/IconCache.h | 18 +++++++++--------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/Common/UI/IconCache.cpp b/Common/UI/IconCache.cpp index 3c8747d4e6..0dfb2615b9 100644 --- a/Common/UI/IconCache.cpp +++ b/Common/UI/IconCache.cpp @@ -196,7 +196,7 @@ void IconCache::Decimate(int64_t maxSize) { } } -bool IconCache::GetDimensions(const std::string &key, int *width, int *height) { +bool IconCache::GetDimensions(std::string_view key, int *width, int *height) { std::unique_lock lock(lock_); auto iter = cache_.find(key); if (iter == cache_.end()) { @@ -214,12 +214,12 @@ bool IconCache::GetDimensions(const std::string &key, int *width, int *height) { } } -bool IconCache::Contains(const std::string &key) { +bool IconCache::Contains(std::string_view key) { std::unique_lock lock(lock_); return cache_.find(key) != cache_.end(); } -bool IconCache::MarkPending(const std::string &key) { +bool IconCache::MarkPending(std::string_view key) { std::unique_lock lock(lock_); if (cache_.find(key) != cache_.end()) { return false; @@ -227,16 +227,21 @@ bool IconCache::MarkPending(const std::string &key) { if (pending_.find(key) != pending_.end()) { return false; } - pending_.insert(key); + pending_.emplace(key); return true; } -void IconCache::CancelPending(const std::string &key) { +void IconCache::CancelPending(std::string_view key) { std::unique_lock lock(lock_); - pending_.erase(key); + auto iter = pending_.find(key); + if (iter == pending_.end()) { + ERROR_LOG(Log::System, "IconCache: CancelPending called for non-pending key: %.*s", STR_VIEW(key)); + return; + } + pending_.erase(iter); } -bool IconCache::InsertIcon(const std::string &key, IconFormat format, std::string &&data) { +bool IconCache::InsertIcon(std::string_view key, IconFormat format, std::string &&data) { if (key.empty()) { return false; } @@ -248,23 +253,27 @@ bool IconCache::InsertIcon(const std::string &key, IconFormat format, std::strin } std::unique_lock lock(lock_); + if (cache_.find(key) != cache_.end()) { // Already have this entry. return false; } if (data.size() > 1024 * 512) { - WARN_LOG(Log::G3D, "Unusually large icon inserted in icon cache: %s (%d bytes)", key.c_str(), (int)data.size()); + WARN_LOG(Log::G3D, "Unusually large icon inserted in icon cache: %.*s (%d bytes)", STR_VIEW(key), (int)data.size()); } - pending_.erase(key); + auto iter = pending_.find(key); + if (iter != pending_.end()) { + pending_.erase(iter); + } double now = time_now_d(); cache_.emplace(key, Entry{ std::move(data), format, nullptr, now, now, false }); return true; } -Draw::Texture *IconCache::BindIconTexture(UIContext *context, const std::string &key) { +Draw::Texture *IconCache::BindIconTexture(UIContext *context, std::string_view key) { if (key.empty()) { return nullptr; } @@ -300,7 +309,7 @@ Draw::Texture *IconCache::BindIconTexture(UIContext *context, const std::string &height, &buffer); if (result != 1) { - ERROR_LOG(Log::G3D, "IconCache: Failed to load png (%d bytes) for key %s", (int)iter->second.data.size(), key.c_str()); + ERROR_LOG(Log::G3D, "IconCache: Failed to load png (%d bytes) for key %.*s", (int)iter->second.data.size(), STR_VIEW(key)); iter->second.badData = true; return nullptr; } @@ -319,7 +328,7 @@ Draw::Texture *IconCache::BindIconTexture(UIContext *context, const std::string iconDesc.mipLevels = 1; iconDesc.swizzle = Draw::TextureSwizzle::DEFAULT; iconDesc.generateMips = false; - iconDesc.tag = key.c_str(); + iconDesc.tag = "icon"; iconDesc.format = dataFormat; iconDesc.type = Draw::TextureType::LINEAR2D; diff --git a/Common/UI/IconCache.h b/Common/UI/IconCache.h index c93483a95b..e631f77ca6 100644 --- a/Common/UI/IconCache.h +++ b/Common/UI/IconCache.h @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include @@ -32,14 +32,14 @@ class IconCache { public: // NOTE: Don't store the returned texture. Only use it to look up dimensions or other properties, // instead call BindIconTexture every time you want to use it. - Draw::Texture *BindIconTexture(UIContext *context, const std::string &key); + Draw::Texture *BindIconTexture(UIContext *context, std::string_view key); // It's okay to call these from any thread. - bool MarkPending(const std::string &key); // returns false if already pending or loaded - void CancelPending(const std::string &key); - bool InsertIcon(const std::string &key, IconFormat format, std::string &&pngData); - bool GetDimensions(const std::string &key, int *width, int *height); - bool Contains(const std::string &key); + bool MarkPending(std::string_view key); // returns false if already pending or loaded + void CancelPending(std::string_view key); + bool InsertIcon(std::string_view key, IconFormat format, std::string &&pngData); + bool GetDimensions(std::string_view key, int *width, int *height); + bool Contains(std::string_view key); void SaveToFile(FILE *file); bool LoadFromFile(FILE *file); @@ -63,8 +63,8 @@ private: bool badData; }; - std::map cache_; - std::set pending_; + std::map> cache_; + std::set> pending_; std::mutex lock_;