[RFA,1/2] Speed up dict_hash

Message ID 20171104161356.17565-2-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey Nov. 4, 2017, 4:13 p.m. UTC
  This speeds up dict_hash a bit, by moving the "TKB" check into the
switch in the loop.

For "gdb -nx -readnow -batch gdb", this improves the time from ~9.8s
before to ~8.5s afterward.

gdb/ChangeLog
2017-11-04  Tom Tromey  <tom@tromey.com>

	* dictionary.c (dict_hash): Move "TKB" check into the "switch".
---
 gdb/ChangeLog    |  4 ++++
 gdb/dictionary.c | 32 ++++++++++++++++----------------
 2 files changed, 20 insertions(+), 16 deletions(-)
  

Comments

Pedro Alves Nov. 8, 2017, 10:46 a.m. UTC | #1
On 11/04/2017 04:13 PM, Tom Tromey wrote:
> This speeds up dict_hash a bit, by moving the "TKB" check into the
> switch in the loop.
> 
> For "gdb -nx -readnow -batch gdb", this improves the time from ~9.8s
> before to ~8.5s afterward.

Nice!

> +	case 'T':
> +	  /* Ignore "TKB" suffixes.
> +
> +	     These are used by Ada for subprograms implementing a task body.
> +	     For instance for a task T inside package Pck, the name of the
> +	     subprogram implementing T's body is `pck__tTKB'.  We need to
> +	     ignore the "TKB" suffix because searches for this task body
> +	     subprogram are going to be performed using `pck__t' (the encoded
> +	     version of the natural name `pck.t').  */
> +	  if (strcmp (string, "TKB") == 0)
> +	    return hash;

This looks good to me.

OOC, did you check whether

  'if (strcmp (string + 1, "KB") == 0)'

or even:

  if (string[1] == 'K' && string[2] == 'B' && string[3] == '\0')

made a difference?

Maybe there aren't enough 'T's in symbols to matter, or the
compiler is already inlining that strcmp...

Thanks,
Pedro Alves
  
Tom Tromey Nov. 8, 2017, 6:44 p.m. UTC | #2
>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

>> For "gdb -nx -readnow -batch gdb", this improves the time from ~9.8s
>> before to ~8.5s afterward.

Pedro> Nice!

I rebuilt today and the baseline times were higher -- now it starts at
~14.1s for an unmodified gdb, down to ~12.2 with my patch.  I'm not sure
if this is due to some other patch, or if the additional time is because
I configured differently (I enabled guile, python, and all targets in
this most recent build).  If so, 4 seconds (comparing to the ~10 seconds
last time around) seems awfully expensive for those features...

Pedro> OOC, did you check whether
Pedro>   'if (strcmp (string + 1, "KB") == 0)'
Pedro> or even:
Pedro>   if (string[1] == 'K' && string[2] == 'B' && string[3] == '\0')
Pedro> made a difference?

I tried these and they didn't make a difference.

Tom
  

Patch

diff --git a/gdb/dictionary.c b/gdb/dictionary.c
index b2cfca28ab..702cd60c2a 100644
--- a/gdb/dictionary.c
+++ b/gdb/dictionary.c
@@ -796,17 +796,6 @@  dict_hash (const char *string0)
   hash = 0;
   while (*string)
     {
-      /* Ignore "TKB" suffixes.
-
-	 These are used by Ada for subprograms implementing a task body.
-	 For instance for a task T inside package Pck, the name of the
-	 subprogram implementing T's body is `pck__tTKB'.  We need to
-	 ignore the "TKB" suffix because searches for this task body
-	 subprogram are going to be performed using `pck__t' (the encoded
-	 version of the natural name `pck.t').  */
-      if (strcmp (string, "TKB") == 0)
-	return hash;
-
       switch (*string)
 	{
 	case '$':
@@ -828,14 +817,25 @@  dict_hash (const char *string0)
 		return hash;
 	      hash = 0;
 	      string += 2;
-	      break;
+	      continue;
 	    }
-	  /* FALL THROUGH */
-	default:
-	  hash = SYMBOL_HASH_NEXT (hash, *string);
-	  string += 1;
+	  break;
+	case 'T':
+	  /* Ignore "TKB" suffixes.
+
+	     These are used by Ada for subprograms implementing a task body.
+	     For instance for a task T inside package Pck, the name of the
+	     subprogram implementing T's body is `pck__tTKB'.  We need to
+	     ignore the "TKB" suffix because searches for this task body
+	     subprogram are going to be performed using `pck__t' (the encoded
+	     version of the natural name `pck.t').  */
+	  if (strcmp (string, "TKB") == 0)
+	    return hash;
 	  break;
 	}
+
+      hash = SYMBOL_HASH_NEXT (hash, *string);
+      string += 1;
     }
   return hash;
 }