From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id BFFC43858433 for ; Tue, 20 Jun 2023 00:54:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BFFC43858433 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687222452; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=OuGiNWFLXr9zWsEd6qpe62qNea5wZMDbbhiBtBjgF68=; b=KvULuTTPN78pO2e1Dotk+j/H/SA9Z5TggaK/XLwUiS08xKfF8Q/AsRyKqbrYnpJgwRlrhh qMT/79+wI4V0j0DE4wLr22PH4GJR+9lWhksMm600xLgsxUj8aGIyh1WMgxPhneQ+A6brc/ 45pinqGhZqsOeVfeNccXnfieesAT6E8= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-650-VzdqtqPlPZqhxXxEtdJ7Dw-1; Mon, 19 Jun 2023 20:54:10 -0400 X-MC-Unique: VzdqtqPlPZqhxXxEtdJ7Dw-1 Received: by mail-qt1-f200.google.com with SMTP id d75a77b69052e-3f9f44bba08so42779581cf.2 for ; Mon, 19 Jun 2023 17:54:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687222450; x=1689814450; h=mime-version:references:message-id:in-reply-to:subject:cc:to:date :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OuGiNWFLXr9zWsEd6qpe62qNea5wZMDbbhiBtBjgF68=; b=O1f8I4prY8iKbkGr0/1e5b1BcYZBT4ZSNdF/OX05mh5OE5Gu0otRA8/bfheC/X16p7 Slp+pcgX3BC2egSnRhk3PgYD75oWSsbyJE2LjyrhynDs0xEl5axp0kqjhJZ1+yoUkFtS x1jG9lneuEdCIw68qBEyCWc2LRoyHRXEyZybYtUgJ81d6cZThqzBbiASWNTLyQtyiqxm gMZUmYHQxXM9MLOk8uyq1nrWuW6241j4tNXJex9Vw0H8JjJPv5SLc7RuysDQuvxH4l7s mDT9glUey7qD9uB2GHG5IFa1f5bCmSLopxcGtPduFKziIWR7fk098FLMBHNmv7o5d9ZX 1Tvw== X-Gm-Message-State: AC+VfDzHdu9ig8M6U3yuE1k+ikTKOXhKsqiMKarmOSlT3CiKbZz3WX2l VWmnAwUnq233l7//B+xH074lDnKHMNpg4qv65ZO9GtJchzqPaUlT2Lm7cOkfpWjnj3GT54cNVCL RSNyKcPSz1RqjJ5l1jA== X-Received: by 2002:a05:622a:1004:b0:3fd:e517:43c9 with SMTP id d4-20020a05622a100400b003fde51743c9mr8446290qte.28.1687222449923; Mon, 19 Jun 2023 17:54:09 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ408ShNHMoZiP6t2Bg2tngul2ytmE3fVLGVSQpDMuWqrUV5C416VZQo/w7y5ZmLBvOH4NbzUQ== X-Received: by 2002:a05:622a:1004:b0:3fd:e517:43c9 with SMTP id d4-20020a05622a100400b003fde51743c9mr8446279qte.28.1687222449559; Mon, 19 Jun 2023 17:54:09 -0700 (PDT) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id ez12-20020a05622a4c8c00b003f4e18bfa06sm487167qtb.60.2023.06.19.17.54.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Jun 2023 17:54:09 -0700 (PDT) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Mon, 19 Jun 2023 20:54:08 -0400 (EDT) To: Jonathan Wakely cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: Re: [committed] libstdc++: Optimize std::to_array for trivial types [PR110167] In-Reply-To: <20230609120917.294304-1-jwakely@redhat.com> Message-ID: References: <20230609120917.294304-1-jwakely@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE,URI_HEX autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Fri, 9 Jun 2023, Jonathan Wakely via Libstdc++ wrote: > Tested powerpc64le-linux. Pushed to trunk. > > This makes sense to backport after some soak time on trunk. > > -- >8 -- > > 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 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. > --- > libstdc++-v3/include/std/array | 53 +++++++++++++------ > .../23_containers/array/creation/110167.cc | 14 +++++ > 2 files changed, 51 insertions(+), 16 deletions(-) > create mode 100644 libstdc++-v3/testsuite/23_containers/array/creation/110167.cc > > diff --git a/libstdc++-v3/include/std/array b/libstdc++-v3/include/std/array > index 70280c1beeb..b791d86ddb2 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 > - constexpr array, sizeof...(_Idx)> > - __to_array(_Tp (&__a)[sizeof...(_Idx)], index_sequence<_Idx...>) > - { > - if constexpr (_Move) > - return {{std::move(__a[_Idx])...}}; > - else > - return {{__a[_Idx]...}}; > - } > - > template > [[nodiscard]] > constexpr array, _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) redundant _Nm != 0 test? > + { > + array, _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](index_sequence<_Idx...>) { > + return array, _Nm>{{ __a[_Idx]... }}; > + }(make_index_sequence<_Nm>{}); > + } > + else > + __builtin_unreachable(); // FIXME: see PR c++/91388 > } > > template > @@ -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, _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]); IIUC this std::move is unnecessary for trivial arrays? > + return __arr; > + } > + else > + return [&__a](index_sequence<_Idx...>) { > + return array, _Nm>{{ std::move(__a[_Idx])... }}; > + }(make_index_sequence<_Nm>{}); > + } > + else > + __builtin_unreachable(); // FIXME: see PR c++/91388 > } > #endif // C++20 > > diff --git a/libstdc++-v3/testsuite/23_containers/array/creation/110167.cc b/libstdc++-v3/testsuite/23_containers/array/creation/110167.cc > new file mode 100644 > index 00000000000..c2aecc911bd > --- /dev/null > +++ b/libstdc++-v3/testsuite/23_containers/array/creation/110167.cc > @@ -0,0 +1,14 @@ > +// { dg-options "-std=gnu++20" } > +// { dg-do compile { target c++20 } } > + > +// PR libstdc++/110167 - excessive compile time when optimizing std::to_array > + > +#include > + > +constexpr int N = 512 * 512; > + > +std::array > +make_std_array(int (&a)[N]) > +{ > + return std::to_array(a); > +} > -- > 2.40.1 > >