From patchwork Fri Jun 21 19:19:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shawn Landden X-Patchwork-Id: 33259 Received: (qmail 19128 invoked by alias); 21 Jun 2019 19:20:10 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 19120 invoked by uid 89); 21 Jun 2019 19:20:10 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.1 spammy=H*Ad:D*icu, H*F:D*icu, HContent-Transfer-Encoding:8bit, HX-Spam-Relays-External:ESMTPA X-HELO: git.icu Received: from git.icu (HELO git.icu) (163.172.180.134) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 21 Jun 2019 19:20:08 +0000 Received: from localhost.localdomain (minicloud.parqtec.unicamp.br [143.106.167.126]) by git.icu (Postfix) with ESMTPA id D33DA221949; Fri, 21 Jun 2019 19:20:03 +0000 (UTC) From: Shawn Landden To: gdb-patches@sourceware.org Cc: Pedro Alves , jhb@freebsd.org, eliz@gnu.org, Shawn Landden Subject: [PATCH] Fix slow and non-deterministic behavior of isspace() and tolower() Date: Fri, 21 Jun 2019 15:19:59 -0400 Message-Id: <20190621191959.24450-1-shawn@git.icu> In-Reply-To: <20190610213017.2021-1-shawn@git.icu> References: <20190610213017.2021-1-shawn@git.icu> MIME-Version: 1.0 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 Co-Author: Pedro Alves 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;