diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2023-06-08 12:24:43 +0100 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2023-06-09 13:08:25 +0100 |
commit | 960de5dd886572711ef86fa1e15e30d3810eccb9 (patch) | |
tree | e874baa0c91fa2050c592c137fc374a6db78fd61 /libstdc++-v3/include/std/array | |
parent | 3e12669a0eb968cfcbe9242b382fd8020935edf8 (diff) | |
download | gcc-960de5dd886572711ef86fa1e15e30d3810eccb9.zip gcc-960de5dd886572711ef86fa1e15e30d3810eccb9.tar.gz gcc-960de5dd886572711ef86fa1e15e30d3810eccb9.tar.bz2 |
libstdc++: Optimize std::to_array for trivial types [PR110167]
As reported in PR libstdc++/110167, std::to_array compiles extremely
slowly for very large arrays. It needs to instantiate a very large
specialization of std::index_sequence<N...> and then create a very large
aggregate initializer from the pack expansion. For trivial types we can
simply default-initialize the std::array and then use memcpy to copy the
values. For non-trivial types we need to use the existing
implementation, despite the compilation cost.
As also noted in the PR, using a generic lambda instead of the
__to_array helper compiles faster since gcc-13. It also produces
slightly smaller code at -O1, due to additional inlining. The code at
-Os, -O2 and -O3 seems to be the same. This new implementation requires
__cpp_generic_lambdas >= 201707L (i.e. P0428R2) but that is supported
since Clang 10 and since Intel icc 2021.5.0 (and since GCC 10.1).
libstdc++-v3/ChangeLog:
PR libstdc++/110167
* include/std/array (to_array): Initialize arrays of trivial
types using memcpy. For non-trivial types, use lambda
expressions instead of a separate helper function.
(__to_array): Remove.
* testsuite/23_containers/array/creation/110167.cc: New test.
Diffstat (limited to 'libstdc++-v3/include/std/array')
-rw-r--r-- | libstdc++-v3/include/std/array | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/libstdc++-v3/include/std/array b/libstdc++-v3/include/std/array index 70280c1..b791d86 100644 --- a/libstdc++-v3/include/std/array +++ b/libstdc++-v3/include/std/array @@ -414,19 +414,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return std::move(std::get<_Int>(__arr)); } -#if __cplusplus > 201703L +#if __cplusplus >= 202002L && __cpp_generic_lambdas >= 201707L #define __cpp_lib_to_array 201907L - - template<bool _Move = false, typename _Tp, size_t... _Idx> - constexpr array<remove_cv_t<_Tp>, sizeof...(_Idx)> - __to_array(_Tp (&__a)[sizeof...(_Idx)], index_sequence<_Idx...>) - { - if constexpr (_Move) - return {{std::move(__a[_Idx])...}}; - else - return {{__a[_Idx]...}}; - } - template<typename _Tp, size_t _Nm> [[nodiscard]] constexpr array<remove_cv_t<_Tp>, _Nm> @@ -436,8 +425,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static_assert(!is_array_v<_Tp>); static_assert(is_constructible_v<_Tp, _Tp&>); if constexpr (is_constructible_v<_Tp, _Tp&>) - return __to_array(__a, make_index_sequence<_Nm>{}); - __builtin_unreachable(); // FIXME: see PR c++/91388 + { + if constexpr (is_trivial_v<_Tp> && _Nm != 0) + { + array<remove_cv_t<_Tp>, _Nm> __arr; + if (!__is_constant_evaluated() && _Nm != 0) + __builtin_memcpy(__arr.data(), __a, sizeof(__a)); + else + for (size_t __i = 0; __i < _Nm; ++__i) + __arr._M_elems[__i] = __a[__i]; + return __arr; + } + else + return [&__a]<size_t... _Idx>(index_sequence<_Idx...>) { + return array<remove_cv_t<_Tp>, _Nm>{{ __a[_Idx]... }}; + }(make_index_sequence<_Nm>{}); + } + else + __builtin_unreachable(); // FIXME: see PR c++/91388 } template<typename _Tp, size_t _Nm> @@ -449,8 +454,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static_assert(!is_array_v<_Tp>); static_assert(is_move_constructible_v<_Tp>); if constexpr (is_move_constructible_v<_Tp>) - return __to_array<1>(__a, make_index_sequence<_Nm>{}); - __builtin_unreachable(); // FIXME: see PR c++/91388 + { + if constexpr (is_trivial_v<_Tp>) + { + array<remove_cv_t<_Tp>, _Nm> __arr; + if (!__is_constant_evaluated() && _Nm != 0) + __builtin_memcpy(__arr.data(), __a, sizeof(__a)); + else + for (size_t __i = 0; __i < _Nm; ++__i) + __arr._M_elems[__i] = std::move(__a[__i]); + return __arr; + } + else + return [&__a]<size_t... _Idx>(index_sequence<_Idx...>) { + return array<remove_cv_t<_Tp>, _Nm>{{ std::move(__a[_Idx])... }}; + }(make_index_sequence<_Nm>{}); + } + else + __builtin_unreachable(); // FIXME: see PR c++/91388 } #endif // C++20 |