public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] gdbsupport: add gdb::string_view_hash
@ 2023-01-05 19:40 Simon Marchi
  0 siblings, 0 replies; only message in thread
From: Simon Marchi @ 2023-01-05 19:40 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=1a8605a8c79b0c4ebc71f5691e36a1338d407837

commit 1a8605a8c79b0c4ebc71f5691e36a1338d407837
Author: Simon Marchi <simon.marchi@efficios.com>
Date:   Thu Oct 20 13:05:19 2022 -0400

    gdbsupport: add gdb::string_view_hash
    
    Add the string_view_hash type, which will be useful to be able to use
    gdb::string_view as std::unordered_map keys.
    
    Use it in gdb/symtab.c, to exercise it.
    
    Change-Id: Id69a466ab19a9f6620b5df8a2dd29b5cddd94c00
    Approved-By: Andrew Burgess <aburgess@redhat.com>

Diff:
---
 gdb/symtab.c              |  2 +-
 gdbsupport/common-utils.h | 17 +++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/gdb/symtab.c b/gdb/symtab.c
index f2b1a14e006..b3445133c8c 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -812,7 +812,7 @@ hash_demangled_name_entry (const void *data)
   const struct demangled_name_entry *e
     = (const struct demangled_name_entry *) data;
 
-  return fast_hash (e->mangled.data (), e->mangled.length ());
+  return gdb::string_view_hash () (e->mangled);
 }
 
 /* Equality function for the demangled name hash.  */
diff --git a/gdbsupport/common-utils.h b/gdbsupport/common-utils.h
index 31ab1a6cec8..97dcb9fa8ce 100644
--- a/gdbsupport/common-utils.h
+++ b/gdbsupport/common-utils.h
@@ -209,4 +209,21 @@ fast_hash (const void *ptr, size_t len, unsigned int start_value = 0)
 #endif
 }
 
+namespace gdb
+{
+
+/* Hash type for gdb::string_view.
+
+   Even after we switch to C++17 and dump our string_view implementation, we
+   might want to keep this hash implementation if it's faster than std::hash
+   for std::string_view.  */
+
+struct string_view_hash
+{
+  std::size_t operator() (gdb::string_view view) const
+  {  return fast_hash (view.data (), view.length ()); }
+};
+
+} /* namespace gdb */
+
 #endif /* COMMON_COMMON_UTILS_H */

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-01-05 19:40 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-05 19:40 [binutils-gdb] gdbsupport: add gdb::string_view_hash Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).