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.129.124]) by sourceware.org (Postfix) with ESMTPS id C5E28385E019 for ; Mon, 15 Jan 2024 19:32:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C5E28385E019 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C5E28385E019 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705347143; cv=none; b=c0U9XqPFUK0M2rbTjUWhKcy9ShN+BTq8EmzmoZCXlnldxZsFhwC8VZNGdRty52L3lZs62h1kzFt06EXyafw237hV2CCzcblky49dTFfiYQe7UsvJsioOm8ckRrFKYvEQIfwzoUveSGelCOd3Zf2Ee4VMT8bK1ronElLqiuotMJU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705347143; c=relaxed/simple; bh=WLkgxcVT5ZnbWVE+D5QhEZbM62QKH6fOdJ/cvLzktzE=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=aQLwgwx19vnHqSe2Q223VNIETW8+hLdmZ1mAMDhK3ycMKbYJr/xyzsVvanwRtOBROM6IT5iXLjvc5sqNVEvN7qZ8BQndtXsKQ/2ZtHaaOhFhnLYiuwu8Q1DlTIN1ndlE19NX4mWPRQnD8ipRasL0zjS7XWALbxuFzLtzGVWZOtA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705347141; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xbmR60CR6IBzx4TzVh2/gSd8hh43Y5gcxcqc+K5xZ5E=; b=Wa4F5EKvwYAW8jkdJN3fGj9zzC8l+zvaWgM4Pk0Scw/h7a1AjZREO24/QLrNr5x9W+k8si jD49OGhj7StSv0cSKBokm5VN52mO4Uav7+ZPK2Nf36C28PlkG2cuOAwaP0vDGbnQIaS15+ 8pfeKDDKNGdCGTFc4160/AyF4A9p3n4= Received: from mail-lj1-f199.google.com (mail-lj1-f199.google.com [209.85.208.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-351-kZP1dO_QN8uDAQyj7AGLug-1; Mon, 15 Jan 2024 14:32:19 -0500 X-MC-Unique: kZP1dO_QN8uDAQyj7AGLug-1 Received: by mail-lj1-f199.google.com with SMTP id 38308e7fff4ca-2cccd597247so79440521fa.0 for ; Mon, 15 Jan 2024 11:32:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705347138; x=1705951938; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xbmR60CR6IBzx4TzVh2/gSd8hh43Y5gcxcqc+K5xZ5E=; b=rJOXmIVnfEGFJ0EiUGs+ICJfg3rHkbdTcVumvSCndXz3vJry/b7nQOf3QztHOFbe4I zxZQWn/ADFkYzFte3Vj6JmnfFIvNIPnabGYPICK42FnCKNHw0VOUoy1mYMJCW9IUaRom n1qI0iLZtwA1DJn0kTDejo4vM75GqNOEn4Va50pKC9/nUvXY7QmMSm3jTwajoJ40n6T2 SYqugLnJHbKBxAjYqdbJ4JYTq3R6K3fkgg60XXgarU+14mJ1Twjj89nd/OOYJHAqUYOf QxpzmffrkYouYAtuqaMan0AfmeQ94I7rBQSrmhCtKFC8hHfMIWiV+c2g+MnYea/M+XwS KVIA== X-Gm-Message-State: AOJu0YwRIhOx3bvuBqWiwUcx2/HJpqxI5KaLG/hxwj26kZqp08zqi4aQ uwq8ylvx11EcPx/Mcd0Ret+ohvF4yYSlAIq+ec9mmUceU+Ro4uRjr6IEene3rnLi4SMnSy9v65Z LBfkrNT9rYcO0/2IX2q+gms6x3GuGoFsZB4pvxWIOsPylqkqWTA== X-Received: by 2002:a2e:a3cd:0:b0:2cc:a7d9:1dc9 with SMTP id w13-20020a2ea3cd000000b002cca7d91dc9mr1343842lje.121.1705347138277; Mon, 15 Jan 2024 11:32:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IGju6VabH/QCRpoQRO5rqtJXikqTaRL0ae2fgmJ3/pPunu1hF26ARb4aUJtlu3FGBccjdVD0ZQTJY6jrIEWpFc= X-Received: by 2002:a2e:a3cd:0:b0:2cc:a7d9:1dc9 with SMTP id w13-20020a2ea3cd000000b002cca7d91dc9mr1343837lje.121.1705347137782; Mon, 15 Jan 2024 11:32:17 -0800 (PST) MIME-Version: 1.0 References: <20240107203259.1705373-1-ppalka@redhat.com> In-Reply-To: <20240107203259.1705373-1-ppalka@redhat.com> From: Patrick Palka Date: Mon, 15 Jan 2024 14:32:06 -0500 Message-ID: Subject: Re: [PATCH] libstdc++: reduce std::variant template instantiation depth To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-13.5 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_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE 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 Sun, Jan 7, 2024 at 3:33=E2=80=AFPM Patrick Palka wr= ote: > > Tested on x86_64-pc-linux-gnu, does this look OK for trunk? Ping. > > -- >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 > std::is_trivially_destructible_v<_Variadic_union> > _Variadic_union::~_Variadic_union() > _Variadic_union > ... > > 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. > > 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 > + template > union _Variadic_union > { > _Variadic_union() =3D default; > @@ -401,8 +401,8 @@ namespace __variant > _Variadic_union(in_place_index_t<_Np>, _Args&&...) =3D delete; > }; > > - template > - union _Variadic_union<_First, _Rest...> > + template > + union _Variadic_union<__trivially_destructible, _First, _Rest...> > { > constexpr _Variadic_union() : _M_rest() { } > > @@ -427,13 +427,12 @@ namespace __variant > ~_Variadic_union() =3D 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 !=3D __index_type(variant_npos); > } > > - _Variadic_union<_Types...> _M_u; > + _Variadic_union _M_u; > using __index_type =3D __select_index<_Types...>; > __index_type _M_index; > }; > @@ -552,7 +551,7 @@ namespace __variant > return this->_M_index !=3D static_cast<__index_type>(variant_npos= ); > } > > - _Variadic_union<_Types...> _M_u; > + _Variadic_union _M_u; > using __index_type =3D __select_index<_Types...>; > __index_type _M_index; > }; > -- > 2.43.0.254.ga26002b628 >