From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qk1-x732.google.com (mail-qk1-x732.google.com [IPv6:2607:f8b0:4864:20::732]) by sourceware.org (Postfix) with ESMTPS id 3D3EA3858D32 for ; Sat, 6 Jan 2024 04:59:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3D3EA3858D32 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.washington.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.washington.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3D3EA3858D32 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::732 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704517187; cv=none; b=FfWTU5RvdJU7GW6S4Iyr3GZS3zW2LEtUPkbO25CsqiO4kzx+8dfox2cmb2+TiI3goRz34x/+Toao57VvXGcTEueomwa0m+S8IGIMpVhc0K9on4MCAlq9V+11cDSjAOESqlPPAn4mai9rLVtdZr++ONkoN+Rj1GK0AO1BcfHsipk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704517187; c=relaxed/simple; bh=H71NI/Fr69/pd61Tyv2aeE3djjs6m9hfxRIoRE1pJ4o=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=bbKZENjtm7u3tc5xhSJ1Bct+PVqX4DiiHY80WYuy4aUZXA7u/8wgemun1+7uLhbgjTh1aJupQmx/rvdjN68XxEzgbu8trkItGVP3/B/BCdwTUZ25peukEbdDCGZokgBs6xxcg96iQYKg3I7h4WpBw6q1CKrP4BlJt3WRYjlu43Q= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-qk1-x732.google.com with SMTP id af79cd13be357-7815ba075f9so26874685a.0 for ; Fri, 05 Jan 2024 20:59:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.washington.edu; s=goo201206; t=1704517183; x=1705121983; darn=gcc.gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=tEEJ9YMGC35eXq7uwQM0tn6Fk+wB7PgID0/XuqJ+8OQ=; b=U/MOxsX56zRDqnpBgvIdGT+bHMIVgF93nVmLdXRTR35HlKxBoWwo/vgXylASPHJiFu xlVQAZYaa+VEBzQQuzd9dqA60pDukZuLDhyHc0rigFjhnuacKrzhJ7GZ7d2wJAlyrOoU tCEf0NSduVKlhjwdj0IOTgDFdCNcJrbItv/KI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704517183; x=1705121983; 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=tEEJ9YMGC35eXq7uwQM0tn6Fk+wB7PgID0/XuqJ+8OQ=; b=ONwpnzn8xGlU3E0b3uiZ/y80n/XMXmawDgRntoRxOqJIjhSJj6/nyEVMmbUE+aisD/ NU5NZmmdT0YJb3tlOP1giqRkUJVQ5naaFf99c2qACgaSvwY5bVvy7xkgY6kq9XKvzdWR Hg6lO7mRISHR7RtfBeA/PYNHTxY1pFlxL8wQXpDia0+JNXaK+KRSIfq5NHo2uqdDKg54 VGXWSY44v2bijvtKhl8AvUO38q20/whoeax1l7JhkYt9Jydb1CcZ5ncfXdUBdrckHLGy jffiIIfBBVRJ8hRlRb/sOshZ6rhSRV8nLGC+aNP2DGJnifyIkBUmO0X0vTW3dATVqLA/ srNA== X-Gm-Message-State: AOJu0Yxl2g+J1c8ipFjY8JbVkFUwnrcvqnj4PFBnacNxESPijY4PmRgF Ahxs9lHtaSnYEciIE9kI4y+smqRbSDOIjNedT0Fy3td/T6Ho X-Google-Smtp-Source: AGHT+IH2rYdRm9ZzN0cm5Z16pnGqEr1JsOwKmT1i9Of8BGqnYHPZlVS2GUbH5PONCq40MX2LG6l006BU1+jk7CY7U1k= X-Received: by 2002:a05:620a:2ef:b0:781:a890:1c55 with SMTP id a15-20020a05620a02ef00b00781a8901c55mr503919qko.112.1704517183201; Fri, 05 Jan 2024 20:59:43 -0800 (PST) MIME-Version: 1.0 References: <20231223220432.712093-1-kmatsui@gcc.gnu.org> <20231223220432.712093-7-kmatsui@gcc.gnu.org> <8f87b635-c957-d4b6-f0b3-5d1378ad1dc6@idea> <34832a51-4920-465e-f4d1-53f0795a1261@idea> In-Reply-To: <34832a51-4920-465e-f4d1-53f0795a1261@idea> From: Ken Matsui Date: Fri, 5 Jan 2024 20:59:07 -0800 Message-ID: Subject: Re: [PATCH v2 6/8] libstdc++: Optimize std::is_pointer compilation performance To: Patrick Palka Cc: Ken Matsui , gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org, Jonathan Wakely Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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 Thu, Jan 4, 2024 at 2:16=E2=80=AFPM Patrick Palka wr= ote: > > On Thu, 4 Jan 2024, Patrick Palka wrote: > > > On Sat, 23 Dec 2023, Ken Matsui wrote: > > > > > This patch optimizes the compilation performance of std::is_pointer > > > by dispatching to the new __is_pointer built-in trait. > > > > > > libstdc++-v3/ChangeLog: > > > > > > * include/bits/cpp_type_traits.h (__is_pointer): Use > > > __is_pointer built-in trait. Optimize its implementation. > > > * include/std/type_traits (is_pointer): Likewise. > > > (is_pointer_v): Likewise. > > > > > > Co-authored-by: Jonathan Wakely > > > Signed-off-by: Ken Matsui > > > --- > > > libstdc++-v3/include/bits/cpp_type_traits.h | 29 ++++++++++++++ > > > libstdc++-v3/include/std/type_traits | 44 +++++++++++++++++--= -- > > > 2 files changed, 65 insertions(+), 8 deletions(-) > > > > > > diff --git a/libstdc++-v3/include/bits/cpp_type_traits.h b/libstdc++-= v3/include/bits/cpp_type_traits.h > > > index 4312f32a4e0..c348df97f72 100644 > > > --- a/libstdc++-v3/include/bits/cpp_type_traits.h > > > +++ b/libstdc++-v3/include/bits/cpp_type_traits.h > > > @@ -363,6 +363,13 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3) > > > // > > > // Pointer types > > > // > > > +#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_pointer) > > > + template > > > + struct __is_pointer : __truth_type<_IsPtr> > > > + { > > > + enum { __value =3D _IsPtr }; > > > + }; > > > +#else > > > template > > > struct __is_pointer > > > { > > > @@ -377,6 +384,28 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3) > > > typedef __true_type __type; > > > }; > > > > > > + template > > > + struct __is_pointer<_Tp* const> > > > + { > > > + enum { __value =3D 1 }; > > > + typedef __true_type __type; > > > + }; > > > + > > > + template > > > + struct __is_pointer<_Tp* volatile> > > > + { > > > + enum { __value =3D 1 }; > > > + typedef __true_type __type; > > > + }; > > > +#endif > > > + > > > + template > > > + struct __is_pointer<_Tp* const volatile> > > > + { > > > + enum { __value =3D 1 }; > > > + typedef __true_type __type; > > > + }; > > > + > > > // > > > // An arithmetic type is an integer type or a floating point type > > > // > > > diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/incl= ude/std/type_traits > > > index 30b0778e58a..d53911b2fa0 100644 > > > --- a/libstdc++-v3/include/std/type_traits > > > +++ b/libstdc++-v3/include/std/type_traits > > > @@ -542,19 +542,33 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION > > > : public true_type { }; > > > #endif > > > > > > - template > > > - struct __is_pointer_helper > > > + /// is_pointer > > > +#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_pointer) > > > + template > > > + struct is_pointer > > > + : public __bool_constant<__is_pointer(_Tp)> > > > + { }; > > > +#else > > > + template > > > + struct is_pointer > > > : public false_type { }; > > > > > > template > > > - struct __is_pointer_helper<_Tp*> > > > + struct is_pointer<_Tp*> > > > : public true_type { }; > > > > > > - /// is_pointer > > > template > > > - struct is_pointer > > > - : public __is_pointer_helper<__remove_cv_t<_Tp>>::type > > > - { }; > > > + struct is_pointer<_Tp* const> > > > + : public true_type { }; > > > + > > > + template > > > + struct is_pointer<_Tp* volatile> > > > + : public true_type { }; > > > + > > > + template > > > + struct is_pointer<_Tp* const volatile> > > > + : public true_type { }; > > > +#endif > > > > > > /// is_lvalue_reference > > > template > > > @@ -3252,8 +3266,22 @@ template > > > inline constexpr bool is_array_v<_Tp[_Num]> =3D true; > > > #endif > > > > > > +#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_pointer) > > > +template > > > + inline constexpr bool is_pointer_v =3D __is_pointer(_Tp); > > > +#else > > > template > > > - inline constexpr bool is_pointer_v =3D is_pointer<_Tp>::value; > > > + inline constexpr bool is_pointer_v =3D false; > > > +template > > > + inline constexpr bool is_pointer_v<_Tp*> =3D true; > > > +template > > > + inline constexpr bool is_pointer_v<_Tp* const> =3D true; > > > +template > > > + inline constexpr bool is_pointer_v<_Tp* volatile> =3D true; > > > +template > > > + inline constexpr bool is_pointer_v<_Tp* const volatile> =3D true; > > > > Is this fallback implementation faster than the current implementation? > > LGTM if so. > Yes. Here are the benchmarks: 1 is current impl vs. built-in, and 2 is new impl vs. built-in. There is no explicit benchmark that compares the current impl and the new impl, but I think this shows the new impl is faster. 1. https://gcc.gnu.org/pipermail/gcc-patches/2023-July/624227.html Time: -62.1344% Peak Memory Usage: -52.4281% Total Memory Usage: -53.5889% 2. https://gcc.gnu.org/pipermail/gcc-patches/2023-July/624326.html Time: -2.79488% Peak Memory Usage: -2.39379% Total Memory Usage: -3.39559% > By the way, a related optimization might be to optimize __remove_cv_t > via the built-in (related since the current fallback implementation is > in terms of __remove_cv_t). This should be safe since the alias > template isn't used in any function signature, so it's never mangled. > Do we want to try this implementation as well? I think in this is_pointer case, the explicit specialization way is enough, but I am happy to hear your thoughts. > > > > > +#endif > > > + > > > template > > > inline constexpr bool is_lvalue_reference_v =3D false; > > > template > > > -- > > > 2.43.0 > > > > > > > > >