Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [review] Don't make an extra copy + allocation of the demangled name
@ 2019-10-17 22:47 Christian Biesinger (Code Review)
  2019-10-21 23:19 ` Luis Machado (Code Review)
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: Christian Biesinger (Code Review) @ 2019-10-17 22:47 UTC (permalink / raw)
  To: gdb-patches; +Cc: Christian Biesinger

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(-)



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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [review] Don't make an extra copy + allocation of the demangled name
  2019-10-17 22:47 [review] Don't make an extra copy + allocation of the demangled name Christian Biesinger (Code Review)
@ 2019-10-21 23:19 ` Luis Machado (Code Review)
  2019-10-22  4:32 ` Simon Marchi (Code Review)
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Luis Machado (Code Review) @ 2019-10-21 23:19 UTC (permalink / raw)
  To: Christian Biesinger, gdb-patches

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?




^ permalink raw reply	[flat|nested] 12+ messages in thread

* [review] Don't make an extra copy + allocation of the demangled name
  2019-10-17 22:47 [review] Don't make an extra copy + allocation of the demangled name Christian Biesinger (Code Review)
  2019-10-21 23:19 ` Luis Machado (Code Review)
@ 2019-10-22  4:32 ` Simon Marchi (Code Review)
  2019-10-22 19:04 ` [review v2] " Christian Biesinger (Code Review)
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Simon Marchi (Code Review) @ 2019-10-22  4:32 UTC (permalink / raw)
  To: Christian Biesinger, gdb-patches; +Cc: Luis Machado

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.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* [review v2] Don't make an extra copy + allocation of the demangled name
  2019-10-17 22:47 [review] Don't make an extra copy + allocation of the demangled name Christian Biesinger (Code Review)
                   ` (2 preceding siblings ...)
  2019-10-22 19:04 ` [review v2] " Christian Biesinger (Code Review)
@ 2019-10-22 19:04 ` Christian Biesinger (Code Review)
  2019-10-22 19:27 ` Simon Marchi (Code Review)
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Christian Biesinger (Code Review) @ 2019-10-22 19:04 UTC (permalink / raw)
  To: Christian Biesinger, gdb-patches; +Cc: Simon Marchi, Luis Machado

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.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* [review v2] Don't make an extra copy + allocation of the demangled name
  2019-10-17 22:47 [review] Don't make an extra copy + allocation of the demangled name Christian Biesinger (Code Review)
  2019-10-21 23:19 ` Luis Machado (Code Review)
  2019-10-22  4:32 ` Simon Marchi (Code Review)
@ 2019-10-22 19:04 ` Christian Biesinger (Code Review)
  2019-10-22 19:04 ` Christian Biesinger (Code Review)
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Christian Biesinger (Code Review) @ 2019-10-22 19:04 UTC (permalink / raw)
  To: Christian Biesinger, Luis Machado, gdb-patches; +Cc: Simon Marchi

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, 13 insertions(+), 25 deletions(-)



diff --git a/gdb/symtab.c b/gdb/symtab.c
index ed55cec..5d0db62 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -720,7 +720,7 @@
 
   gdb::string_view mangled;
   ENUM_BITFIELD(language) language : LANGUAGE_BITS;
-  char demangled[1];
+  gdb::unique_xmalloc_ptr<char> demangled;
 };
 
 /* Hash function for the demangled name hash.  */
@@ -877,64 +877,52 @@
   if (*slot == NULL
       /* A C version of the symbol may have already snuck into the table.
 	 This happens to, e.g., main.init (__go_init_main).  Cope.  */
-      || (gsymbol->language == language_go
-	  && (*slot)->demangled[0] == '\0'))
+      || (gsymbol->language == language_go && (*slot)->demangled == nullptr))
     {
-      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;
+      gdb::unique_xmalloc_ptr<char> demangled_name_ptr
+	(symbol_find_demangled_name (gsymbol, linkage_name_copy));
 
       /* Suppose we have demangled_name==NULL, copy_name==0, and
 	 linkage_name_copy==linkage_name.  In this case, we already have the
 	 mangled name saved, and we don't have a demangled name.  So,
 	 you might think we could save a little space by not recording
 	 this in the hash table at all.
-	 
+
 	 It turns out that it is actually important to still save such
 	 an entry in the hash table, because storing this name gives
 	 us better bcache hit rates for partial symbols.  */
-      if (!copy_name && linkage_name_copy == linkage_name)
+      if (!copy_name)
 	{
 	  *slot
 	    = ((struct demangled_name_entry *)
 	       obstack_alloc (&per_bfd->storage_obstack,
-			      offsetof (struct demangled_name_entry, demangled)
-			      + demangled_len + 1));
+			      sizeof (demangled_name_entry)));
 	  new (*slot) demangled_name_entry
 	    (gdb::string_view (linkage_name, len));
 	}
       else
 	{
-	  char *mangled_ptr;
-
 	  /* If we must copy the mangled name, put it directly after
-	     the demangled name so we can have a single
-	     allocation.  */
+	     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);
 	  new (*slot) demangled_name_entry
 	    (gdb::string_view (mangled_ptr, len));
 	}
+      (*slot)->demangled = std::move (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.data ();
-  if ((*slot)->demangled[0] != '\0')
-    symbol_set_demangled_name (gsymbol, (*slot)->demangled,
+  if ((*slot)->demangled != nullptr)
+    symbol_set_demangled_name (gsymbol, (*slot)->demangled.get (),
 			       &per_bfd->storage_obstack);
   else
     symbol_set_demangled_name (gsymbol, NULL, &per_bfd->storage_obstack);


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [review v2] Don't make an extra copy + allocation of the demangled name
  2019-10-17 22:47 [review] Don't make an extra copy + allocation of the demangled name Christian Biesinger (Code Review)
                   ` (3 preceding siblings ...)
  2019-10-22 19:04 ` Christian Biesinger (Code Review)
@ 2019-10-22 19:27 ` Simon Marchi (Code Review)
  2019-10-23 16:51 ` [review v3] " Christian Biesinger (Code Review)
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Simon Marchi (Code Review) @ 2019-10-22 19:27 UTC (permalink / raw)
  To: Christian Biesinger, gdb-patches; +Cc: Tom Tromey, Luis Machado

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.



^ permalink raw reply	[flat|nested] 12+ messages in thread

* [review v3] Don't make an extra copy + allocation of the demangled name
  2019-10-17 22:47 [review] Don't make an extra copy + allocation of the demangled name Christian Biesinger (Code Review)
                   ` (4 preceding siblings ...)
  2019-10-22 19:27 ` Simon Marchi (Code Review)
@ 2019-10-23 16:51 ` Christian Biesinger (Code Review)
  2019-10-25 18:23 ` Tom Tromey (Code Review)
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Christian Biesinger (Code Review) @ 2019-10-23 16:51 UTC (permalink / raw)
  To: Christian Biesinger, Luis Machado, Tom Tromey, Simon Marchi, gdb-patches

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 unique_xmalloc_ptr<char>, now that we don't allocate it as
	part of the struct anymore.
	(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, 13 insertions(+), 25 deletions(-)



diff --git a/gdb/symtab.c b/gdb/symtab.c
index 160a4c0..8553fc9 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -723,7 +723,7 @@
 
   gdb::string_view mangled;
   enum language language;
-  char demangled[1];
+  gdb::unique_xmalloc_ptr<char> demangled;
 };
 
 /* Hash function for the demangled name hash.  */
@@ -880,64 +880,52 @@
   if (*slot == NULL
       /* A C version of the symbol may have already snuck into the table.
 	 This happens to, e.g., main.init (__go_init_main).  Cope.  */
-      || (gsymbol->language == language_go
-	  && (*slot)->demangled[0] == '\0'))
+      || (gsymbol->language == language_go && (*slot)->demangled == nullptr))
     {
-      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;
+      gdb::unique_xmalloc_ptr<char> demangled_name_ptr
+	(symbol_find_demangled_name (gsymbol, linkage_name_copy));
 
       /* Suppose we have demangled_name==NULL, copy_name==0, and
 	 linkage_name_copy==linkage_name.  In this case, we already have the
 	 mangled name saved, and we don't have a demangled name.  So,
 	 you might think we could save a little space by not recording
 	 this in the hash table at all.
-	 
+
 	 It turns out that it is actually important to still save such
 	 an entry in the hash table, because storing this name gives
 	 us better bcache hit rates for partial symbols.  */
-      if (!copy_name && linkage_name_copy == linkage_name)
+      if (!copy_name)
 	{
 	  *slot
 	    = ((struct demangled_name_entry *)
 	       obstack_alloc (&per_bfd->storage_obstack,
-			      offsetof (struct demangled_name_entry, demangled)
-			      + demangled_len + 1));
+			      sizeof (demangled_name_entry)));
 	  new (*slot) demangled_name_entry
 	    (gdb::string_view (linkage_name, len));
 	}
       else
 	{
-	  char *mangled_ptr;
-
 	  /* If we must copy the mangled name, put it directly after
-	     the demangled name so we can have a single
-	     allocation.  */
+	     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);
 	  new (*slot) demangled_name_entry
 	    (gdb::string_view (mangled_ptr, len));
 	}
+      (*slot)->demangled = std::move (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.data ();
-  if ((*slot)->demangled[0] != '\0')
-    symbol_set_demangled_name (gsymbol, (*slot)->demangled,
+  if ((*slot)->demangled != nullptr)
+    symbol_set_demangled_name (gsymbol, (*slot)->demangled.get (),
 			       &per_bfd->storage_obstack);
   else
     symbol_set_demangled_name (gsymbol, NULL, &per_bfd->storage_obstack);


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [review v3] Don't make an extra copy + allocation of the demangled name
  2019-10-17 22:47 [review] Don't make an extra copy + allocation of the demangled name Christian Biesinger (Code Review)
                   ` (5 preceding siblings ...)
  2019-10-23 16:51 ` [review v3] " Christian Biesinger (Code Review)
@ 2019-10-25 18:23 ` Tom Tromey (Code Review)
  2019-10-25 18:24 ` Tom Tromey (Code Review)
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Tom Tromey (Code Review) @ 2019-10-25 18:23 UTC (permalink / raw)
  To: Christian Biesinger, gdb-patches; +Cc: Simon Marchi, Luis Machado

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).



-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ie6ad50e1e1e73509f55d756f0a437897bb93e3b0
Gerrit-Change-Number: 132
Gerrit-PatchSet: 3
Gerrit-Owner: Christian Biesinger <cbiesinger@google.com>
Gerrit-Reviewer: Luis Machado <luis.machado@linaro.org>
Gerrit-Reviewer: Simon Marchi <simon.marchi@polymtl.ca>
Gerrit-Reviewer: Tom Tromey <tromey@sourceware.org>
Gerrit-Comment-Date: Fri, 25 Oct 2019 18:23:15 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [review v3] Don't make an extra copy + allocation of the demangled name
  2019-10-17 22:47 [review] Don't make an extra copy + allocation of the demangled name Christian Biesinger (Code Review)
                   ` (6 preceding siblings ...)
  2019-10-25 18:23 ` Tom Tromey (Code Review)
@ 2019-10-25 18:24 ` Tom Tromey (Code Review)
  2019-10-25 19:10 ` [pushed] " Sourceware to Gerrit sync (Code Review)
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Tom Tromey (Code Review) @ 2019-10-25 18:24 UTC (permalink / raw)
  To: Christian Biesinger, gdb-patches; +Cc: Simon Marchi, Luis Machado

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.


-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ie6ad50e1e1e73509f55d756f0a437897bb93e3b0
Gerrit-Change-Number: 132
Gerrit-PatchSet: 3
Gerrit-Owner: Christian Biesinger <cbiesinger@google.com>
Gerrit-Reviewer: Luis Machado <luis.machado@linaro.org>
Gerrit-Reviewer: Simon Marchi <simon.marchi@polymtl.ca>
Gerrit-Reviewer: Tom Tromey <tromey@sourceware.org>
Gerrit-Comment-Date: Fri, 25 Oct 2019 18:24:46 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [review v3] Don't make an extra copy + allocation of the demangled name
  2019-10-17 22:47 [review] Don't make an extra copy + allocation of the demangled name Christian Biesinger (Code Review)
                   ` (9 preceding siblings ...)
  2019-10-25 19:10 ` Sourceware to Gerrit sync (Code Review)
@ 2019-10-25 19:10 ` Christian Biesinger (Code Review)
  10 siblings, 0 replies; 12+ messages in thread
From: Christian Biesinger (Code Review) @ 2019-10-25 19:10 UTC (permalink / raw)
  To: Christian Biesinger, gdb-patches; +Cc: Tom Tromey, Simon Marchi, Luis Machado

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.



-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ie6ad50e1e1e73509f55d756f0a437897bb93e3b0
Gerrit-Change-Number: 132
Gerrit-PatchSet: 3
Gerrit-Owner: Christian Biesinger <cbiesinger@google.com>
Gerrit-Reviewer: Christian Biesinger <cbiesinger@google.com>
Gerrit-Reviewer: Luis Machado <luis.machado@linaro.org>
Gerrit-Reviewer: Simon Marchi <simon.marchi@polymtl.ca>
Gerrit-Reviewer: Tom Tromey <tromey@sourceware.org>
Gerrit-Comment-Date: Fri, 25 Oct 2019 19:10:10 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Tom Tromey <tromey@sourceware.org>
Gerrit-MessageType: comment


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [pushed] Don't make an extra copy + allocation of the demangled name
  2019-10-17 22:47 [review] Don't make an extra copy + allocation of the demangled name Christian Biesinger (Code Review)
                   ` (7 preceding siblings ...)
  2019-10-25 18:24 ` Tom Tromey (Code Review)
@ 2019-10-25 19:10 ` Sourceware to Gerrit sync (Code Review)
  2019-10-25 19:10 ` Sourceware to Gerrit sync (Code Review)
  2019-10-25 19:10 ` [review v3] " Christian Biesinger (Code Review)
  10 siblings, 0 replies; 12+ messages in thread
From: Sourceware to Gerrit sync (Code Review) @ 2019-10-25 19:10 UTC (permalink / raw)
  To: Christian Biesinger, gdb-patches; +Cc: Tom Tromey, Simon Marchi, Luis Machado

Sourceware to Gerrit sync has submitted this change.

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-25  Christian Biesinger  <cbiesinger@google.com>

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

Change-Id: Ie6ad50e1e1e73509f55d756f0a437897bb93e3b0
---
M gdb/ChangeLog
M gdb/symtab.c
2 files changed, 21 insertions(+), 25 deletions(-)


diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 84fb9aa..55c4647 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,11 @@
+2019-10-25  Christian Biesinger  <cbiesinger@google.com>
+
+	* symtab.c (struct demangled_name_entry): Change demangled name
+	to a unique_xmalloc_ptr<char>, now that we don't allocate it as
+	part of the struct anymore.
+	(symbol_set_names): No longer obstack allocate + copy the demangled
+	name, just store the allocated name from bfd.
+
 2019-10-25  Tom Tromey  <tromey@adacore.com>
 
 	* dwarf2-frame.c (dwarf2_cie_table): Now a typedef.
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 160a4c0..adf9e08 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -723,7 +723,7 @@
 
   gdb::string_view mangled;
   enum language language;
-  char demangled[1];
+  gdb::unique_xmalloc_ptr<char> demangled;
 };
 
 /* Hash function for the demangled name hash.  */
@@ -839,7 +839,7 @@
     {
       /* In Ada, we do the symbol lookups using the mangled name, so
          we can save some space by not storing the demangled name.  */
-      if (!copy_name)
+      if (!copy_name && linkage_name_copy == linkage_name)
 	gsymbol->name = linkage_name;
       else
 	{
@@ -880,20 +880,17 @@
   if (*slot == NULL
       /* A C version of the symbol may have already snuck into the table.
 	 This happens to, e.g., main.init (__go_init_main).  Cope.  */
-      || (gsymbol->language == language_go
-	  && (*slot)->demangled[0] == '\0'))
+      || (gsymbol->language == language_go && (*slot)->demangled == nullptr))
     {
-      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;
+      gdb::unique_xmalloc_ptr<char> demangled_name_ptr
+	(symbol_find_demangled_name (gsymbol, linkage_name_copy));
 
       /* Suppose we have demangled_name==NULL, copy_name==0, and
 	 linkage_name_copy==linkage_name.  In this case, we already have the
 	 mangled name saved, and we don't have a demangled name.  So,
 	 you might think we could save a little space by not recording
 	 this in the hash table at all.
-	 
+
 	 It turns out that it is actually important to still save such
 	 an entry in the hash table, because storing this name gives
 	 us better bcache hit rates for partial symbols.  */
@@ -902,42 +899,33 @@
 	  *slot
 	    = ((struct demangled_name_entry *)
 	       obstack_alloc (&per_bfd->storage_obstack,
-			      offsetof (struct demangled_name_entry, demangled)
-			      + demangled_len + 1));
+			      sizeof (demangled_name_entry)));
 	  new (*slot) demangled_name_entry
 	    (gdb::string_view (linkage_name, len));
 	}
       else
 	{
-	  char *mangled_ptr;
-
 	  /* If we must copy the mangled name, put it directly after
-	     the demangled name so we can have a single
-	     allocation.  */
+	     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);
 	  new (*slot) demangled_name_entry
 	    (gdb::string_view (mangled_ptr, len));
 	}
+      (*slot)->demangled = std::move (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.data ();
-  if ((*slot)->demangled[0] != '\0')
-    symbol_set_demangled_name (gsymbol, (*slot)->demangled,
+  if ((*slot)->demangled != nullptr)
+    symbol_set_demangled_name (gsymbol, (*slot)->demangled.get (),
 			       &per_bfd->storage_obstack);
   else
     symbol_set_demangled_name (gsymbol, NULL, &per_bfd->storage_obstack);

-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ie6ad50e1e1e73509f55d756f0a437897bb93e3b0
Gerrit-Change-Number: 132
Gerrit-PatchSet: 4
Gerrit-Owner: Christian Biesinger <cbiesinger@google.com>
Gerrit-Reviewer: Luis Machado <luis.machado@linaro.org>
Gerrit-Reviewer: Simon Marchi <simon.marchi@polymtl.ca>
Gerrit-Reviewer: Tom Tromey <tromey@sourceware.org>
Gerrit-MessageType: merged


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [pushed] Don't make an extra copy + allocation of the demangled name
  2019-10-17 22:47 [review] Don't make an extra copy + allocation of the demangled name Christian Biesinger (Code Review)
                   ` (8 preceding siblings ...)
  2019-10-25 19:10 ` [pushed] " Sourceware to Gerrit sync (Code Review)
@ 2019-10-25 19:10 ` Sourceware to Gerrit sync (Code Review)
  2019-10-25 19:10 ` [review v3] " Christian Biesinger (Code Review)
  10 siblings, 0 replies; 12+ messages in thread
From: Sourceware to Gerrit sync (Code Review) @ 2019-10-25 19:10 UTC (permalink / raw)
  To: Christian Biesinger, Luis Machado, Tom Tromey, Simon Marchi, gdb-patches

The original change was created by Christian Biesinger.

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-25  Christian Biesinger  <cbiesinger@google.com>

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

Change-Id: Ie6ad50e1e1e73509f55d756f0a437897bb93e3b0
---
M gdb/ChangeLog
M gdb/symtab.c
2 files changed, 21 insertions(+), 25 deletions(-)



diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 84fb9aa..55c4647 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,11 @@
+2019-10-25  Christian Biesinger  <cbiesinger@google.com>
+
+	* symtab.c (struct demangled_name_entry): Change demangled name
+	to a unique_xmalloc_ptr<char>, now that we don't allocate it as
+	part of the struct anymore.
+	(symbol_set_names): No longer obstack allocate + copy the demangled
+	name, just store the allocated name from bfd.
+
 2019-10-25  Tom Tromey  <tromey@adacore.com>
 
 	* dwarf2-frame.c (dwarf2_cie_table): Now a typedef.
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 160a4c0..adf9e08 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -723,7 +723,7 @@
 
   gdb::string_view mangled;
   enum language language;
-  char demangled[1];
+  gdb::unique_xmalloc_ptr<char> demangled;
 };
 
 /* Hash function for the demangled name hash.  */
@@ -839,7 +839,7 @@
     {
       /* In Ada, we do the symbol lookups using the mangled name, so
          we can save some space by not storing the demangled name.  */
-      if (!copy_name)
+      if (!copy_name && linkage_name_copy == linkage_name)
 	gsymbol->name = linkage_name;
       else
 	{
@@ -880,20 +880,17 @@
   if (*slot == NULL
       /* A C version of the symbol may have already snuck into the table.
 	 This happens to, e.g., main.init (__go_init_main).  Cope.  */
-      || (gsymbol->language == language_go
-	  && (*slot)->demangled[0] == '\0'))
+      || (gsymbol->language == language_go && (*slot)->demangled == nullptr))
     {
-      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;
+      gdb::unique_xmalloc_ptr<char> demangled_name_ptr
+	(symbol_find_demangled_name (gsymbol, linkage_name_copy));
 
       /* Suppose we have demangled_name==NULL, copy_name==0, and
 	 linkage_name_copy==linkage_name.  In this case, we already have the
 	 mangled name saved, and we don't have a demangled name.  So,
 	 you might think we could save a little space by not recording
 	 this in the hash table at all.
-	 
+
 	 It turns out that it is actually important to still save such
 	 an entry in the hash table, because storing this name gives
 	 us better bcache hit rates for partial symbols.  */
@@ -902,42 +899,33 @@
 	  *slot
 	    = ((struct demangled_name_entry *)
 	       obstack_alloc (&per_bfd->storage_obstack,
-			      offsetof (struct demangled_name_entry, demangled)
-			      + demangled_len + 1));
+			      sizeof (demangled_name_entry)));
 	  new (*slot) demangled_name_entry
 	    (gdb::string_view (linkage_name, len));
 	}
       else
 	{
-	  char *mangled_ptr;
-
 	  /* If we must copy the mangled name, put it directly after
-	     the demangled name so we can have a single
-	     allocation.  */
+	     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);
 	  new (*slot) demangled_name_entry
 	    (gdb::string_view (mangled_ptr, len));
 	}
+      (*slot)->demangled = std::move (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.data ();
-  if ((*slot)->demangled[0] != '\0')
-    symbol_set_demangled_name (gsymbol, (*slot)->demangled,
+  if ((*slot)->demangled != nullptr)
+    symbol_set_demangled_name (gsymbol, (*slot)->demangled.get (),
 			       &per_bfd->storage_obstack);
   else
     symbol_set_demangled_name (gsymbol, NULL, &per_bfd->storage_obstack);

-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ie6ad50e1e1e73509f55d756f0a437897bb93e3b0
Gerrit-Change-Number: 132
Gerrit-PatchSet: 4
Gerrit-Owner: Christian Biesinger <cbiesinger@google.com>
Gerrit-Reviewer: Luis Machado <luis.machado@linaro.org>
Gerrit-Reviewer: Simon Marchi <simon.marchi@polymtl.ca>
Gerrit-Reviewer: Tom Tromey <tromey@sourceware.org>
Gerrit-MessageType: newpatchset


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2019-10-25 19:10 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-17 22:47 [review] Don't make an extra copy + allocation of the demangled name Christian Biesinger (Code Review)
2019-10-21 23:19 ` Luis Machado (Code Review)
2019-10-22  4:32 ` Simon Marchi (Code Review)
2019-10-22 19:04 ` [review v2] " Christian Biesinger (Code Review)
2019-10-22 19:04 ` Christian Biesinger (Code Review)
2019-10-22 19:27 ` Simon Marchi (Code Review)
2019-10-23 16:51 ` [review v3] " Christian Biesinger (Code Review)
2019-10-25 18:23 ` Tom Tromey (Code Review)
2019-10-25 18:24 ` Tom Tromey (Code Review)
2019-10-25 19:10 ` [pushed] " Sourceware to Gerrit sync (Code Review)
2019-10-25 19:10 ` Sourceware to Gerrit sync (Code Review)
2019-10-25 19:10 ` [review v3] " Christian Biesinger (Code Review)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox