From patchwork Fri Apr 21 15:02:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 86376 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1134754vqo; Fri, 21 Apr 2023 08:05:06 -0700 (PDT) X-Google-Smtp-Source: AKy350ZFnAHcihT9+TqKRwTLDU8+UMMDc39DccnCTxTOPKqRMzfsQfMTiMnUIox+Z92x4ym0ikHr X-Received: by 2002:a17:907:765a:b0:94f:39b4:e32c with SMTP id kj26-20020a170907765a00b0094f39b4e32cmr2743461ejc.43.1682089505725; Fri, 21 Apr 2023 08:05:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682089505; cv=none; d=google.com; s=arc-20160816; b=ODxLy2FyWe0p4No92pnP6sui2qQM8O4PlzN6jZy6fxW+Vg6Co/jZK/VbcpoQp99oqU 9cd2UNZyXIapw3ns4KGATnJLRshlXB+kPw0JBTR8s9ayf90D7yaK6QsAwrqZaEPnHBlV vjuInuxFLYDhCgM424DlWwFNBIhgrqf7OCOunOEAoSBt/BxRAY16YgiNOs9aKRm6y2wj a62dcys96KvSC2U4f4M8/S54DN1UAO6cZagirMTBTGsujHVgZskhIu95/JjbLvc9K6E6 lF7wABdayACGFA7h/TfRT3wzw54lcd1mlNyMlMACoZ0KRHmLtBhS+Hbc9nTJkoaudfJe TNtA== 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=e7kH1+DzRqRjwXD7L6AnDE/zDvU1UdSwKw2waoT2xww=; b=Wh5hzEKEZt3BuMRp4zhdVHjWU3UnMupsH+GDwQRVi1ujq/nxxHhTZXRAclejywgBXp W1OP3dpdF/QjuttU2Gx3BLsH4bR/vtbS3X3VWtKerFLbeKACzpCaeAIGm30xxmZefuOj cx6Nki/njClD4+KWUNOePDr7nnqNOJ7uhPBaPYGuloggWkMLp/c/bI4bNdGF9tlPqEtP VoSfTFCiE2HLe98qS58Y33swYIDP7xPnF5lDwkShnC+BNr5Sex+EhcXm0ceAMS0QChpC Cd7OdFusY5AQeg3AMD+ebzwvoBC4q4wwAH7Z23P/uk37EplQ4RkrxgWms1MvgHBr+8Qw eD9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=AzWSoh8p; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id mb15-20020a170906eb0f00b0094f6b45259dsi3727202ejb.1013.2023.04.21.08.05.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Apr 2023 08:05:05 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=AzWSoh8p; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 59DE3385B507 for ; Fri, 21 Apr 2023 15:03:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 59DE3385B507 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682089432; bh=e7kH1+DzRqRjwXD7L6AnDE/zDvU1UdSwKw2waoT2xww=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=AzWSoh8pjlB89+xq3jGpo5P08rVyFJBrLohuEB/Q1zxLfDUSJLfTcTdVBi630pv8k 9vU0r1Nwu05ywiStoFgs0J71C2htQCuGL+uXP78TCd0ilNIrXOyehNidoULhBjFZmY zQCKwErYsPAGklmSPI61pUjkgLvO/7np3Ta/txMA= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 34C2C3853560 for ; Fri, 21 Apr 2023 15:02:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 34C2C3853560 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-356-qJp3x34-MPihhPpVcI4UKw-1; Fri, 21 Apr 2023 11:02:49 -0400 X-MC-Unique: qJp3x34-MPihhPpVcI4UKw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 43CA438173E2; Fri, 21 Apr 2023 15:02:49 +0000 (UTC) Received: from localhost (unknown [10.42.28.164]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0F89440C2064; Fri, 21 Apr 2023 15:02:48 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed, gcc-12] libstdc++: Optimize std::try_facet and std::use_facet [PR103755] Date: Fri, 21 Apr 2023 16:02:48 +0100 Message-Id: <20230421150248.968828-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763798685441174883?= X-GMAIL-MSGID: =?utf-8?q?1763798685441174883?= Tested powerpc64le-linux. Pushed to gcc-12. -- >8 -- The std::try_facet and std::use_facet functions were optimized in r13-3888-gb3ac43a3c05744 to avoid redundant checking for all facets that are required to always be present in every locale. This performs a simpler version of the optimization that only applies to std::ctype, std::num_get, std::num_put, and the wchar_t specializations of those facets. Those are the facets that are cached by std::basic_ios, which means they're used on construction for every iostream object. This smaller change is suitable for the gcc-12 branch, and mitigates the performance loss for powerpc64le-linux on the gcc-12 branch caused by r12-9454-g24cf9f4c6f45f7 for PR 103387. It also greatly improves the performance of constructing iostreams objects, for all targets. libstdc++-v3/ChangeLog: PR libstdc++/103755 * include/bits/locale_classes.tcc (try_facet, use_facet): Do not check array index or dynamic type when accessing required specializations of std::ctype, std::num_get, or std::num_put. * testsuite/22_locale/ctype/is/string/89728_neg.cc: Adjust expected errors. --- libstdc++-v3/include/bits/locale_classes.tcc | 23 +++++++++++++++++++ .../22_locale/ctype/is/string/89728_neg.cc | 1 + 2 files changed, 24 insertions(+) diff --git a/libstdc++-v3/include/bits/locale_classes.tcc b/libstdc++-v3/include/bits/locale_classes.tcc index 64cd7534dc6..e6ce07ae8b7 100644 --- a/libstdc++-v3/include/bits/locale_classes.tcc +++ b/libstdc++-v3/include/bits/locale_classes.tcc @@ -103,6 +103,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION bool has_facet(const locale& __loc) throw() { + if _GLIBCXX17_CONSTEXPR (__is_same(_Facet, ctype) + || __is_same(_Facet, num_get) + || __is_same(_Facet, num_put)) + return true; +#ifdef _GLIBCXX_USE_WCHAR_T + else if _GLIBCXX17_CONSTEXPR (__is_same(_Facet, ctype) + || __is_same(_Facet, num_get) + || __is_same(_Facet, num_put)) + return true; +#endif + const size_t __i = _Facet::id._M_id(); const locale::facet** __facets = __loc._M_impl->_M_facets; return (__i < __loc._M_impl->_M_facets_size @@ -133,6 +144,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { const size_t __i = _Facet::id._M_id(); const locale::facet** __facets = __loc._M_impl->_M_facets; + + if _GLIBCXX17_CONSTEXPR (__is_same(_Facet, ctype) + || __is_same(_Facet, num_get) + || __is_same(_Facet, num_put)) + return static_cast(*__facets[__i]); +#ifdef _GLIBCXX_USE_WCHAR_T + else if _GLIBCXX17_CONSTEXPR (__is_same(_Facet, ctype) + || __is_same(_Facet, num_get) + || __is_same(_Facet, num_put)) + return static_cast(*__facets[__i]); +#endif + if (__i >= __loc._M_impl->_M_facets_size || !__facets[__i]) __throw_bad_cast(); #if __cpp_rtti diff --git a/libstdc++-v3/testsuite/22_locale/ctype/is/string/89728_neg.cc b/libstdc++-v3/testsuite/22_locale/ctype/is/string/89728_neg.cc index 77bb1a64f45..baed67d64f4 100644 --- a/libstdc++-v3/testsuite/22_locale/ctype/is/string/89728_neg.cc +++ b/libstdc++-v3/testsuite/22_locale/ctype/is/string/89728_neg.cc @@ -18,6 +18,7 @@ // . // { dg-error "complete" "" { target *-*-* } 0 } +// { dg-error "invalid .static_cast." "" { target c++14_down } 0 } #include