From 20b0954aaf54bc3189f18825b9ad2c618612ddd9 Mon Sep 17 00:00:00 2001 From: Alexander Borsuk Date: Thu, 8 Aug 2024 22:17:31 +0200 Subject: [PATCH] Avoid temporary std::string for brands searching Signed-off-by: Alexander Borsuk --- indexer/brands_holder.hpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/indexer/brands_holder.hpp b/indexer/brands_holder.hpp index f41463536a..877f75822e 100644 --- a/indexer/brands_holder.hpp +++ b/indexer/brands_holder.hpp @@ -40,7 +40,7 @@ public: std::set const & GetKeys() const { return m_keys; } - template void ForEachNameByKey(std::string const & key, FnT && fn) const + template void ForEachNameByKey(std::string_view key, FnT && fn) const { auto const it = m_keyToName.find(key); if (it == m_keyToName.end()) @@ -57,7 +57,17 @@ private: void LoadFromStream(std::istream & s); void AddBrand(Brand & brand, std::string const & key); - std::unordered_map> m_keyToName; + struct StringViewHash + { + using hash_type = std::hash; + using is_transparent = void; + + // std::size_t operator()(const char* str) const { return hash_type{}(str); } + std::size_t operator()(std::string_view str) const { return hash_type{}(str); } + std::size_t operator()(std::string const & str) const { return hash_type{}(str); } + }; + + std::unordered_map, StringViewHash, std::equal_to<>> m_keyToName; std::set m_keys; }; @@ -67,9 +77,8 @@ BrandsHolder const & GetDefaultBrands(); template void ForEachLocalizedBrands(std::string_view brand, FnT && fn) { bool processed = false; - /// @todo Remove temporary string with the new cpp standard. /// Localized brands are not working as expected now because we store raw names from OSM, not brand IDs. - GetDefaultBrands().ForEachNameByKey(std::string(brand), [&fn, &processed](auto const & name) + GetDefaultBrands().ForEachNameByKey(brand, [&fn, &processed](auto const & name) { fn(name); processed = true;