]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Optimize std::try_facet and std::use_facet [PR103755]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 20 Apr 2023 20:02:22 +0000 (21:02 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 21 Apr 2023 15:01:07 +0000 (16:01 +0100)
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<char>, std::num_get<char>, std::num_put<char>, 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
libstdc++-v3/testsuite/22_locale/ctype/is/string/89728_neg.cc

index 64cd7534dc652f2684d7d3cc1aef87208438dd17..e6ce07ae8b78c6d3195509fef945f12b2398bdeb 100644 (file)
@@ -103,6 +103,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     bool
     has_facet(const locale& __loc) throw()
     {
+      if _GLIBCXX17_CONSTEXPR (__is_same(_Facet, ctype<char>)
+                                || __is_same(_Facet, num_get<char>)
+                                || __is_same(_Facet, num_put<char>))
+       return true;
+#ifdef _GLIBCXX_USE_WCHAR_T
+      else if _GLIBCXX17_CONSTEXPR (__is_same(_Facet, ctype<wchar_t>)
+                                     || __is_same(_Facet, num_get<wchar_t>)
+                                     || __is_same(_Facet, num_put<wchar_t>))
+       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<char>)
+                                || __is_same(_Facet, num_get<char>)
+                                || __is_same(_Facet, num_put<char>))
+       return static_cast<const _Facet&>(*__facets[__i]);
+#ifdef _GLIBCXX_USE_WCHAR_T
+      else if _GLIBCXX17_CONSTEXPR (__is_same(_Facet, ctype<wchar_t>)
+                                     || __is_same(_Facet, num_get<wchar_t>)
+                                     || __is_same(_Facet, num_put<wchar_t>))
+       return static_cast<const _Facet&>(*__facets[__i]);
+#endif
+
       if (__i >= __loc._M_impl->_M_facets_size || !__facets[__i])
         __throw_bad_cast();
 #if __cpp_rtti
index 77bb1a64f4518457c7330b136d62d7f55b1f6cc2..baed67d64f4e06abd1c724603802c9e763b21cc1 100644 (file)
@@ -18,6 +18,7 @@
 // <http://www.gnu.org/licenses/>.
 
 // { dg-error "complete" "" { target *-*-* } 0 }
+// { dg-error "invalid .static_cast." "" { target c++14_down } 0 }
 
 #include <locale>