[v24,33/33] libstdc++: Optimize std::is_invocable compilation performance

Message ID 20231020162115.2307797-34-kmatsui@gcc.gnu.org
State Accepted
Headers
Series Optimize type traits performance |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Ken Matsui Oct. 20, 2023, 4:17 p.m. UTC
  This patch optimizes the compilation performance of std::is_invocable
by dispatching to the new __is_invocable built-in trait.

libstdc++-v3/ChangeLog:

	* include/std/type_traits (is_invocable): Use __is_invocable
	built-in trait.
	* testsuite/20_util/is_invocable/incomplete_args_neg.cc: Handle
	the new error from __is_invocable.
	* testsuite/20_util/is_invocable/incomplete_neg.cc: Likewise.

Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
 libstdc++-v3/include/std/type_traits                        | 6 ++++++
 .../testsuite/20_util/is_invocable/incomplete_args_neg.cc   | 1 +
 .../testsuite/20_util/is_invocable/incomplete_neg.cc        | 1 +
 3 files changed, 8 insertions(+)
  

Comments

Patrick Palka Oct. 23, 2023, 5:04 p.m. UTC | #1
On Fri, Oct 20, 2023 at 12:22 PM Ken Matsui <kmatsui@gcc.gnu.org> wrote:
>
> This patch optimizes the compilation performance of std::is_invocable
> by dispatching to the new __is_invocable built-in trait.
>
> libstdc++-v3/ChangeLog:
>
>         * include/std/type_traits (is_invocable): Use __is_invocable
>         built-in trait.

Nice!  We should use the trait directly in is_invocable_v too.

>         * testsuite/20_util/is_invocable/incomplete_args_neg.cc: Handle
>         the new error from __is_invocable.
>         * testsuite/20_util/is_invocable/incomplete_neg.cc: Likewise.
>
> Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
> ---
>  libstdc++-v3/include/std/type_traits                        | 6 ++++++
>  .../testsuite/20_util/is_invocable/incomplete_args_neg.cc   | 1 +
>  .../testsuite/20_util/is_invocable/incomplete_neg.cc        | 1 +
>  3 files changed, 8 insertions(+)
>
> diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
> index 75a94cb8d7e..91851b78c7e 100644
> --- a/libstdc++-v3/include/std/type_traits
> +++ b/libstdc++-v3/include/std/type_traits
> @@ -3167,9 +3167,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>      using invoke_result_t = typename invoke_result<_Fn, _Args...>::type;
>
>    /// std::is_invocable
> +#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_invocable)
> +  template<typename _Fn, typename... _ArgTypes>
> +    struct is_invocable
> +    : public __bool_constant<__is_invocable(_Fn, _ArgTypes...)>
> +#else
>    template<typename _Fn, typename... _ArgTypes>
>      struct is_invocable
>      : __is_invocable_impl<__invoke_result<_Fn, _ArgTypes...>, void>::type
> +#endif
>      {
>        static_assert(std::__is_complete_or_unbounded(__type_identity<_Fn>{}),
>         "_Fn must be a complete class or an unbounded array");
> diff --git a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc
> index 34d1d9431d1..3f9e5274f3c 100644
> --- a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc
> +++ b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc
> @@ -18,6 +18,7 @@
>  // <http://www.gnu.org/licenses/>.
>
>  // { dg-error "must be a complete class" "" { target *-*-* } 0 }
> +// { dg-prune-output "invalid use of incomplete type" }
>
>  #include <type_traits>
>
> diff --git a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc
> index e1e54d25ee5..92af48c48b6 100644
> --- a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc
> +++ b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc
> @@ -18,6 +18,7 @@
>  // <http://www.gnu.org/licenses/>.
>
>  // { dg-error "must be a complete class" "" { target *-*-* } 0 }
> +// { dg-prune-output "invalid use of incomplete type" }
>
>  #include <type_traits>
>
> --
> 2.42.0
>
  
Ken Matsui Oct. 23, 2023, 5:14 p.m. UTC | #2
On Mon, Oct 23, 2023 at 10:05 AM Patrick Palka <ppalka@redhat.com> wrote:

> On Fri, Oct 20, 2023 at 12:22 PM Ken Matsui <kmatsui@gcc.gnu.org> wrote:
> >
> > This patch optimizes the compilation performance of std::is_invocable
> > by dispatching to the new __is_invocable built-in trait.
> >
> > libstdc++-v3/ChangeLog:
> >
> >         * include/std/type_traits (is_invocable): Use __is_invocable
> >         built-in trait.
>
> Nice!  We should use the trait directly in is_invocable_v too.
>

Thank you! But we want to take account of static_assert’s in is_invocable,
so I think we cannot use the built-in directly?


> >         * testsuite/20_util/is_invocable/incomplete_args_neg.cc: Handle
> >         the new error from __is_invocable.
> >         * testsuite/20_util/is_invocable/incomplete_neg.cc: Likewise.
> >
> > Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
> > ---
> >  libstdc++-v3/include/std/type_traits                        | 6 ++++++
> >  .../testsuite/20_util/is_invocable/incomplete_args_neg.cc   | 1 +
> >  .../testsuite/20_util/is_invocable/incomplete_neg.cc        | 1 +
> >  3 files changed, 8 insertions(+)
> >
> > diff --git a/libstdc++-v3/include/std/type_traits
> b/libstdc++-v3/include/std/type_traits
> > index 75a94cb8d7e..91851b78c7e 100644
> > --- a/libstdc++-v3/include/std/type_traits
> > +++ b/libstdc++-v3/include/std/type_traits
> > @@ -3167,9 +3167,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >      using invoke_result_t = typename invoke_result<_Fn, _Args...>::type;
> >
> >    /// std::is_invocable
> > +#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_invocable)
> > +  template<typename _Fn, typename... _ArgTypes>
> > +    struct is_invocable
> > +    : public __bool_constant<__is_invocable(_Fn, _ArgTypes...)>
> > +#else
> >    template<typename _Fn, typename... _ArgTypes>
> >      struct is_invocable
> >      : __is_invocable_impl<__invoke_result<_Fn, _ArgTypes...>,
> void>::type
> > +#endif
> >      {
> >
> static_assert(std::__is_complete_or_unbounded(__type_identity<_Fn>{}),
> >         "_Fn must be a complete class or an unbounded array");
> > diff --git
> a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc
> b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc
> > index 34d1d9431d1..3f9e5274f3c 100644
> > --- a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc
> > +++ b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc
> > @@ -18,6 +18,7 @@
> >  // <http://www.gnu.org/licenses/>.
> >
> >  // { dg-error "must be a complete class" "" { target *-*-* } 0 }
> > +// { dg-prune-output "invalid use of incomplete type" }
> >
> >  #include <type_traits>
> >
> > diff --git
> a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc
> b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc
> > index e1e54d25ee5..92af48c48b6 100644
> > --- a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc
> > +++ b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc
> > @@ -18,6 +18,7 @@
> >  // <http://www.gnu.org/licenses/>.
> >
> >  // { dg-error "must be a complete class" "" { target *-*-* } 0 }
> > +// { dg-prune-output "invalid use of incomplete type" }
> >
> >  #include <type_traits>
> >
> > --
> > 2.42.0
> >
>
>
  
Patrick Palka Oct. 23, 2023, 5:38 p.m. UTC | #3
On Mon, 23 Oct 2023, Ken Matsui wrote:

> On Mon, Oct 23, 2023 at 10:05 AM Patrick Palka <ppalka@redhat.com> wrote:
>       On Fri, Oct 20, 2023 at 12:22 PM Ken Matsui <kmatsui@gcc.gnu.org> wrote:
>       >
>       > This patch optimizes the compilation performance of std::is_invocable
>       > by dispatching to the new __is_invocable built-in trait.
>       >
>       > libstdc++-v3/ChangeLog:
>       >
>       >         * include/std/type_traits (is_invocable): Use __is_invocable
>       >         built-in trait.
> 
>       Nice!  We should use the trait directly in is_invocable_v too.
> 
> 
> Thank you! But we want to take account of static_assert’s in is_invocable, so I think we cannot use the built-in directly?

Good point, I guess that's a great reason to improvement the diagnostic
that check_trait_type emits: it'd speed up the class template version
because we could get rid of the static_asserts (without regressing
diagnostic quality), and it'd speed up the variable template version
because we could use the built-in directly there.

Your patch LGTM as is though, that could be a follow-up if anything.

> 
> 
>       >         * testsuite/20_util/is_invocable/incomplete_args_neg.cc: Handle
>       >         the new error from __is_invocable.
>       >         * testsuite/20_util/is_invocable/incomplete_neg.cc: Likewise.
>       >
>       > Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
>       > ---
>       >  libstdc++-v3/include/std/type_traits                        | 6 ++++++
>       >  .../testsuite/20_util/is_invocable/incomplete_args_neg.cc   | 1 +
>       >  .../testsuite/20_util/is_invocable/incomplete_neg.cc        | 1 +
>       >  3 files changed, 8 insertions(+)
>       >
>       > diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
>       > index 75a94cb8d7e..91851b78c7e 100644
>       > --- a/libstdc++-v3/include/std/type_traits
>       > +++ b/libstdc++-v3/include/std/type_traits
>       > @@ -3167,9 +3167,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>       >      using invoke_result_t = typename invoke_result<_Fn, _Args...>::type;
>       >
>       >    /// std::is_invocable
>       > +#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_invocable)
>       > +  template<typename _Fn, typename... _ArgTypes>
>       > +    struct is_invocable
>       > +    : public __bool_constant<__is_invocable(_Fn, _ArgTypes...)>
>       > +#else
>       >    template<typename _Fn, typename... _ArgTypes>
>       >      struct is_invocable
>       >      : __is_invocable_impl<__invoke_result<_Fn, _ArgTypes...>, void>::type
>       > +#endif
>       >      {
>       >        static_assert(std::__is_complete_or_unbounded(__type_identity<_Fn>{}),
>       >         "_Fn must be a complete class or an unbounded array");
>       > diff --git a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc
>       > index 34d1d9431d1..3f9e5274f3c 100644
>       > --- a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc
>       > +++ b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc
>       > @@ -18,6 +18,7 @@
>       >  // <http://www.gnu.org/licenses/>.
>       >
>       >  // { dg-error "must be a complete class" "" { target *-*-* } 0 }
>       > +// { dg-prune-output "invalid use of incomplete type" }
>       >
>       >  #include <type_traits>
>       >
>       > diff --git a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc
>       > index e1e54d25ee5..92af48c48b6 100644
>       > --- a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc
>       > +++ b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc
>       > @@ -18,6 +18,7 @@
>       >  // <http://www.gnu.org/licenses/>.
>       >
>       >  // { dg-error "must be a complete class" "" { target *-*-* } 0 }
>       > +// { dg-prune-output "invalid use of incomplete type" }
>       >
>       >  #include <type_traits>
>       >
>       > --
>       > 2.42.0
>       >
> 
> 
>
  
Ken Matsui Oct. 23, 2023, 5:47 p.m. UTC | #4
On Mon, Oct 23, 2023 at 10:39 AM Patrick Palka <ppalka@redhat.com> wrote:
>
> On Mon, 23 Oct 2023, Ken Matsui wrote:
>
> > On Mon, Oct 23, 2023 at 10:05 AM Patrick Palka <ppalka@redhat.com> wrote:
> >       On Fri, Oct 20, 2023 at 12:22 PM Ken Matsui <kmatsui@gcc.gnu.org> wrote:
> >       >
> >       > This patch optimizes the compilation performance of std::is_invocable
> >       > by dispatching to the new __is_invocable built-in trait.
> >       >
> >       > libstdc++-v3/ChangeLog:
> >       >
> >       >         * include/std/type_traits (is_invocable): Use __is_invocable
> >       >         built-in trait.
> >
> >       Nice!  We should use the trait directly in is_invocable_v too.
> >
> >
> > Thank you! But we want to take account of static_assert’s in is_invocable, so I think we cannot use the built-in directly?
>
> Good point, I guess that's a great reason to improvement the diagnostic
> that check_trait_type emits: it'd speed up the class template version
> because we could get rid of the static_asserts (without regressing
> diagnostic quality), and it'd speed up the variable template version
> because we could use the built-in directly there.
>
> Your patch LGTM as is though, that could be a follow-up if anything.
>

Thank you! I will also work on this after other built-in traits end!

> >
> >
> >       >         * testsuite/20_util/is_invocable/incomplete_args_neg.cc: Handle
> >       >         the new error from __is_invocable.
> >       >         * testsuite/20_util/is_invocable/incomplete_neg.cc: Likewise.
> >       >
> >       > Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
> >       > ---
> >       >  libstdc++-v3/include/std/type_traits                        | 6 ++++++
> >       >  .../testsuite/20_util/is_invocable/incomplete_args_neg.cc   | 1 +
> >       >  .../testsuite/20_util/is_invocable/incomplete_neg.cc        | 1 +
> >       >  3 files changed, 8 insertions(+)
> >       >
> >       > diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
> >       > index 75a94cb8d7e..91851b78c7e 100644
> >       > --- a/libstdc++-v3/include/std/type_traits
> >       > +++ b/libstdc++-v3/include/std/type_traits
> >       > @@ -3167,9 +3167,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >       >      using invoke_result_t = typename invoke_result<_Fn, _Args...>::type;
> >       >
> >       >    /// std::is_invocable
> >       > +#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_invocable)
> >       > +  template<typename _Fn, typename... _ArgTypes>
> >       > +    struct is_invocable
> >       > +    : public __bool_constant<__is_invocable(_Fn, _ArgTypes...)>
> >       > +#else
> >       >    template<typename _Fn, typename... _ArgTypes>
> >       >      struct is_invocable
> >       >      : __is_invocable_impl<__invoke_result<_Fn, _ArgTypes...>, void>::type
> >       > +#endif
> >       >      {
> >       >        static_assert(std::__is_complete_or_unbounded(__type_identity<_Fn>{}),
> >       >         "_Fn must be a complete class or an unbounded array");
> >       > diff --git a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc
> >       > index 34d1d9431d1..3f9e5274f3c 100644
> >       > --- a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc
> >       > +++ b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc
> >       > @@ -18,6 +18,7 @@
> >       >  // <http://www.gnu.org/licenses/>.
> >       >
> >       >  // { dg-error "must be a complete class" "" { target *-*-* } 0 }
> >       > +// { dg-prune-output "invalid use of incomplete type" }
> >       >
> >       >  #include <type_traits>
> >       >
> >       > diff --git a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc
> >       > index e1e54d25ee5..92af48c48b6 100644
> >       > --- a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc
> >       > +++ b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc
> >       > @@ -18,6 +18,7 @@
> >       >  // <http://www.gnu.org/licenses/>.
> >       >
> >       >  // { dg-error "must be a complete class" "" { target *-*-* } 0 }
> >       > +// { dg-prune-output "invalid use of incomplete type" }
> >       >
> >       >  #include <type_traits>
> >       >
> >       > --
> >       > 2.42.0
> >       >
> >
> >
> >
  

Patch

diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 75a94cb8d7e..91851b78c7e 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -3167,9 +3167,15 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
     using invoke_result_t = typename invoke_result<_Fn, _Args...>::type;
 
   /// std::is_invocable
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_invocable)
+  template<typename _Fn, typename... _ArgTypes>
+    struct is_invocable
+    : public __bool_constant<__is_invocable(_Fn, _ArgTypes...)>
+#else
   template<typename _Fn, typename... _ArgTypes>
     struct is_invocable
     : __is_invocable_impl<__invoke_result<_Fn, _ArgTypes...>, void>::type
+#endif
     {
       static_assert(std::__is_complete_or_unbounded(__type_identity<_Fn>{}),
 	"_Fn must be a complete class or an unbounded array");
diff --git a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc
index 34d1d9431d1..3f9e5274f3c 100644
--- a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc
+++ b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_args_neg.cc
@@ -18,6 +18,7 @@ 
 // <http://www.gnu.org/licenses/>.
 
 // { dg-error "must be a complete class" "" { target *-*-* } 0 }
+// { dg-prune-output "invalid use of incomplete type" }
 
 #include <type_traits>
 
diff --git a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc
index e1e54d25ee5..92af48c48b6 100644
--- a/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc
+++ b/libstdc++-v3/testsuite/20_util/is_invocable/incomplete_neg.cc
@@ -18,6 +18,7 @@ 
 // <http://www.gnu.org/licenses/>.
 
 // { dg-error "must be a complete class" "" { target *-*-* } 0 }
+// { dg-prune-output "invalid use of incomplete type" }
 
 #include <type_traits>