From a171402b838b9d53ca28019bfc4bcc31667e5859 Mon Sep 17 00:00:00 2001 From: Corentin Jabot Date: Tue, 8 Oct 2024 15:49:29 +0200 Subject: [PATCH] try to optimize string swapping --- libcxx/include/__utility/swap.h | 10 +++++----- libcxx/include/string | 11 ++++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/libcxx/include/__utility/swap.h b/libcxx/include/__utility/swap.h index ed6ad3e5eb22ff..e7b23d145ea7cf 100644 --- a/libcxx/include/__utility/swap.h +++ b/libcxx/include/__utility/swap.h @@ -59,19 +59,19 @@ void swap_value_representations(_Tp& __a, _Tp& __b) { constexpr size_t __chunk = __value_size / __size; constexpr size_t __rem = __value_size % __size; - // TODO: research better memswap algorithms char __buffer[__size]; + if constexpr (__chunk) { for (std::size_t n = 0; n < __chunk; n++, __aptr += __size, __bptr += __size) { __builtin_memcpy(__buffer, __aptr, __size); - __builtin_memmove(__aptr, __bptr, __size); - __builtin_memmove(__bptr, __buffer, __size); + __builtin_memcpy(__aptr, __bptr, __size); + __builtin_memcpy(__bptr, __buffer, __size); } } if constexpr (__rem) { __builtin_memcpy(__buffer, __aptr, __rem); - __builtin_memmove(__aptr, __bptr, __rem); - __builtin_memmove(__bptr, __buffer, __rem); + __builtin_memcpy(__aptr, __bptr, __rem); + __builtin_memcpy(__bptr, __buffer, __rem); } } #endif diff --git a/libcxx/include/string b/libcxx/include/string index 1fef17db545f6e..89953d839abd9a 100644 --- a/libcxx/include/string +++ b/libcxx/include/string @@ -750,7 +750,8 @@ struct __uninitialized_size_tag {}; struct __init_with_sentinel_tag {}; template -class basic_string memberwise_trivially_relocatable { +class basic_string memberwise_trivially_relocatable + memberwise_replaceable { private: using __default_allocator_type = allocator<_CharT>; @@ -3446,6 +3447,14 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v) #endif { + +#if _LIBCPP_STD_VER >= 26 + if constexpr(__alloc_traits::is_always_equal::value) { + std::swap_value_representations(*this, __str); + return; + } +#endif + _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR( __alloc_traits::propagate_on_container_swap::value || __alloc_traits::is_always_equal::value || __alloc() == __str.__alloc(),