From patchwork Sun Jan 7 20:32:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 185761 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:37c1:b0:101:2151:f287 with SMTP id y1csp714765dyq; Sun, 7 Jan 2024 13:37:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IFUUHpkjoV4W6u+F0oqbHyMEQMykXVPmP97JXHgQtn2JzGCNewSHWFzNtc+YtwXOizrk6dq X-Received: by 2002:a05:6214:5093:b0:67f:5c5:c035 with SMTP id kk19-20020a056214509300b0067f05c5c035mr2798872qvb.9.1704663475907; Sun, 07 Jan 2024 13:37:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1704663475; cv=pass; d=google.com; s=arc-20160816; b=cceT2hcdO2FW85Ty2nserNSpd3zK8WqsHirCgTEwd5dIF4e7MMvX8rkhplvcYDEpny VJS8YNgRo6w7lavNWiB8HZMof8nSE23mINPDcpMYYOcENYbqBwRc2ebrhMfgxNOZrbeV RxzOZ8HmlM6IBG3y45slYZ1BBf7jWHhLyl8uTZJb/vFj/QUbTKRn9yxb1OKfI9Al1sKG 6uOuVYc0g72RaMbHbDtX3g69qgyTZo7ifTN3OQPyhcWkoS6fzu50umUg1qlyq+M3Caei pqhymz+9r2fFUxOzWPqMCRavvxexgeYWKlrQzEawi+yTPd6pKcEMArfTadLrHap3keIX 2h5w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=zk5MR9jQ/0bQyfI80gKbvcG4UUt/ioBLNbb9Ub4gxYI=; fh=EVSvWd94bH3+6DLHxQIQnUkCnGLOu/mRCJOpcO366zA=; b=xEt/k0/GTmOWFRWYzK9zXl6zj/Y7lRF5JripWMZcIeD0enLQ32o/mjWdpf8O2mTRP8 +f3DihqSnIsEHjady+WRU6vh7EUhPjrRQOsiMnArRuWk7TdsY5sbIp4vyUQC1e9JTCjZ lRE20WOBkwmbsyAjA/gKFX2Lmev6EUxAr6yrQqzhUMoBSThyrnk/X2aMTbaYzfiEUMDK pjeO2bOAjGZpkz7xwezZXIFyNMxfifUrad47XWAeIIk2ppQtjXRsxwmwnH0zurEe/o+s OltQkrUJmaQ6EjOQ6ZBXUDR5LIhpXbuDVktPL6bLsq3KYM43Imu9eQ/PNMTQh4LVpCrw Pyxw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WPGYajlH; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id c7-20020a05620a268700b0078162ba620esi6818612qkp.122.2024.01.07.13.37.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Jan 2024 13:37:55 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WPGYajlH; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E086D385E446 for ; Sun, 7 Jan 2024 20:34:28 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 6CF823858C42 for ; Sun, 7 Jan 2024 20:33:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6CF823858C42 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 6CF823858C42 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=1704659590; cv=none; b=LB0C+YYDaMWil7NREj+NEpXNfXA3wzz6B1HCnVU2dzSEIbnX5czsgd2rcj9AGmt4PzaFaEp9Fb2jeXYP+hgYe90YGV4auKUZh3fEJrGH8zFhPaO5XJ3k9rMRyXKp+E5mkccGifupaTPxelnWC/zddlW3QsC231E0GT1sipTJgbo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704659590; c=relaxed/simple; bh=xYlUJlAm3SHZayzZp8g6TVTN8a6x1dOnn7yf87WzT+4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=PlHwmFqWrA8bETtxV9bDjn4YkVuOI/yStMYMXPzPRvGJuFm9Ize3CXqS/vJfUJZp+iV0zdAxc0n/xGpwFFt3pItVO3J+fekoHLcZgB9D8r/VD8nPntZaLQOgltVaXAHNNHXdIHNduUaM4iGXhFXSCrHrHdyiOsgskZRmaUgHOPU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704659589; 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; bh=zk5MR9jQ/0bQyfI80gKbvcG4UUt/ioBLNbb9Ub4gxYI=; b=WPGYajlHwON7U07QWVx3tazxCFLD1oEtFi26bCOfxgaHX+qZh5JkiBHNoBSa6SUvtxpt5/ Sk2dwA3EuMPgcaMi/LUwf/Du1y/Evav+snw/Zv5zqZUHoGhCOZAQxPLXo0aS3PETVyj7DR S/Va4U/MF1Q58yLBfy6jHXB6Nevu8TE= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-597-8c7zI9mVNg-yKvRmuX_4Eg-1; Sun, 07 Jan 2024 15:33:07 -0500 X-MC-Unique: 8c7zI9mVNg-yKvRmuX_4Eg-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-7830ab8fb5aso153380185a.2 for ; Sun, 07 Jan 2024 12:33:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704659586; x=1705264386; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zk5MR9jQ/0bQyfI80gKbvcG4UUt/ioBLNbb9Ub4gxYI=; b=a81fuazG3+bcZC5gf+x3JyB75Dq65hk4OSsv6L7GC/5cjf5uPT/VEJLLEg7tnZ3blO oIndtPBFpG0Foj0/5lbHxuSeDY8c9SSeWsTPaOul/ew3iMi1ZLSDJzEGAdhmfXAbAq15 4axhFiiya3EY0W3qrioBCSzWuxanRUyVJg4tUy4BhHkPFI/x8gjilbn+padSVF9x28zD hgc/V1kPAM33bxo9gt21YVJCS7lBKvUyW0C7qUaUjdDE4WPglUQLbGi/IDkNTe25kNes o2wvFRuTCa2xk5thcSCmnSKOubOvskAbuIwsqZypXKwRClh5RG1Ecx7TlSI6hcmHgHc1 SFpQ== X-Gm-Message-State: AOJu0YzFd60+P9gVVFzWgLZznZvxS7z3+JFQxd4GVGf89nOcY3XJ1QIO 3KdLmKKGWA9jMKRjMx6LLH/F2DKDS4Bl9gGXlk2UgZAex2CCSRnn0dprOzEKheR1H2LliWnzdMt /pafutTKKwr2ynI/U/YfhY4fROtm591bmHuT+7+WPz9wA0aRSnivqFyTrC1TN4YzABv5UovRq6O DpUSbGX88= X-Received: by 2002:a05:620a:1676:b0:783:2399:e13b with SMTP id d22-20020a05620a167600b007832399e13bmr435184qko.86.1704659585896; Sun, 07 Jan 2024 12:33:05 -0800 (PST) X-Received: by 2002:a05:620a:1676:b0:783:2399:e13b with SMTP id d22-20020a05620a167600b007832399e13bmr435172qko.86.1704659585473; Sun, 07 Jan 2024 12:33:05 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id oo12-20020a05620a530c00b0078318dac889sm827807qkn.19.2024.01.07.12.33.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Jan 2024 12:33:04 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, Patrick Palka Subject: [PATCH] libstdc++: reduce std::variant template instantiation depth Date: Sun, 7 Jan 2024 15:32:59 -0500 Message-ID: <20240107203259.1705373-1-ppalka@redhat.com> X-Mailer: git-send-email 2.43.0.254.ga26002b628 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.7 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=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787469209027542650 X-GMAIL-MSGID: 1787469209027542650 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 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() = default; @@ -401,8 +401,8 @@ namespace __variant _Variadic_union(in_place_index_t<_Np>, _Args&&...) = 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() = 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 _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 _M_u; using __index_type = __select_index<_Types...>; __index_type _M_index; };