This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix slow and non-deterministic behavior of isspace() and tolower()


I was getting 8% and 6% cpu usage in tolower() and isspace(),
respectively, waiting for a breakpoint on ppc64el.

Also, gdb doesn't want non-deterministic behavior here.

v2: do not touch C namespace
v3: Turns out there are two places using these in performance-critical
parts.
    Follow GNU coding standards.

Signed-off-by: Shawn Landden <shawn@git.icu>
Co-Author: Pedro Alves <palves@redhat.com>
Mailing-list: https://sourceware.org/ml/gdb-patches/2019-06/threads.html#00187
---
 gdb/common/common-utils.c |  6 +++---
 gdb/common/common-utils.h | 22 ++++++++++++++++++++++
 gdb/utils.c               |  8 ++++----
 3 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/gdb/common/common-utils.c b/gdb/common/common-utils.c
index ae2dd9db2b..8215f505e8 100644
--- a/gdb/common/common-utils.c
+++ b/gdb/common/common-utils.c
@@ -338,7 +338,7 @@ skip_spaces (char *chp)
 {
   if (chp == NULL)
     return NULL;
-  while (*chp && isspace (*chp))
+  while (*chp && gdb_isspace (*chp))
     chp++;
   return chp;
 }
@@ -350,7 +350,7 @@ skip_spaces (const char *chp)
 {
   if (chp == NULL)
     return NULL;
-  while (*chp && isspace (*chp))
+  while (*chp && gdb_isspace (*chp))
     chp++;
   return chp;
 }
@@ -362,7 +362,7 @@ skip_to_space (const char *chp)
 {
   if (chp == NULL)
     return NULL;
-  while (*chp && !isspace (*chp))
+  while (*chp && !gdb_isspace (*chp))
     chp++;
   return chp;
 }
diff --git a/gdb/common/common-utils.h b/gdb/common/common-utils.h
index 2320318de7..5e6e0afb9c 100644
--- a/gdb/common/common-utils.h
+++ b/gdb/common/common-utils.h
@@ -146,4 +146,26 @@ in_inclusive_range (T value, T low, T high)
   return value >= low && value <= high;
 }
 
+static inline
+int
+gdb_isspace (int c)
+{
+  return c == ' ' || (unsigned)c - '\t' < 5;
+}
+
+static inline
+int
+gdb_isupper (int c)
+{
+  return (unsigned)c - 'A' < 26;
+}
+
+static inline
+int
+gdb_tolower (int c)
+{
+  if (isupper(c)) return c | 32;
+  return c;
+}
+
 #endif
diff --git a/gdb/utils.c b/gdb/utils.c
index c531748fe4..f92e8f5346 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -2572,16 +2572,16 @@ strcmp_iw_ordered (const char *string1, const char *string2)
 
       while (*string1 != '\0' && *string2 != '\0')
 	{
-	  while (isspace (*string1))
+	  while (gdb_isspace (*string1))
 	    string1++;
-	  while (isspace (*string2))
+	  while (gdb_isspace (*string2))
 	    string2++;
 
 	  switch (case_pass)
 	  {
 	    case case_sensitive_off:
-	      c1 = tolower ((unsigned char) *string1);
-	      c2 = tolower ((unsigned char) *string2);
+	      c1 = gdb_tolower ((unsigned char) *string1);
+	      c2 = gdb_tolower ((unsigned char) *string2);
 	      break;
 	    case case_sensitive_on:
 	      c1 = *string1;
-- 
2.20.1


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]