From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id CAFCE385B508; Fri, 21 Apr 2023 15:01:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CAFCE385B508 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682089287; bh=G+ipV74i2Xc+no+cL34B/c7HAsd2+NKOh30WRud+6oQ=; h=From:To:Subject:Date:From; b=OkBE3PeE0/sput0bM8QNOnGB4MLLuf54UOH5uyOCLZJD1B920CBTfcXJYFYE4dsGz zUVBAWwVPvXm7OpNj7px0uRpZhj2DO7mdEf5ekQ7U+KrGYkOVG1+JihiBw6qGgWddU o2FH4iKZPiI53WYZOGim/93/UprkaNOTMT126K9s= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-9462] libstdc++: Optimize std::try_facet and std::use_facet [PR103755] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: dca9419cc3844d3cf3c06f51d5ca57e3b5f50920 X-Git-Newrev: 8caf5805ad76125b84430b8653003f4776489d46 Message-Id: <20230421150127.CAFCE385B508@sourceware.org> Date: Fri, 21 Apr 2023 15:01:27 +0000 (GMT) List-Id: https://gcc.gnu.org/g:8caf5805ad76125b84430b8653003f4776489d46 commit r12-9462-g8caf5805ad76125b84430b8653003f4776489d46 Author: Jonathan Wakely Date: Thu Apr 20 21:02:22 2023 +0100 libstdc++: Optimize std::try_facet and std::use_facet [PR103755] 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. Diff: --- 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