Skip to content

Commit

Permalink
try to optimize string swapping
Browse files Browse the repository at this point in the history
  • Loading branch information
cor3ntin committed Oct 8, 2024
1 parent 7859fce commit a171402
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
10 changes: 5 additions & 5 deletions libcxx/include/__utility/swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion libcxx/include/string
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,8 @@ struct __uninitialized_size_tag {};
struct __init_with_sentinel_tag {};

template <class _CharT, class _Traits, class _Allocator>
class basic_string memberwise_trivially_relocatable {
class basic_string memberwise_trivially_relocatable
memberwise_replaceable {

private:
using __default_allocator_type = allocator<_CharT>;
Expand Down Expand Up @@ -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<allocator_type>)
#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(),
Expand Down

0 comments on commit a171402

Please sign in to comment.