[libcxx-commits] [libcxx] Speed up legacy (non-builtin) std::invoke() compile times by ~45% (PR #213403)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jul 31 22:58:03 PDT 2026


https://github.com/higher-performance updated https://github.com/llvm/llvm-project/pull/213403

>From 49bb0cebcd6e3cc89ad1f7334cc0e2be342d89d7 Mon Sep 17 00:00:00 2001
From: higher-performance <higher.performance.github at gmail.com>
Date: Sat, 1 Aug 2026 00:40:29 -0400
Subject: [PATCH] Speed up legacy implementation of std::invoke() by 50%

---
 libcxx/include/__functional/invoke.h  |   4 +-
 libcxx/include/__type_traits/invoke.h | 160 +++++++++++---------------
 2 files changed, 71 insertions(+), 93 deletions(-)

diff --git a/libcxx/include/__functional/invoke.h b/libcxx/include/__functional/invoke.h
index f7d6a23a4e520..823c5c1532800 100644
--- a/libcxx/include/__functional/invoke.h
+++ b/libcxx/include/__functional/invoke.h
@@ -24,8 +24,8 @@
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _Fn, class... _Args>
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 invoke_result_t<_Fn, _Args...>
-invoke(_Fn&& __f, _Args&&... __args) noexcept(is_nothrow_invocable_v<_Fn, _Args...>) {
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __sfinae_invoke_result_t<_Fn, _Args...>
+invoke(_Fn&& __f, _Args&&... __args) noexcept(__is_nothrow_invocable_v<_Fn, _Args...>) {
   return std::__invoke(std::forward<_Fn>(__f), std::forward<_Args>(__args)...);
 }
 
diff --git a/libcxx/include/__type_traits/invoke.h b/libcxx/include/__type_traits/invoke.h
index ba8202576593e..4759e706a6686 100644
--- a/libcxx/include/__type_traits/invoke.h
+++ b/libcxx/include/__type_traits/invoke.h
@@ -21,7 +21,8 @@
 #include <__type_traits/is_reference_wrapper.h>
 #include <__type_traits/is_same.h>
 #include <__type_traits/is_void.h>
-#include <__type_traits/nat.h>
+#include <__type_traits/remove_cvref.h>
+#include <__type_traits/type_identity.h>
 #include <__type_traits/void_t.h>
 #include <__utility/declval.h>
 #include <__utility/forward.h>
@@ -74,8 +75,11 @@ template <class, class... _Args>
 struct __invoke_result_impl {};
 
 template <class... _Args>
-struct __invoke_result_impl<__void_t<decltype(__builtin_invoke(std::declval<_Args>()...))>, _Args...> {
-  using type _LIBCPP_NODEBUG = decltype(__builtin_invoke(std::declval<_Args>()...));
+using __sfinae_invoke_result_t = decltype(__builtin_invoke(std::declval<_Args>()...));
+
+template <class... _Args>
+struct __invoke_result_impl<__void_t<__sfinae_invoke_result_t<_Args...> >, _Args...> {
+  using type _LIBCPP_NODEBUG = __sfinae_invoke_result_t<_Args...>;
 };
 
 template <class... _Args>
@@ -192,32 +196,27 @@ using __enable_if_bullet6 _LIBCPP_NODEBUG =
 
 // __invoke forward declarations
 
-// fall back - none of the bullets
-
-template <class... _Args>
-__nat __invoke(_Args&&... __args);
-
 // bullets 1, 2 and 3
 
 // clang-format off
 template <class _Fp, class _A0, class... _Args, class = __enable_if_bullet1<_Fp, _A0> >
 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
 decltype((std::declval<_A0>().*std::declval<_Fp>())(std::declval<_Args>()...))
-__invoke(_Fp&& __f, _A0&& __a0, _Args&&... __args)
+__invoke_member(_Fp&& __f, _A0&& __a0, _Args&&... __args)
     _NOEXCEPT_(noexcept((static_cast<_A0&&>(__a0).*__f)(static_cast<_Args&&>(__args)...)))
                { return (static_cast<_A0&&>(__a0).*__f)(static_cast<_Args&&>(__args)...); }
 
 template <class _Fp, class _A0, class... _Args, class = __enable_if_bullet2<_Fp, _A0> >
 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
 decltype((std::declval<_A0>().get().*std::declval<_Fp>())(std::declval<_Args>()...))
-__invoke(_Fp&& __f, _A0&& __a0, _Args&&... __args)
+__invoke_member(_Fp&& __f, _A0&& __a0, _Args&&... __args)
     _NOEXCEPT_(noexcept((__a0.get().*__f)(static_cast<_Args&&>(__args)...)))
                { return (__a0.get().*__f)(static_cast<_Args&&>(__args)...); }
 
 template <class _Fp, class _A0, class... _Args, class = __enable_if_bullet3<_Fp, _A0> >
 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
 decltype(((*std::declval<_A0>()).*std::declval<_Fp>())(std::declval<_Args>()...))
-__invoke(_Fp&& __f, _A0&& __a0, _Args&&... __args)
+__invoke_member(_Fp&& __f, _A0&& __a0, _Args&&... __args)
     _NOEXCEPT_(noexcept(((*static_cast<_A0&&>(__a0)).*__f)(static_cast<_Args&&>(__args)...)))
                { return ((*static_cast<_A0&&>(__a0)).*__f)(static_cast<_Args&&>(__args)...); }
 
@@ -226,24 +225,33 @@ __invoke(_Fp&& __f, _A0&& __a0, _Args&&... __args)
 template <class _Fp, class _A0, class = __enable_if_bullet4<_Fp, _A0> >
 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
 decltype(std::declval<_A0>().*std::declval<_Fp>())
-__invoke(_Fp&& __f, _A0&& __a0)
+__invoke_member(_Fp&& __f, _A0&& __a0)
     _NOEXCEPT_(noexcept(static_cast<_A0&&>(__a0).*__f))
                { return static_cast<_A0&&>(__a0).*__f; }
 
 template <class _Fp, class _A0, class = __enable_if_bullet5<_Fp, _A0> >
 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
 decltype(std::declval<_A0>().get().*std::declval<_Fp>())
-__invoke(_Fp&& __f, _A0&& __a0)
+__invoke_member(_Fp&& __f, _A0&& __a0)
     _NOEXCEPT_(noexcept(__a0.get().*__f))
                { return __a0.get().*__f; }
 
 template <class _Fp, class _A0, class = __enable_if_bullet6<_Fp, _A0> >
 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
 decltype((*std::declval<_A0>()).*std::declval<_Fp>())
-__invoke(_Fp&& __f, _A0&& __a0)
+__invoke_member(_Fp&& __f, _A0&& __a0)
     _NOEXCEPT_(noexcept((*static_cast<_A0&&>(__a0)).*__f))
                { return (*static_cast<_A0&&>(__a0)).*__f; }
 
+// bullets < 7
+
+template <class _Fp, class... _Args>
+inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
+decltype(std::__invoke_member(std::declval<__enable_if_t<is_member_pointer<__remove_cvref_t<_Fp> >::value, _Fp>&&>(), std::declval<_Args&&>()...))
+__invoke(_Fp&& __f, _Args&&... __args)
+    _NOEXCEPT_(noexcept(std::__invoke_member(static_cast<_Fp&&>(__f), static_cast<_Args&&>(__args)...)))
+               { return std::__invoke_member(static_cast<_Fp&&>(__f), static_cast<_Args&&>(__args)...); }
+
 // bullet 7
 
 template <class _Fp, class... _Args>
@@ -254,106 +262,76 @@ __invoke(_Fp&& __f, _Args&&... __args)
                { return static_cast<_Fp&&>(__f)(static_cast<_Args&&>(__args)...); }
 // clang-format on
 
-// __invokable
-template <class _Ret, class _Fp, class... _Args>
-struct __invokable_r {
-  template <class _XFp, class... _XArgs>
-  static decltype(std::__invoke(std::declval<_XFp>(), std::declval<_XArgs>()...)) __try_call(int);
-  template <class _XFp, class... _XArgs>
-  static __nat __try_call(...);
-
-  // FIXME: Check that _Ret, _Fp, and _Args... are all complete types, cv void,
-  // or incomplete array types as required by the standard.
-  using _Result _LIBCPP_NODEBUG = decltype(__try_call<_Fp, _Args...>(0));
-
-  using type              = __conditional_t<_IsNotSame<_Result, __nat>::value,
-                                            __conditional_t<is_void<_Ret>::value, true_type, __is_core_convertible<_Result, _Ret> >,
-                                            false_type>;
-  static const bool value = type::value;
-};
-template <class _Fp, class... _Args>
-using __is_invocable _LIBCPP_NODEBUG = __invokable_r<void, _Fp, _Args...>;
-
-template <bool _IsInvokable, bool _IsCVVoid, class _Ret, class _Fp, class... _Args>
-struct __nothrow_invokable_r_imp {
-  static const bool value = false;
-};
+// This is cheaper to compute than the non-SFINAE version, so prefer it when possible.
+template <class _XFp, class... _XArgs>
+using __sfinae_invoke_result_t = decltype(std::__invoke(std::declval<_XFp>(), std::declval<_XArgs>()...));
 
-template <class _Ret, class _Fp, class... _Args>
-struct __nothrow_invokable_r_imp<true, false, _Ret, _Fp, _Args...> {
-  typedef __nothrow_invokable_r_imp _ThisT;
+template <class _Void, class _Fp, class... _Args>
+inline const auto __is_invocable_enable_if = enable_if<false>();
 
-  template <class _Tp>
-  static void __test_noexcept(_Tp) _NOEXCEPT;
+template <class _Fp, class... _Args>
+inline const auto __is_invocable_enable_if<__void_t<__sfinae_invoke_result_t<_Fp, _Args...> >, _Fp, _Args...> =
+    enable_if<true, __sfinae_invoke_result_t<_Fp, _Args...> >();
 
-#  ifdef _LIBCPP_CXX03_LANG
-  static const bool value = false;
-#  else
-  static const bool value =
-      noexcept(_ThisT::__test_noexcept<_Ret>(std::__invoke(std::declval<_Fp>(), std::declval<_Args>()...)));
-#  endif
-};
+// __invokable
+template <class, class _Ret, class _Fp, class... _Args>
+inline const bool __is_invocable_r_impl = false;
 
 template <class _Ret, class _Fp, class... _Args>
-struct __nothrow_invokable_r_imp<true, true, _Ret, _Fp, _Args...> {
-#  ifdef _LIBCPP_CXX03_LANG
-  static const bool value = false;
-#  else
-  static const bool value = noexcept(std::__invoke(std::declval<_Fp>(), std::declval<_Args>()...));
-#  endif
-};
+inline const bool __is_invocable_r_impl<decltype(void(std::__invoke(std::declval<_Fp>(), std::declval<_Args>()...))),
+                                        _Ret,
+                                        _Fp,
+                                        _Args...> =
+    is_void<_Ret>::value || __is_core_convertible<__sfinae_invoke_result_t<_Fp, _Args...>, _Ret>::value;
 
 template <class _Ret, class _Fp, class... _Args>
-using __nothrow_invokable_r _LIBCPP_NODEBUG =
-    __nothrow_invokable_r_imp<__invokable_r<_Ret, _Fp, _Args...>::value, is_void<_Ret>::value, _Ret, _Fp, _Args...>;
+inline const bool __is_invocable_r_v = __is_invocable_r_impl<void, _Ret, _Fp, _Args...>;
+
+template <class _Func, class... _Args>
+inline const bool __is_invocable_v = __is_invocable_r_v<void, _Func, _Args...>;
 
 template <class _Fp, class... _Args>
-using __nothrow_invokable _LIBCPP_NODEBUG =
-    __nothrow_invokable_r_imp<__is_invocable<_Fp, _Args...>::value, true, void, _Fp, _Args...>;
+using __is_invocable _LIBCPP_NODEBUG = bool_constant<__is_invocable_v<_Fp, _Args...> >;
 
 template <class _Func, class... _Args>
-inline const bool __is_invocable_v = __is_invocable<_Func, _Args...>::value;
+inline const bool __is_nothrow_invocable_v = noexcept(std::__invoke(std::declval<_Func>(), std::declval<_Args>()...));
 
-template <class _Ret, class _Func, class... _Args>
-inline const bool __is_invocable_r_v = __invokable_r<_Ret, _Func, _Args...>::value;
+template <bool _IsInvokable, class _Ret, class _Fp, class... _Args>
+inline const bool __is_nothrow_invocable_r_v_imp = false;
 
-template <class _Func, class... _Args>
-inline const bool __is_nothrow_invocable_v = __nothrow_invokable<_Func, _Args...>::value;
+template <class _Ret, class _Fp, class... _Args>
+inline const bool __is_nothrow_invocable_r_v_imp<true, _Ret, _Fp, _Args...> =
+    noexcept(static_cast<void (*)(_Ret)>(0)(std::__invoke(std::declval<_Fp>(), std::declval<_Args>()...)));
 
-template <class _Ret, class _Func, class... _Args>
-inline const bool __is_nothrow_invocable_r_v = __nothrow_invokable_r<_Ret, _Func, _Args...>::value;
+template <class _Fp, class... _Args>
+inline const bool __is_nothrow_invocable_r_v_imp<true, void, _Fp, _Args...> = __is_nothrow_invocable_v<_Fp, _Args...>;
+
+template <class _Ret, class _Fp, class... _Args>
+inline const bool __is_nothrow_invocable_r_v _LIBCPP_NODEBUG =
+    __is_nothrow_invocable_r_v_imp<__is_invocable_r_v<_Ret, _Fp, _Args...>,
+                                   __conditional_t<is_void<_Ret>::value, void, _Ret>,
+                                   _Fp,
+                                   _Args...>;
 
 template <class _Func, class... _Args>
-struct __invoke_result
-    : enable_if<__is_invocable_v<_Func, _Args...>, typename __invokable_r<void, _Func, _Args...>::_Result> {};
+using __invoke_result = decltype(__is_invocable_enable_if<_Func, _Args...>);
 
 template <class _Func, class... _Args>
 using __invoke_result_t _LIBCPP_NODEBUG = typename __invoke_result<_Func, _Args...>::type;
 
-#endif // __has_builtin(__builtin_invoke_r)
+#endif // __has_builtin(__builtin_invoke)
 
 template <class _Ret, class _Func, class... _Args>
 struct __is_invocable_r : integral_constant<bool, __is_invocable_r_v<_Ret, _Func, _Args...> > {};
 
-template <class _Ret, bool = is_void<_Ret>::value>
-struct __invoke_void_return_wrapper {
-  template <class... _Args>
-  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static _Ret __call(_Args&&... __args) {
-    return std::__invoke(std::forward<_Args>(__args)...);
-  }
-};
-
-template <class _Ret>
-struct __invoke_void_return_wrapper<_Ret, true> {
-  template <class... _Args>
-  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void __call(_Args&&... __args) {
+template <class _Ret, class... _Args>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Ret __invoke_r(_Args&&... __args)
+    _NOEXCEPT_(noexcept(std::__invoke(std::forward<_Args>(__args)...))) {
+  if constexpr (is_void<_Ret>::value) {
     std::__invoke(std::forward<_Args>(__args)...);
+  } else {
+    return std::__invoke(std::forward<_Args>(__args)...);
   }
-};
-
-template <class _Ret, class... _Args>
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Ret __invoke_r(_Args&&... __args) {
-  return __invoke_void_return_wrapper<_Ret>::__call(std::forward<_Args>(__args)...);
 }
 
 #if _LIBCPP_STD_VER >= 17
@@ -379,7 +357,7 @@ struct _LIBCPP_NO_SPECIALIZATIONS is_nothrow_invocable : bool_constant<__is_noth
 
 template <class _Ret, class _Fn, class... _Args>
 struct _LIBCPP_NO_SPECIALIZATIONS is_nothrow_invocable_r
-    : bool_constant<__is_nothrow_invocable_r_v<_Ret, _Fn, _Args...>> {};
+    : bool_constant<__is_nothrow_invocable_r_v<_Ret, _Fn, _Args...> > {};
 
 template <class _Fn, class... _Args>
 _LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_nothrow_invocable_v = __is_nothrow_invocable_v<_Fn, _Args...>;
@@ -389,10 +367,10 @@ _LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_nothrow_invocable_r_v =
     __is_nothrow_invocable_r_v<_Ret, _Fn, _Args...>;
 
 template <class _Fn, class... _Args>
-struct _LIBCPP_NO_SPECIALIZATIONS invoke_result : __invoke_result<_Fn, _Args...> {};
+using invoke_result_t = __invoke_result_t<_Fn, _Args...>;
 
 template <class _Fn, class... _Args>
-using invoke_result_t = __invoke_result_t<_Fn, _Args...>;
+struct _LIBCPP_NO_SPECIALIZATIONS invoke_result : __type_identity<invoke_result_t<_Fn, _Args...> > {};
 
 #endif
 



More information about the libcxx-commits mailing list