On Thu, Jan 4, 2024 at 2:16 PM Patrick Palka <ppa...@redhat.com> wrote:
>
> 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 <jwak...@redhat.com>
> > > Signed-off-by: Ken Matsui <kmat...@gcc.gnu.org>
> > > ---
> > >  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<typename _Tp, bool _IsPtr = __is_pointer(_Tp)>
> > > +    struct __is_pointer : __truth_type<_IsPtr>
> > > +    {
> > > +      enum { __value = _IsPtr };
> > > +    };
> > > +#else
> > >    template<typename _Tp>
> > >      struct __is_pointer
> > >      {
> > > @@ -377,6 +384,28 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
> > >        typedef __true_type __type;
> > >      };
> > >
> > > +  template<typename _Tp>
> > > +    struct __is_pointer<_Tp* const>
> > > +    {
> > > +      enum { __value = 1 };
> > > +      typedef __true_type __type;
> > > +    };
> > > +
> > > +  template<typename _Tp>
> > > +    struct __is_pointer<_Tp* volatile>
> > > +    {
> > > +      enum { __value = 1 };
> > > +      typedef __true_type __type;
> > > +    };
> > > +#endif
> > > +
> > > +  template<typename _Tp>
> > > +    struct __is_pointer<_Tp* const volatile>
> > > +    {
> > > +      enum { __value = 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/include/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<typename>
> > > -    struct __is_pointer_helper
> > > +  /// is_pointer
> > > +#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_pointer)
> > > +  template<typename _Tp>
> > > +    struct is_pointer
> > > +    : public __bool_constant<__is_pointer(_Tp)>
> > > +    { };
> > > +#else
> > > +  template<typename _Tp>
> > > +    struct is_pointer
> > >      : public false_type { };
> > >
> > >    template<typename _Tp>
> > > -    struct __is_pointer_helper<_Tp*>
> > > +    struct is_pointer<_Tp*>
> > >      : public true_type { };
> > >
> > > -  /// is_pointer
> > >    template<typename _Tp>
> > > -    struct is_pointer
> > > -    : public __is_pointer_helper<__remove_cv_t<_Tp>>::type
> > > -    { };
> > > +    struct is_pointer<_Tp* const>
> > > +    : public true_type { };
> > > +
> > > +  template<typename _Tp>
> > > +    struct is_pointer<_Tp* volatile>
> > > +    : public true_type { };
> > > +
> > > +  template<typename _Tp>
> > > +    struct is_pointer<_Tp* const volatile>
> > > +    : public true_type { };
> > > +#endif
> > >
> > >    /// is_lvalue_reference
> > >    template<typename>
> > > @@ -3252,8 +3266,22 @@ template <typename _Tp, size_t _Num>
> > >    inline constexpr bool is_array_v<_Tp[_Num]> = true;
> > >  #endif
> > >
> > > +#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_pointer)
> > > +template <typename _Tp>
> > > +  inline constexpr bool is_pointer_v = __is_pointer(_Tp);
> > > +#else
> > >  template <typename _Tp>
> > > -  inline constexpr bool is_pointer_v = is_pointer<_Tp>::value;
> > > +  inline constexpr bool is_pointer_v = false;
> > > +template <typename _Tp>
> > > +  inline constexpr bool is_pointer_v<_Tp*> = true;
> > > +template <typename _Tp>
> > > +  inline constexpr bool is_pointer_v<_Tp* const> = true;
> > > +template <typename _Tp>
> > > +  inline constexpr bool is_pointer_v<_Tp* volatile> = true;
> > > +template <typename _Tp>
> > > +  inline constexpr bool is_pointer_v<_Tp* const volatile> = 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 <typename _Tp>
> > >    inline constexpr bool is_lvalue_reference_v = false;
> > >  template <typename _Tp>
> > > --
> > > 2.43.0
> > >
> > >
> >
>

Reply via email to