From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-vs1-xe29.google.com (mail-vs1-xe29.google.com [IPv6:2607:f8b0:4864:20::e29]) by sourceware.org (Postfix) with ESMTPS id BACB9385842E for ; Thu, 31 Aug 2023 13:57:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BACB9385842E 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 Received: by mail-vs1-xe29.google.com with SMTP id ada2fe7eead31-44e8984b724so473790137.1 for ; Thu, 31 Aug 2023 06:57:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.washington.edu; s=goo201206; t=1693490238; x=1694095038; 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=D4P4Cmff2YmAtz8BozWv24fNj6Zq8zJaGNjUu9heLV0=; b=i46nMoxeAOoQP7ka8WDZ1aez3EFUpk7tY9+mf/RH3DTbxWvS99BtwATky+wR+yQWaH WM9MnF44a/Y42O0wGhvG0FeKv0cTiia8zaIPBHJatql6S37kQv+vJepCppFaXKM83JfP mMKU5aIbteR9xTI+kOo9MqxVxKvWBTqxpfWtk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693490238; x=1694095038; 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=D4P4Cmff2YmAtz8BozWv24fNj6Zq8zJaGNjUu9heLV0=; b=TeLx0GGJMiSHM9MLCGI5UEwQx9/kpPjpi7ZHtCayFOPRNeRlSXRF/S5lqF+BSj/wt5 Ws5vdHBNcdXRFyj6jzvSzbYGjx6fmlLgzRawg4TvYiwTGg3j9/nWFoVNXnyz39wP+pUE 5SOsck/oZzm/DPaGIxcex8VgawlYYr9CMyd6+UYnl5856PLaaJ7LcAedEsAJcKBYdupY MU+v9hZsLX4wD+zDyUBJsibtCu1kI/e/dVtKvIlqsNalOLrT6H03hzNi2MgNBXi7df8V YtmHtWWwE9nVq57TAGdMZGlt3YII1IoQAde9KiKPFPZL+Pwo+yGHswIp9QnE1bHxdF57 NnXQ== X-Gm-Message-State: AOJu0Yzw6Sc3PlG7k5E6yCm+4FGoLfGB9lV6zF3ALhfZbdeKzPECWZdU b59wjV4SrrOIETOW4jdajFRcupfHr7D86oEu+eHFm//VYro3Aa5qN/ewtg== X-Google-Smtp-Source: AGHT+IFGWZvkNZ85JrEw0wmcFQ/nna9EVRiROU2Q5+TmGjqxMWKwyLxUbiLBiL4OMHY7BCEFh0NfD1KYZPxROWnYpLI= X-Received: by 2002:a05:6102:a2d:b0:44e:bad5:97e3 with SMTP id 13-20020a0561020a2d00b0044ebad597e3mr1393545vsb.5.1693490238072; Thu, 31 Aug 2023 06:57:18 -0700 (PDT) MIME-Version: 1.0 References: <20230709125715.26884-1-kmatsui@gcc.gnu.org> <20230715045519.50684-1-kmatsui@gcc.gnu.org> <20230715045519.50684-3-kmatsui@gcc.gnu.org> <3aa7cf30-27f1-7e69-7334-fc9918928f90@gmail.com> In-Reply-To: From: Ken Matsui Date: Thu, 31 Aug 2023 06:57:02 -0700 Message-ID: Subject: Re: [PATCH v2 3/3] libstdc++: Optimize is_fundamental performance by __is_arithmetic built-in To: Jonathan Wakely Cc: =?UTF-8?Q?Fran=C3=A7ois_Dumont?= , Ken Matsui , gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=unavailable 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 Tue, Aug 8, 2023 at 1:19=E2=80=AFPM Jonathan Wakely = wrote: > > > > On Tue, 18 Jul 2023 at 07:25, Ken Matsui via Libstdc++ wrote: >> >> Hi, >> >> I took a benchmark for this. >> >> https://github.com/ken-matsui/gcc-benches/blob/main/is_fundamental-disju= nction.md#mon-jul-17-105937-pm-pdt-2023 >> >> template >> struct is_fundamental >> : public std::bool_constant<__is_arithmetic(_Tp) >> || std::is_void<_Tp>::value >> || std::is_null_pointer<_Tp>::value> >> { }; >> >> is faster than: >> >> template >> struct is_fundamental >> : public std::bool_constant<__is_arithmetic(_Tp) >> || std::disjunction, >> std::is_null_pointer<_Tp= > >> >::value> >> { }; >> >> Time: -32.2871% >> Peak Memory: -18.5071% >> Total Memory: -20.1991% > > > But what about the fallback implementation of is_fundamental where we don= 't have the __is_arithmetic built-in? That fallback implementation would be this: https://github.com/ken-matsui/gsoc23/blob/967e20770599f2a8925c9794669111fae= f11beb7/is_fundamental.cc#L11-L15. The is_fundamental-disjunction.cc benchmark used the USE_BUILTIN macro, but in this benchmark, I used it to just switch two different implementations that use the __is_arithmetic built-in. > - : public __or_, is_void<_Tp>, > - is_null_pointer<_Tp>>::type > + : public __bool_constant::value > + || is_void<_Tp>::value > + || is_null_pointer<_Tp>::value> > > Here the use of __or_ means that for is_fundamental we don't instant= iate is_void and is_null_pointer. Isn't that still worthwhile? > Let me take a benchmark with __or_ later! We may see a difference. > > > >> >> >> Sincerely, >> Ken Matsui >> >> On Sun, Jul 16, 2023 at 9:49=E2=80=AFPM Ken Matsui wrote: >> > >> > On Sun, Jul 16, 2023 at 5:41=E2=80=AFAM Fran=C3=A7ois Dumont wrote: >> > > >> > > >> > > On 15/07/2023 06:55, Ken Matsui via Libstdc++ wrote: >> > > > This patch optimizes the performance of the is_fundamental trait b= y >> > > > dispatching to the new __is_arithmetic built-in trait. >> > > > >> > > > libstdc++-v3/ChangeLog: >> > > > >> > > > * include/std/type_traits (is_fundamental_v): Use __is_arith= metic >> > > > built-in trait. >> > > > (is_fundamental): Likewise. Optimize the original implementa= tion. >> > > > >> > > > Signed-off-by: Ken Matsui >> > > > --- >> > > > libstdc++-v3/include/std/type_traits | 21 +++++++++++++++++---- >> > > > 1 file changed, 17 insertions(+), 4 deletions(-) >> > > > >> > > > diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/i= nclude/std/type_traits >> > > > index 7ebbe04c77b..cf24de2fcac 100644 >> > > > --- a/libstdc++-v3/include/std/type_traits >> > > > +++ b/libstdc++-v3/include/std/type_traits >> > > > @@ -668,11 +668,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION >> > > > #endif >> > > > >> > > > /// is_fundamental >> > > > +#if __has_builtin(__is_arithmetic) >> > > > + template >> > > > + struct is_fundamental >> > > > + : public __bool_constant<__is_arithmetic(_Tp) >> > > > + || is_void<_Tp>::value >> > > > + || is_null_pointer<_Tp>::value> >> > > > + { }; >> > > >> > > What about doing this ? >> > > >> > > template >> > > struct is_fundamental >> > > : public __bool_constant<__is_arithmetic(_Tp) >> > > || __or_, >> > > is_null_pointer<_Tp>>::value> >> > > { }; >> > > >> > > Based on your benches it seems that builtin __is_arithmetic is much = better that std::is_arithmetic. But __or_ could still avoid instantiation o= f is_null_pointer. >> > > >> > Let me take a benchmark for this later. >>