[libcxx-commits] [libcxx] d814824 - [libc++] Decrease instantiation cost of __constexpr_memmove (#125109)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Feb 4 08:12:38 PST 2025
Author: Nikolas Klauser
Date: 2025-02-04T17:12:35+01:00
New Revision: d8148244e9be9d4c7b12abbdbf275d80d5ba57a5
URL: https://github.com/llvm/llvm-project/commit/d8148244e9be9d4c7b12abbdbf275d80d5ba57a5
DIFF: https://github.com/llvm/llvm-project/commit/d8148244e9be9d4c7b12abbdbf275d80d5ba57a5.diff
LOG: [libc++] Decrease instantiation cost of __constexpr_memmove (#125109)
Using `if constexpr` in `__constexpr_memmove` makes the instantiation
three times faster for the same type, since it avoids a bunch of class
instantiations and SFINAE for constexpr support that's never actually
used. Given that `__constexpr_memmove` is used quite a bit through
`std::copy` and is instantiated multiple times when just including
`<__string/char_traits.h>` this can provide a nice compile time speedup
for a very simple change.
Added:
Modified:
libcxx/include/__string/constexpr_c_functions.h
Removed:
################################################################################
diff --git a/libcxx/include/__string/constexpr_c_functions.h b/libcxx/include/__string/constexpr_c_functions.h
index 0bc128b68b5799..fbe7e10d440ce1 100644
--- a/libcxx/include/__string/constexpr_c_functions.h
+++ b/libcxx/include/__string/constexpr_c_functions.h
@@ -204,23 +204,26 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& __assign_trivially_copy
return __dest;
}
-template <class _Tp, class _Up, __enable_if_t<__is_always_bitcastable<_Up, _Tp>::value, int> = 0>
+template <class _Tp, class _Up>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp*
__constexpr_memmove(_Tp* __dest, _Up* __src, __element_count __n) {
+ static_assert(__is_always_bitcastable<_Up, _Tp>::value);
size_t __count = static_cast<size_t>(__n);
if (__libcpp_is_constant_evaluated()) {
#ifdef _LIBCPP_COMPILER_CLANG_BASED
- if (is_same<__remove_cv_t<_Tp>, __remove_cv_t<_Up> >::value) {
+ if _LIBCPP_CONSTEXPR (is_same<__remove_cv_t<_Tp>, __remove_cv_t<_Up> >::value) {
::__builtin_memmove(__dest, __src, __count * sizeof(_Tp));
return __dest;
- }
+ } else
#endif
- if (std::__is_pointer_in_range(__src, __src + __count, __dest)) {
- for (; __count > 0; --__count)
- std::__assign_trivially_copyable(__dest[__count - 1], __src[__count - 1]);
- } else {
- for (size_t __i = 0; __i != __count; ++__i)
- std::__assign_trivially_copyable(__dest[__i], __src[__i]);
+ {
+ if (std::__is_pointer_in_range(__src, __src + __count, __dest)) {
+ for (; __count > 0; --__count)
+ std::__assign_trivially_copyable(__dest[__count - 1], __src[__count - 1]);
+ } else {
+ for (size_t __i = 0; __i != __count; ++__i)
+ std::__assign_trivially_copyable(__dest[__i], __src[__i]);
+ }
}
} else if (__count > 0) {
::__builtin_memmove(__dest, __src, (__count - 1) * sizeof(_Tp) + __datasizeof_v<_Tp>);
More information about the libcxx-commits
mailing list