Skip to content

Commit 674792c

Browse files
authored
Merge pull request #41 from cor3ntin/corentin/string_swap
try to optimize string swapping
2 parents 12e1ae2 + af33fb7 commit 674792c

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

libcxx/include/__utility/swap.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,19 @@ void swap_value_representations(_Tp& __a, _Tp& __b) {
5959
constexpr size_t __chunk = __value_size / __size;
6060
constexpr size_t __rem = __value_size % __size;
6161

62-
// TODO: research better memswap algorithms
6362
char __buffer[__size];
63+
6464
if constexpr (__chunk) {
6565
for (std::size_t __n = 0; __n < __chunk; __n++, __aptr += __size, __bptr += __size) {
6666
__builtin_memcpy(__buffer, __aptr, __size);
67-
__builtin_memmove(__aptr, __bptr, __size);
68-
__builtin_memmove(__bptr, __buffer, __size);
67+
__builtin_memcpy(__aptr, __bptr, __size);
68+
__builtin_memcpy(__bptr, __buffer, __size);
6969
}
7070
}
7171
if constexpr (__rem) {
7272
__builtin_memcpy(__buffer, __aptr, __rem);
73-
__builtin_memmove(__aptr, __bptr, __rem);
74-
__builtin_memmove(__bptr, __buffer, __rem);
73+
__builtin_memcpy(__aptr, __bptr, __rem);
74+
__builtin_memcpy(__bptr, __buffer, __rem);
7575
}
7676
}
7777
#endif

libcxx/include/string

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,8 @@ struct __uninitialized_size_tag {};
750750
struct __init_with_sentinel_tag {};
751751

752752
template <class _CharT, class _Traits, class _Allocator>
753-
class basic_string memberwise_trivially_relocatable {
753+
class basic_string memberwise_trivially_relocatable
754+
memberwise_replaceable {
754755

755756
private:
756757
using __default_allocator_type = allocator<_CharT>;
@@ -3446,6 +3447,14 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34463447
_NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>)
34473448
#endif
34483449
{
3450+
3451+
#if _LIBCPP_STD_VER >= 26
3452+
if constexpr(std::is_same_v<allocator_type, allocator<_CharT>>) {
3453+
std::swap_value_representations(*this, __str);
3454+
return;
3455+
}
3456+
#endif
3457+
34493458
_LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
34503459
__alloc_traits::propagate_on_container_swap::value || __alloc_traits::is_always_equal::value ||
34513460
__alloc() == __str.__alloc(),

0 commit comments

Comments
 (0)