public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] libstdc++: Optimize std::is_trivially_destructible_v
@ 2023-12-14  1:07 Jonathan Wakely
  2024-01-07  1:00 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Wakely @ 2023-12-14  1:07 UTC (permalink / raw)
  To: libstdc++, gcc-patches

Tested x86_64-linux.

Does this look right? Can we do it faster, or simplify it?

-- >8 --

This reduces the overhead of using std::is_trivially_destructible_v and
as a result fixes some recent regressions seen with a non-default
GLIBCXX_TESTSUITE_STDS env var:
FAIL: 20_util/variant/87619.cc  -std=gnu++20 (test for excess errors)
FAIL: 20_util/variant/87619.cc  -std=gnu++23 (test for excess errors)
FAIL: 20_util/variant/87619.cc  -std=gnu++26 (test for excess errors)

libstdc++-v3/ChangeLog:

	* include/std/type_traits (is_trivially_destructible_v): Use
	built-in directly when concepts are supported.
	* testsuite/20_util/is_trivially_destructible/value_v.cc: New
	test.
---
 libstdc++-v3/include/std/type_traits          | 19 +++++++++
 .../is_trivially_destructible/value_v.cc      | 40 +++++++++++++++++++
 2 files changed, 59 insertions(+)
 create mode 100644 libstdc++-v3/testsuite/20_util/is_trivially_destructible/value_v.cc

diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 677cd934b94..a0821347676 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -3300,9 +3300,28 @@ template <typename _Tp>
   inline constexpr bool is_trivially_move_assignable_v
     = __is_trivially_assignable(__add_lval_ref_t<_Tp>,
 				__add_rval_ref_t<_Tp>);
+
+#if __cpp_concepts
+template <typename _Tp>
+  inline constexpr bool is_trivially_destructible_v = false;
+
+template <typename _Tp>
+  requires (!is_reference_v<_Tp>) && requires (_Tp& __t) { __t.~_Tp(); }
+  inline constexpr bool is_trivially_destructible_v<_Tp>
+    = __has_trivial_destructor(_Tp);
+template <typename _Tp>
+  inline constexpr bool is_trivially_destructible_v<_Tp&> = true;
+template <typename _Tp>
+  inline constexpr bool is_trivially_destructible_v<_Tp&&> = true;
+template <typename _Tp, size_t _Nm>
+  inline constexpr bool is_trivially_destructible_v<_Tp[_Nm]>
+    = is_trivially_destructible_v<_Tp>;
+#else
 template <typename _Tp>
   inline constexpr bool is_trivially_destructible_v =
     is_trivially_destructible<_Tp>::value;
+#endif
+
 template <typename _Tp, typename... _Args>
   inline constexpr bool is_nothrow_constructible_v
     = __is_nothrow_constructible(_Tp, _Args...);
diff --git a/libstdc++-v3/testsuite/20_util/is_trivially_destructible/value_v.cc b/libstdc++-v3/testsuite/20_util/is_trivially_destructible/value_v.cc
new file mode 100644
index 00000000000..7db098a2c4d
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/is_trivially_destructible/value_v.cc
@@ -0,0 +1,40 @@
+// { dg-do compile { target c++17 } }
+#include <type_traits>
+#include <testsuite_tr1.h>
+
+template<typename T>
+constexpr void test_cv()
+{
+  static_assert(std::is_trivially_destructible_v<const T>
+      == std::is_trivially_destructible_v<T>);
+  static_assert(std::is_trivially_destructible_v<volatile T>
+      == std::is_trivially_destructible_v<T>);
+  static_assert(std::is_trivially_destructible_v<const volatile T>
+      == std::is_trivially_destructible_v<T>);
+}
+
+template<typename T, bool Expected>
+void test()
+{
+  static_assert(std::is_trivially_destructible_v<T> == Expected);
+  test_cv<T>();
+}
+
+void test01()
+{
+  using namespace __gnu_test;
+
+  test<int, true>();
+  test<int&, true>();
+  test<int&&, true>();
+  test<int[1], true>();
+  test<TType, true>();
+  test<TType[1], true>();
+  test<PODType, true>();
+  test<PODType[1], true>();
+  test<NType, false>();
+  test<SLType, false>();
+  test<int(), false>();
+  test<void, false>();
+  test<int[], false>();
+}
-- 
2.43.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] libstdc++: Optimize std::is_trivially_destructible_v
  2023-12-14  1:07 [PATCH] libstdc++: Optimize std::is_trivially_destructible_v Jonathan Wakely
@ 2024-01-07  1:00 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2024-01-07  1:00 UTC (permalink / raw)
  To: libstdc++, gcc-patches

Pushed to trunk now.

On Thu, 14 Dec 2023 at 01:09, Jonathan Wakely <jwakely@redhat.com> wrote:
>
> Tested x86_64-linux.
>
> Does this look right? Can we do it faster, or simplify it?
>
> -- >8 --
>
> This reduces the overhead of using std::is_trivially_destructible_v and
> as a result fixes some recent regressions seen with a non-default
> GLIBCXX_TESTSUITE_STDS env var:
> FAIL: 20_util/variant/87619.cc  -std=gnu++20 (test for excess errors)
> FAIL: 20_util/variant/87619.cc  -std=gnu++23 (test for excess errors)
> FAIL: 20_util/variant/87619.cc  -std=gnu++26 (test for excess errors)
>
> libstdc++-v3/ChangeLog:
>
>         * include/std/type_traits (is_trivially_destructible_v): Use
>         built-in directly when concepts are supported.
>         * testsuite/20_util/is_trivially_destructible/value_v.cc: New
>         test.
> ---
>  libstdc++-v3/include/std/type_traits          | 19 +++++++++
>  .../is_trivially_destructible/value_v.cc      | 40 +++++++++++++++++++
>  2 files changed, 59 insertions(+)
>  create mode 100644 libstdc++-v3/testsuite/20_util/is_trivially_destructible/value_v.cc
>
> diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
> index 677cd934b94..a0821347676 100644
> --- a/libstdc++-v3/include/std/type_traits
> +++ b/libstdc++-v3/include/std/type_traits
> @@ -3300,9 +3300,28 @@ template <typename _Tp>
>    inline constexpr bool is_trivially_move_assignable_v
>      = __is_trivially_assignable(__add_lval_ref_t<_Tp>,
>                                 __add_rval_ref_t<_Tp>);
> +
> +#if __cpp_concepts
> +template <typename _Tp>
> +  inline constexpr bool is_trivially_destructible_v = false;
> +
> +template <typename _Tp>
> +  requires (!is_reference_v<_Tp>) && requires (_Tp& __t) { __t.~_Tp(); }
> +  inline constexpr bool is_trivially_destructible_v<_Tp>
> +    = __has_trivial_destructor(_Tp);
> +template <typename _Tp>
> +  inline constexpr bool is_trivially_destructible_v<_Tp&> = true;
> +template <typename _Tp>
> +  inline constexpr bool is_trivially_destructible_v<_Tp&&> = true;
> +template <typename _Tp, size_t _Nm>
> +  inline constexpr bool is_trivially_destructible_v<_Tp[_Nm]>
> +    = is_trivially_destructible_v<_Tp>;
> +#else
>  template <typename _Tp>
>    inline constexpr bool is_trivially_destructible_v =
>      is_trivially_destructible<_Tp>::value;
> +#endif
> +
>  template <typename _Tp, typename... _Args>
>    inline constexpr bool is_nothrow_constructible_v
>      = __is_nothrow_constructible(_Tp, _Args...);
> diff --git a/libstdc++-v3/testsuite/20_util/is_trivially_destructible/value_v.cc b/libstdc++-v3/testsuite/20_util/is_trivially_destructible/value_v.cc
> new file mode 100644
> index 00000000000..7db098a2c4d
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/20_util/is_trivially_destructible/value_v.cc
> @@ -0,0 +1,40 @@
> +// { dg-do compile { target c++17 } }
> +#include <type_traits>
> +#include <testsuite_tr1.h>
> +
> +template<typename T>
> +constexpr void test_cv()
> +{
> +  static_assert(std::is_trivially_destructible_v<const T>
> +      == std::is_trivially_destructible_v<T>);
> +  static_assert(std::is_trivially_destructible_v<volatile T>
> +      == std::is_trivially_destructible_v<T>);
> +  static_assert(std::is_trivially_destructible_v<const volatile T>
> +      == std::is_trivially_destructible_v<T>);
> +}
> +
> +template<typename T, bool Expected>
> +void test()
> +{
> +  static_assert(std::is_trivially_destructible_v<T> == Expected);
> +  test_cv<T>();
> +}
> +
> +void test01()
> +{
> +  using namespace __gnu_test;
> +
> +  test<int, true>();
> +  test<int&, true>();
> +  test<int&&, true>();
> +  test<int[1], true>();
> +  test<TType, true>();
> +  test<TType[1], true>();
> +  test<PODType, true>();
> +  test<PODType[1], true>();
> +  test<NType, false>();
> +  test<SLType, false>();
> +  test<int(), false>();
> +  test<void, false>();
> +  test<int[], false>();
> +}
> --
> 2.43.0
>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-01-07  1:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-14  1:07 [PATCH] libstdc++: Optimize std::is_trivially_destructible_v Jonathan Wakely
2024-01-07  1:00 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).