From: Patrick Palka <ppalka@redhat.com>
To: Patrick Palka <ppalka@redhat.com>
Cc: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org
Subject: Re: [PATCH] libstdc++: reduce std::variant template instantiation depth
Date: Sun, 7 Jan 2024 15:45:01 -0500 (EST) [thread overview]
Message-ID: <1c418f4f-2a36-79cc-3517-957354ed45c9@idea> (raw)
In-Reply-To: <20240107203259.1705373-1-ppalka@redhat.com>
On Sun, 7 Jan 2024, Patrick Palka wrote:
> Tested on x86_64-pc-linux-gnu, does this look OK for trunk?
>
> -- >8 --
>
> The recursively defined constraints on _Variadic_union's user-defined
> destructor (necessary for maintaining trivial destructibility of the
> variant iff all of its alternatives are) effectively require a template
> instantiation depth of 3x the number of variants, with the instantiation
> stack looking like
>
> ...
> _Variadic_union<B, C, ...>
> std::is_trivially_destructible_v<_Variadic_union<B, C, ...>>
> _Variadic_union<A, B, C, ...>::~_Variadic_union()
> _Variadic_union<A, B, C, ...>
> ...
>
> Ideally the template depth should be ~equal to the number of variants
> (plus a constant). Luckily it seems we don't need to compute trivial
> destructibility of the alternatives at all from _Variadic_union, since
> its only user _Variant_storage already has that information. To that
> end this patch removes these recursive constraints and instead passes
> this information down from _Variant_storage. After this patch, the
> template instantiation depth for 87619.cc is ~270 instead of ~780.
Perhaps we should also test this change with by setting -ftemplate-depth
to something between 256 and 512:
libstdc++-v3/testsuite/20_util/variant/87619.cc | 1 +
1 file changed, 1 insertion(+)
diff --git a/libstdc++-v3/testsuite/20_util/variant/87619.cc b/libstdc++-v3/testsuite/20_util/variant/87619.cc
index 45418e16ca8..b7cfc20858a 100644
--- a/libstdc++-v3/testsuite/20_util/variant/87619.cc
+++ b/libstdc++-v3/testsuite/20_util/variant/87619.cc
@@ -16,6 +16,7 @@
// <http://www.gnu.org/licenses/>.
// { dg-do compile { target c++17 } }
+// { dg-additional-options "-ftemplate-depth=300" }
#include <variant>
#include <utility>
>
> libstdc++-v3/ChangeLog:
>
> * include/std/variant (__detail::__variant::_Variadic_union):
> Add bool __trivially_destructible template parameter.
> (__detail::__variant::_Variadic_union::~_Variadic_union):
> Use __trivially_destructible in constraints instead.
> (_Variant_storage): Pass __trivially_destructible value to
> _Variadic_union.
> ---
> libstdc++-v3/include/std/variant | 15 +++++++--------
> 1 file changed, 7 insertions(+), 8 deletions(-)
>
> diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant
> index 20a76c8aa87..4b9002e0917 100644
> --- a/libstdc++-v3/include/std/variant
> +++ b/libstdc++-v3/include/std/variant
> @@ -392,7 +392,7 @@ namespace __variant
> };
>
> // Defines members and ctors.
> - template<typename... _Types>
> + template<bool __trivially_destructible, typename... _Types>
> union _Variadic_union
> {
> _Variadic_union() = default;
> @@ -401,8 +401,8 @@ namespace __variant
> _Variadic_union(in_place_index_t<_Np>, _Args&&...) = delete;
> };
>
> - template<typename _First, typename... _Rest>
> - union _Variadic_union<_First, _Rest...>
> + template<bool __trivially_destructible, typename _First, typename... _Rest>
> + union _Variadic_union<__trivially_destructible, _First, _Rest...>
> {
> constexpr _Variadic_union() : _M_rest() { }
>
> @@ -427,13 +427,12 @@ namespace __variant
> ~_Variadic_union() = default;
>
> constexpr ~_Variadic_union()
> - requires (!is_trivially_destructible_v<_First>)
> - || (!is_trivially_destructible_v<_Variadic_union<_Rest...>>)
> + requires (!__trivially_destructible)
> { }
> #endif
>
> _Uninitialized<_First> _M_first;
> - _Variadic_union<_Rest...> _M_rest;
> + _Variadic_union<__trivially_destructible, _Rest...> _M_rest;
> };
>
> // _Never_valueless_alt is true for variant alternatives that can
> @@ -514,7 +513,7 @@ namespace __variant
> return this->_M_index != __index_type(variant_npos);
> }
>
> - _Variadic_union<_Types...> _M_u;
> + _Variadic_union<false, _Types...> _M_u;
> using __index_type = __select_index<_Types...>;
> __index_type _M_index;
> };
> @@ -552,7 +551,7 @@ namespace __variant
> return this->_M_index != static_cast<__index_type>(variant_npos);
> }
>
> - _Variadic_union<_Types...> _M_u;
> + _Variadic_union<true, _Types...> _M_u;
> using __index_type = __select_index<_Types...>;
> __index_type _M_index;
> };
> --
> 2.43.0.254.ga26002b628
>
>
next prev parent reply other threads:[~2024-01-07 20:45 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-07 20:32 Patrick Palka
2024-01-07 20:45 ` Patrick Palka [this message]
2024-01-15 19:32 ` Patrick Palka
2024-01-15 20:30 ` Jonathan Wakely
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1c418f4f-2a36-79cc-3517-957354ed45c9@idea \
--to=ppalka@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).