Patchwork [review] Don't make an extra copy + allocation of the demangled name

login
register
mail settings
Submitter Simon Marchi (Code Review)
Date Oct. 17, 2019, 10:47 p.m.
Message ID <gerrit.1571352433000.Ie6ad50e1e1e73509f55d756f0a437897bb93e3b0@gnutoolchain-gerrit.osci.io>
Download mbox | patch
Permalink /patch/35103/
State New
Headers show

Comments

Simon Marchi (Code Review) - Oct. 17, 2019, 10:47 p.m.
Christian Biesinger has uploaded a new change for review.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132
......................................................................

Don't make an extra copy + allocation of the demangled name

We can just keep around the malloc()-ed name we got from bfd and free
it later.

gdb/ChangeLog:

2019-10-02  Christian Biesinger  <cbiesinger@google.com>

	* symtab.c (struct demangled_name_entry): Change demangled name
	to a char*, now that we don't allocate it as part of the struct
	anymore.
	(free_demangled_name_entry): New function.
	(create_demangled_names_hash): Pass free function to htab_create_alloc.
	(symbol_set_names): No longer obstack allocate + copy the demangled
	name, just store the allocated name from bfd.

Change-Id: Ie6ad50e1e1e73509f55d756f0a437897bb93e3b0
---
M gdb/symtab.c
1 file changed, 17 insertions(+), 18 deletions(-)
Simon Marchi (Code Review) - Oct. 21, 2019, 11:19 p.m.
Luis Machado has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132
......................................................................


Patch Set 1: Code-Review+1

(2 comments)

Although the obstack manipulation bits are a bit odd with the offsets and potentially less than ideal documentation, this looks reasonable to me. Only a couple nits in formatting.

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132/1/gdb/symtab.c 
File gdb/symtab.c:

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132/1/gdb/symtab.c@718 
PS1, Line 718:   char* demangled;
Usual style is "char *demangled;" i think. Although there is one offender in symtab.c already.


https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132/1/gdb/symtab.c@887 
PS1, Line 887: 	 
Something odd here? Whitespace?
Simon Marchi (Code Review) - Oct. 22, 2019, 4:32 a.m.
Simon Marchi has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132
......................................................................


Patch Set 1:

(1 comment)

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132/1/gdb/symtab.c 
File gdb/symtab.c:

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132/1/gdb/symtab.c@718 
PS1, Line 718:   char* demangled;
Would it make sense to rebase that patch over your other series (especially https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/37/5).  This field could become a gdb::unique_xmalloc_ptr.  Since you already call the destructor in the other patch, this will get cleaned up automatically.
Simon Marchi (Code Review) - Oct. 22, 2019, 7:04 p.m.
Christian Biesinger has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132
......................................................................


Uploaded patch set 2.

(3 comments)

rebased

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132/1/gdb/symtab.c 
File gdb/symtab.c:

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132/1/gdb/symtab.c@718 
PS1, Line 718:   char* demangled;
> Usual style is "char *demangled;" i think. Although there is one offender in symtab.c already.
I rebased & followed Simon's suggestion, so this comment is moot now.


https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132/1/gdb/symtab.c@718 
PS1, Line 718:   char* demangled;
> Would it make sense to rebase that patch over your other series (especially https://gnutoolchain-ger […]
Done


https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132/1/gdb/symtab.c@887 
PS1, Line 887: 	 
> Something odd here? Whitespace?
Yeah, there's whitespace on this line. This was already pre-existing but I removed it now.
Simon Marchi (Code Review) - Oct. 22, 2019, 7:27 p.m.
Simon Marchi has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132
......................................................................


Patch Set 2: Code-Review+1

LGTM, but I'd like if Tom could give this a quick look, since he has more experience with the symbol handling stuff.
Simon Marchi (Code Review) - Oct. 25, 2019, 6:23 p.m.
Tom Tromey has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132
......................................................................


Patch Set 3:

(1 comment)

Thank you.  This looks good, but I think free_demangled_name_entry needs to
be updated to destroy the new member.

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132/3/gdb/symtab.c 
File gdb/symtab.c:

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132/3/gdb/symtab.c@914 
PS3, Line 914: 	  char *mangled_ptr = reinterpret_cast<char*> (*slot + 1);
Formatting should be `<char *>` here (missing a space).
Simon Marchi (Code Review) - Oct. 25, 2019, 6:24 p.m.
Tom Tromey has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132
......................................................................


Patch Set 3: Code-Review+2

> Patch Set 3:
> 
> (1 comment)
> 
> Thank you.  This looks good, but I think free_demangled_name_entry needs to
> be updated to destroy the new member.

Oh, never mind -- I was misreading that function!
I see now it calls the full object's destructor, which will
do the right thing.

This patch is ok with the formatting nit fixed.  No need to re-review.
Simon Marchi (Code Review) - Oct. 25, 2019, 7:10 p.m.
Christian Biesinger has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132
......................................................................


Patch Set 3:

(2 comments)

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132/3/gdb/symtab.c 
File gdb/symtab.c:

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132/3/gdb/symtab.c@897 
PS3, Line 897:       if (!copy_name)
I reverted the change on this line; I'm no longer sure it's correct.


https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/132/3/gdb/symtab.c@914 
PS3, Line 914: 	  char *mangled_ptr = reinterpret_cast<char*> (*slot + 1);
> Formatting should be `<char *>` here (missing a space).
Thanks, done.

Patch

diff --git a/gdb/symtab.c b/gdb/symtab.c
index 8a551f1..a185f92 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -715,7 +715,7 @@ 
 {
   const char *mangled;
   ENUM_BITFIELD(language) language : LANGUAGE_BITS;
-  char demangled[1];
+  char* demangled;
 };
 
 /* Hash function for the demangled name hash.  */
@@ -742,6 +742,15 @@ 
   return strcmp (da->mangled, db->mangled) == 0;
 }
 
+static void
+free_demangled_name_entry (void *data)
+{
+  struct demangled_name_entry *e
+    = (struct demangled_name_entry *) data;
+
+  xfree (e->demangled);
+}
+
 /* Create the hash table used for demangled names.  Each hash entry is
    a pair of strings; one for the mangled name and one for the demangled
    name.  The entry is hashed via just the mangled name.  */
@@ -756,7 +765,7 @@ 
 
   per_bfd->demangled_names_hash.reset (htab_create_alloc
     (256, hash_demangled_name_entry, eq_demangled_name_entry,
-     NULL, xcalloc, xfree));
+     free_demangled_name_entry, xcalloc, xfree));
 }
 
 /* Try to determine the demangled name for a symbol, based on the
@@ -869,8 +878,6 @@ 
     {
       char *demangled_name_ptr
 	= symbol_find_demangled_name (gsymbol, linkage_name_copy);
-      gdb::unique_xmalloc_ptr<char> demangled_name (demangled_name_ptr);
-      int demangled_len = demangled_name ? strlen (demangled_name.get ()) : 0;
 
       /* Suppose we have demangled_name==NULL, copy_name==0, and
 	 linkage_name_copy==linkage_name.  In this case, we already have the
@@ -886,39 +893,31 @@ 
 	  *slot
 	    = ((struct demangled_name_entry *)
 	       obstack_alloc (&per_bfd->storage_obstack,
-			      offsetof (struct demangled_name_entry, demangled)
-			      + demangled_len + 1));
+			      sizeof (demangled_name_entry)));
 	  (*slot)->mangled = linkage_name;
 	}
       else
 	{
-	  char *mangled_ptr;
-
 	  /* If we must copy the mangled name, put it directly after
-	     the demangled name so we can have a single
+	     the struct so we can have a single
 	     allocation.  */
 	  *slot
 	    = ((struct demangled_name_entry *)
 	       obstack_alloc (&per_bfd->storage_obstack,
-			      offsetof (struct demangled_name_entry, demangled)
-			      + len + demangled_len + 2));
-	  mangled_ptr = &((*slot)->demangled[demangled_len + 1]);
+			      sizeof (demangled_name_entry) + len + 1));
+	  char *mangled_ptr = reinterpret_cast<char*> (*slot + 1);
 	  strcpy (mangled_ptr, linkage_name_copy);
 	  (*slot)->mangled = mangled_ptr;
 	}
+      (*slot)->demangled = demangled_name_ptr;
       (*slot)->language = gsymbol->language;
-
-      if (demangled_name != NULL)
-	strcpy ((*slot)->demangled, demangled_name.get ());
-      else
-	(*slot)->demangled[0] = '\0';
     }
   else if (gsymbol->language == language_unknown
 	   || gsymbol->language == language_auto)
     gsymbol->language = (*slot)->language;
 
   gsymbol->name = (*slot)->mangled;
-  if ((*slot)->demangled[0] != '\0')
+  if ((*slot)->demangled != nullptr)
     symbol_set_demangled_name (gsymbol, (*slot)->demangled,
 			       &per_bfd->storage_obstack);
   else