From: simon.marchi@polymtl.ca
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH 3/4] gdbsupport: add gdb::unordered_string_map
Date: Fri, 13 Feb 2026 00:38:50 -0500 [thread overview]
Message-ID: <20260213053912.3520505-3-simon.marchi@polymtl.ca> (raw)
In-Reply-To: <20260213053912.3520505-1-simon.marchi@polymtl.ca>
From: Simon Marchi <simon.marchi@efficios.com>
It occurred to me that when we use a
gdb::unordered_map<std::string, T>
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
---
gdbsupport/unordered_map.h | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/gdbsupport/unordered_map.h b/gdbsupport/unordered_map.h
index b615f2422f62..e0b97764d0b3 100644
--- a/gdbsupport/unordered_map.h
+++ b/gdbsupport/unordered_map.h
@@ -32,6 +32,40 @@ using unordered_map
<Key, T, Hash, KeyEqual, std::allocator<std::pair<Key, T>>,
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<std::string_view> () (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<typename T>
+using unordered_string_map
+ = gdb::unordered_map<std::string, T,
+ detail::unordered_string_map_hash,
+ detail::unordered_string_map_eq>;
+
} /* namespace gdb */
#endif /* GDBSUPPORT_UNORDERED_MAP_H */
--
2.53.0
next prev parent reply other threads:[~2026-02-13 5:40 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-13 5:38 [PATCH 1/4] gdb/elfread: add debug output for GNU ifunc resolution simon.marchi
2026-02-13 5:38 ` [PATCH 2/4] gdb/elfread: replace ifunc htab_t with gdb::unordered_map simon.marchi
2026-02-13 5:38 ` simon.marchi [this message]
2026-02-13 5:38 ` [PATCH 4/4] gdb: use gdb::unordered_string_map throughout simon.marchi
2026-02-13 8:30 ` [PATCH 1/4] gdb/elfread: add debug output for GNU ifunc resolution Eli Zaretskii
2026-02-13 17:20 ` Simon Marchi
2026-02-13 18:27 ` Kevin Buettner
2026-02-13 19:10 ` Simon Marchi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260213053912.3520505-3-simon.marchi@polymtl.ca \
--to=simon.marchi@polymtl.ca \
--cc=gdb-patches@sourceware.org \
--cc=simon.marchi@efficios.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox