[2/2] lazily allocate demangled hash

Message ID 1403278737-29257-3-git-send-email-tromey@redhat.com
State New, archived
Headers

Commit Message

Tom Tromey June 20, 2014, 3:38 p.m. UTC
  Currently, the minsym demangled hash table is always allocated in the
per-BFD, even when the objfile holds no symbols with mangled names.

This patch changes gdb to lazily allocate the demangled hash table.
This saves a little memory for each such objfile.

Built and regtested on x86-64 Fedora 20.

2014-06-20  Tom Tromey  <tromey@redhat.com>

	* minsyms.c (lookup_minimal_symbol, iterate_over_minimal_symbols):
	Handle case where demangled hash table is not allocated.
	(build_minimal_symbol_hash_tables): Lazily create demangled hash
	table.
	* objfiles.h (struct objfile_per_bfd_storage)
	<msymbol_demangled_hash>: Change type.
---
 gdb/ChangeLog  |  9 +++++++++
 gdb/minsyms.c  | 39 ++++++++++++++++++++++++++-------------
 gdb/objfiles.h |  5 +++--
 3 files changed, 38 insertions(+), 15 deletions(-)
  

Patch

diff --git a/gdb/minsyms.c b/gdb/minsyms.c
index e59b534..c7e7dfa 100644
--- a/gdb/minsyms.c
+++ b/gdb/minsyms.c
@@ -208,6 +208,8 @@  lookup_minimal_symbol (const char *name, const char *sfile,
 	      /* Select hash list according to pass.  */
 	      if (pass == 1)
 		msymbol = objfile->per_bfd->msymbol_hash[hash];
+	      else if (objfile->per_bfd->msymbol_demangled_hash == NULL)
+		msymbol = NULL;
 	      else
 		msymbol = objfile->per_bfd->msymbol_demangled_hash[dem_hash];
 
@@ -324,13 +326,16 @@  iterate_over_minimal_symbols (struct objfile *objf, const char *name,
     }
 
   /* The second pass is over the demangled table.  */
-  hash = msymbol_hash_iw (name) % MINIMAL_SYMBOL_HASH_SIZE;
-  iter = objf->per_bfd->msymbol_demangled_hash[hash];
-  while (iter)
+  if (objf->per_bfd->msymbol_demangled_hash != NULL)
     {
-      if (MSYMBOL_MATCHES_SEARCH_NAME (iter, name))
-	(*callback) (iter, user_data);
-      iter = iter->demangled_hash_next;
+      hash = msymbol_hash_iw (name) % MINIMAL_SYMBOL_HASH_SIZE;
+      iter = objf->per_bfd->msymbol_demangled_hash[hash];
+      while (iter)
+	{
+	  if (MSYMBOL_MATCHES_SEARCH_NAME (iter, name))
+	    (*callback) (iter, user_data);
+	  iter = iter->demangled_hash_next;
+	}
     }
 }
 
@@ -1147,11 +1152,11 @@  build_minimal_symbol_hash_tables (struct objfile *objfile)
   struct minimal_symbol *msym;
 
   /* Clear the hash tables.  */
-  for (i = 0; i < MINIMAL_SYMBOL_HASH_SIZE; i++)
-    {
-      objfile->per_bfd->msymbol_hash[i] = 0;
-      objfile->per_bfd->msymbol_demangled_hash[i] = 0;
-    }
+  memset (objfile->per_bfd->msymbol_hash, 0,
+	  MINIMAL_SYMBOL_HASH_SIZE * sizeof (struct minimal_symbol *));
+  if (objfile->per_bfd->msymbol_demangled_hash != NULL)
+    memset (objfile->per_bfd->msymbol_demangled_hash, 0,
+	    MINIMAL_SYMBOL_HASH_SIZE * sizeof (struct minimal_symbol *));
 
   /* Now, (re)insert the actual entries.  */
   for ((i = objfile->per_bfd->minimal_symbol_count,
@@ -1164,8 +1169,16 @@  build_minimal_symbol_hash_tables (struct objfile *objfile)
 
       msym->demangled_hash_next = 0;
       if (MSYMBOL_SEARCH_NAME (msym) != MSYMBOL_LINKAGE_NAME (msym))
-	add_minsym_to_demangled_hash_table (msym,
-                                            objfile->per_bfd->msymbol_demangled_hash);
+	{
+	  if (objfile->per_bfd->msymbol_demangled_hash == NULL)
+	    objfile->per_bfd->msymbol_demangled_hash
+	      = OBSTACK_CALLOC (&objfile->per_bfd->storage_obstack,
+				MINIMAL_SYMBOL_HASH_SIZE,
+				struct minimal_symbol *);
+
+	  add_minsym_to_demangled_hash_table (msym,
+					      objfile->per_bfd->msymbol_demangled_hash);
+	}
     }
 }
 
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index 57a94e1..c37d0c5 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -244,9 +244,10 @@  struct objfile_per_bfd_storage
   struct minimal_symbol *msymbol_hash[MINIMAL_SYMBOL_HASH_SIZE];
 
   /* This hash table is used to index the minimal symbols by their
-     demangled names.  */
+     demangled names.  This may be NULL if no minimal symbol has a
+     mangled name.  */
 
-  struct minimal_symbol *msymbol_demangled_hash[MINIMAL_SYMBOL_HASH_SIZE];
+  struct minimal_symbol **msymbol_demangled_hash;
 };
 
 /* Master structure for keeping track of each file from which