From: Simon Marchi Date: Mon, 9 Feb 2026 19:17:53 +0000 (-0500) Subject: gdbsupport: add gdb::unordered_string_map X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=68075f6e8670335be6199a175c60999eca47c20f;p=thirdparty%2Fbinutils-gdb.git gdbsupport: add gdb::unordered_string_map It occurred to me that when we use a gdb::unordered_map we construct a temporary std::string for each lookup from a `const char *`, possibly doing dynamic allocation. This is really unnecessary, because it's easy to compare an existing `const char *` (or std::string_view) with an std::string. This can therefore be avoided by having transparent hash and eq types. Because this is a common enough case, add it to gdbsupport/unordered_map.h, so it can easily be reused. Define the gdb::unordered_string_map type, which uses hash and eq types that work on std::string_view. Both `const char *` and `std::string` can implicitly be converted to std::string_view, so this should be sufficient. Change-Id: Id93448d831696d25472f13c15212f13712ad8492 Approved-by: Kevin Buettner --- diff --git a/gdbsupport/unordered_map.h b/gdbsupport/unordered_map.h index b615f2422f6..e0b97764d0b 100644 --- a/gdbsupport/unordered_map.h +++ b/gdbsupport/unordered_map.h @@ -32,6 +32,40 @@ using unordered_map >, ankerl::unordered_dense::bucket_type::standard>; +/* An unordered_map with std::string keys that supports transparent + lookup from std::string_view, avoiding the construction of temporary + std::string objects during lookups. std::string_view is implicitly + constructible from `const char *` and `std::string`, so it covers those + too. */ + +namespace detail +{ + +struct unordered_string_map_hash +{ + using is_transparent = void; + using is_avalanching = void; + + std::uint64_t operator() (std::string_view sv) const noexcept + { return ankerl::unordered_dense::hash () (sv); } +}; + +struct unordered_string_map_eq +{ + using is_transparent = void; + + bool operator() (std::string_view lhs, std::string_view rhs) const noexcept + { return lhs == rhs; } +}; + +} /* namespace detail */ + +template +using unordered_string_map + = gdb::unordered_map; + } /* namespace gdb */ #endif /* GDBSUPPORT_UNORDERED_MAP_H */