Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Shawn Landden <shawn@git.icu>
To: gdb-patches@sourceware.org
Cc: Pedro Alves <palves@redhat.com>,
	jhb@freebsd.org,	eliz@gnu.org,	Shawn Landden <shawn@git.icu>
Subject: [PATCH] Fix slow and non-deterministic behavior of isspace() and tolower()
Date: Fri, 21 Jun 2019 19:20:00 -0000	[thread overview]
Message-ID: <20190621191959.24450-1-shawn@git.icu> (raw)
In-Reply-To: <20190610213017.2021-1-shawn@git.icu>

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


  parent reply	other threads:[~2019-06-21 19:20 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-09 15:17 Shawn Landden
2019-06-09 15:42 ` Eli Zaretskii
2019-06-09 15:51   ` Shawn Landden
2019-06-10 21:14 ` John Baldwin
2019-06-10 21:30 ` [PATCH v2] " Shawn Landden
2019-06-21 17:35   ` Pedro Alves
2019-06-21 17:59     ` Shawn Landden
2019-06-21 18:08       ` Pedro Alves
2019-06-21 19:20   ` Shawn Landden [this message]
2019-06-21 20:12     ` [PATCH] " Pedro Alves
2019-06-21 20:30       ` Shawn Landden
2019-06-21 21:14         ` Pedro Alves

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190621191959.24450-1-shawn@git.icu \
    --to=shawn@git.icu \
    --cc=eliz@gnu.org \
    --cc=gdb-patches@sourceware.org \
    --cc=jhb@freebsd.org \
    --cc=palves@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox