diff mbox

[1/2] Accelerate iter_match_first_hashed 1.8x

Message ID 20141020214438.GB22011@host2.jankratochvil.net
State New
Headers show

Commit Message

Jan Kratochvil Oct. 20, 2014, 9:44 p.m. UTC
Hi,

very simple caching.  dict_hash() is being called again and again for the same
string.

#0  in skip_spaces_const (chp=0x7fffb10f9bb6 "ts<char>, std::allocator<char> >::npos") at ./cli/cli-utils.c:244
#1  in msymbol_hash_iw (string=0x7fffb10f9bb6 "ts<char>, std::allocator<char> >::npos") at minsyms.c:89
#2  in dict_hash ( string0=0x7fffb10f9b90 "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::npos") at dictionary.c:840
#3  in iter_match_first_hashed (dict=0x105a7840, name=0x7fffb10f9b90 "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::npos", compare=0x8b82f8 <strcmp_iw>, iterator=0x7fffb10f9970) at dictionary.c:659
#4  in dict_iter_match_first (dict=0x105a7840, name=0x7fffb10f9b90 "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::npos", compare=0x8b82f8 <strcmp_iw>, iterator=0x7fffb10f9970) at dictionary.c:555
#5  in dict_iter_name_first (dict=0x105a7840, name=0x7fffb10f9b90 "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::npos", iterator=0x7fffb10f9970) at dictionary.c:541
#6  in block_iter_name_step (iterator=0x7fffb10f9960, name=0x7fffb10f9b90 "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::npos", first=1) at block.c:580
#7  in block_iter_name_first (block=0x10593e10, name=0x7fffb10f9b90 "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::npos", iterator=0x7fffb10f9960) at block.c:609
#8  in lookup_block_symbol (block=0x10593e10, name=0x7fffb10f9b90 "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::npos", domain=VAR_DOMAIN) at symtab.c:2062
#9  in lookup_symbol_aux_objfile (objfile=0x466f870, block_index=0, name=0x7fffb10f9b90 "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::npos", domain=VAR_DOMAIN) at symtab.c:1664
#10 in lookup_symbol_global_iterator_cb (objfile=0x466f870, cb_data=0x7fffb10f9ad0) at symtab.c:1868


Maybe it could get cached at the caller but:
 * We would need to pass the hash value along the whole {dict,block}_iter_*
   call chain.
 * The DICT_VECTOR virtualization would get violated as dict_linear*_vector do
   not use any hash values.


Jan
gdb/
2014-10-20  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* dictionary.c (iter_match_first_hashed): Provide state cache for
	hash_index from NAME.
diff mbox

Patch

diff --git a/gdb/dictionary.c b/gdb/dictionary.c
index 055c87e..90bcd6d 100644
--- a/gdb/dictionary.c
+++ b/gdb/dictionary.c
@@ -656,9 +656,26 @@  iter_match_first_hashed (const struct dictionary *dict, const char *name,
 			 symbol_compare_ftype *compare,
 			 struct dict_iterator *iterator)
 {
-  unsigned int hash_index = dict_hash (name) % DICT_HASHED_NBUCKETS (dict);
+  unsigned int hash_index;
   struct symbol *sym;
 
+  /* Cache HASH_INDEX.  */
+  {
+    static const char *name_ptr_cached;
+    static char *name_content_cached;
+    static unsigned int dict_hash_cached;
+
+    if (name_ptr_cached != name || strcmp (name_content_cached, name) != 0)
+      {
+	dict_hash_cached = dict_hash (name);
+	name_ptr_cached = name;
+	xfree (name_content_cached);
+	name_content_cached = xstrdup (name);
+      }
+    hash_index = dict_hash_cached;
+  }
+  hash_index %= DICT_HASHED_NBUCKETS (dict);
+
   DICT_ITERATOR_DICT (iterator) = dict;
 
   /* Loop through the symbols in the given bucket, breaking when SYM