From patchwork Thu Aug 17 20:32:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 135948 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b82d:0:b0:3f2:4152:657d with SMTP id z13csp965330vqi; Thu, 17 Aug 2023 13:40:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGhM/sq59ou9CHrZQOfmLo/aaJ2QwoOgJMxaQw17WSgJxIqVTLx70UjP9msuoeAjfx8l551 X-Received: by 2002:a17:906:8f:b0:99b:cb7a:c164 with SMTP id 15-20020a170906008f00b0099bcb7ac164mr407491ejc.62.1692304859522; Thu, 17 Aug 2023 13:40:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692304859; cv=none; d=google.com; s=arc-20160816; b=X5EM24j0QzelTaJ3JaZWPJP9w1MC9a6Rm+ZF+yzjP5bvyZQ7cT17TZacLBxPzAPrqV I7vKKE49NHOsIjrZytKztsWBr+8kUNItRPfAzUI+mRwnzYR0mH4E/fOGJs/JfCy+D6VP CuDmPgoG+Avlt35NuSI29vTclY76DqaaBTUpBPLFfCe4RtJOQpbTVJSiIHDD5TH5/cA3 bd2r1cWoiF1fUpR7s4PDBKckCZqs1t3JEmbjWH8FaWkWvcEk9JUbYcp2jUdVr8oc4/B3 2xfFDzBMk4VvYTQ0V1SIBz1fpXHTMOW0DpzU9uEkX/y2tHiiHsIMZfH/5w5pUef8+Km6 lqSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=2GAyMKZBPO2azSOE2IuTUTRZu109rMIvVYAQPUh6iYg=; fh=sC8lnOfyH6gL5FYlMbZ1VDz6qVPMj/3ViLJWKNUCUgQ=; b=HwVOhuJJKXkgOWoRViZ+nv2Vm5lwJBNPcuxoNwr0+ifovIkw4zXKzQPKtAZrbJKQBj caqINrvey9hx4flUqSIzDVasr5+mK6n4kSm2wEqb3p0DMWxDN7C3jQZZYeV1IWytNYAd VLlqZDe9EjtONQY7OSRVZQRcq7rQDzzKry5n+o/4gNYd3jx6Vatyy/AV0zB+0IpX90O1 9Koe4t8VhhMZnyXWeVTCudHUqWbvegTrIS25HZWcbMOWZf6BqucW3v5lGAYkCZJvw8zs l71EkIF9+1S1JGSdcRI3QAS5SgOp8QSGW8ZkhAulESwIT68UUy87mn6mhV2osTvfCO6y oJVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=cApbjMEX; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id o5-20020a1709064f8500b00992f8116abdsi213682eju.480.2023.08.17.13.40.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Aug 2023 13:40:59 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=cApbjMEX; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 15B7B3948A63 for ; Thu, 17 Aug 2023 20:35:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 15B7B3948A63 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692304503; bh=2GAyMKZBPO2azSOE2IuTUTRZu109rMIvVYAQPUh6iYg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=cApbjMEXnPAadi1sZgdn24zyB4Gdh/6npBIVfVEsCDgR/tIlSmtycpbBbFRIkq1lc WdYoZJ00VQYBU+2ZS/W6ziRn5bAg60fZrhs/6izmwjqCsc+wfUQOVkwW6Ne0sRwxjy My+AjV+lGPcwQgH4mU3q7RxcMtoeHxf+ULGfRyW0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 6B86C3881D27 for ; Thu, 17 Aug 2023 20:32:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6B86C3881D27 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-223-uFbNvMmSPXCDjpeIP55ZcA-1; Thu, 17 Aug 2023 16:32:28 -0400 X-MC-Unique: uFbNvMmSPXCDjpeIP55ZcA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6D8438DC66B; Thu, 17 Aug 2023 20:32:28 +0000 (UTC) Received: from localhost (unknown [10.42.28.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 364FA1121314; Thu, 17 Aug 2023 20:32:28 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Micro-optimize construction of named std::locale Date: Thu, 17 Aug 2023 21:32:24 +0100 Message-ID: <20230817203228.1131577-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774510260297914506 X-GMAIL-MSGID: 1774510260297914506 Tested x86_64-linux. Pushed to trunk. -- >8 -- This shaves about 100ns off the std::locale constructor for named locales (which is only about 1% of the total time). Using !*s instead of !strcmp(s, "") doesn't make any difference as GCC optimizes that already even at -O1. !strcmp(s, "C") is optimized at -O2 so replacing that with s[0] == 'C' && s[1] == '\0' only matters for the --enable-libstdcxx-debug builds. But !strcmp(s, "POSIX") always makes a call to strcmp at any optimization level. We make that strcmp call, maybe several times, for any locale name except for "C" (which will be matched before we get to the check for "POSIX"). For most targets, locale names begin with a lowercase letter and the only one that begins with 'P' is "POSIX". Replacing !strcmp(s, "POSIX") with s[0] == 'P' && !strcmp(s+1, "OSIX") means that we avoid calling strcmp unless the string really does match "POSIX". Maybe more importantly, I find is_C_locale(s) easier to read than strcmp(s, "C") == 0 || strcmp(s, "POSIX") == 0, and !is_C_locale(s) easier to read than strcmp(s, "C") != 0 && strcmp(s, "POSIX") != 0. libstdc++-v3/ChangeLog: * src/c++98/localename.cc (is_C_locale): New function. (locale::locale(const char*)): Use is_C_locale. --- libstdc++-v3/src/c++98/localename.cc | 39 ++++++++++++++++------------ 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/libstdc++-v3/src/c++98/localename.cc b/libstdc++-v3/src/c++98/localename.cc index 25e6d966dca..68cb81d0709 100644 --- a/libstdc++-v3/src/c++98/localename.cc +++ b/libstdc++-v3/src/c++98/localename.cc @@ -36,24 +36,37 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION using namespace __gnu_cxx; + static inline bool + is_C_locale(const char* s) + { + switch (s[0]) + { + case 'C': + return s[1] == '\0'; + case 'P': + return !std::strcmp(s+1, "OSIX"); + default: + return false; + } + } + locale::locale(const char* __s) : _M_impl(0) { if (__s) { _S_initialize(); - if (std::strcmp(__s, "C") == 0 || std::strcmp(__s, "POSIX") == 0) + if (is_C_locale(__s)) (_M_impl = _S_classic)->_M_add_reference(); - else if (std::strcmp(__s, "") != 0) + else if (*__s) _M_impl = new _Impl(__s, 1); else { // Get it from the environment. char* __env = std::getenv("LC_ALL"); // If LC_ALL is set we are done. - if (__env && std::strcmp(__env, "") != 0) + if (__env && *__env) { - if (std::strcmp(__env, "C") == 0 - || std::strcmp(__env, "POSIX") == 0) + if (is_C_locale(__env)) (_M_impl = _S_classic)->_M_add_reference(); else _M_impl = new _Impl(__env, 1); @@ -63,9 +76,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // LANG may set a default different from "C". string __lang; __env = std::getenv("LANG"); - if (!__env || std::strcmp(__env, "") == 0 - || std::strcmp(__env, "C") == 0 - || std::strcmp(__env, "POSIX") == 0) + if (!__env || !*__env || is_C_locale(__env)) __lang = "C"; else __lang = __env; @@ -77,17 +88,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION for (; __i < _S_categories_size; ++__i) { __env = std::getenv(_S_categories[__i]); - if (__env && std::strcmp(__env, "") != 0 - && std::strcmp(__env, "C") != 0 - && std::strcmp(__env, "POSIX") != 0) + if (__env && *__env && !is_C_locale(__env)) break; } else for (; __i < _S_categories_size; ++__i) { __env = std::getenv(_S_categories[__i]); - if (__env && std::strcmp(__env, "") != 0 - && __lang != __env) + if (__env && *__env && __lang != __env) break; } @@ -113,14 +121,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { __env = std::getenv(_S_categories[__i]); __str += _S_categories[__i]; - if (!__env || std::strcmp(__env, "") == 0) + if (!__env || !*__env) { __str += '='; __str += __lang; __str += ';'; } - else if (std::strcmp(__env, "C") == 0 - || std::strcmp(__env, "POSIX") == 0) + else if (is_C_locale(__env)) __str += "=C;"; else {