Skip to content

Commit 2096f37

Browse files
authored
[libc++][NFC] Use __constexpr_memmove instead of copy_n in <__string/char_traits.h> (#85920)
`copy_n` has been used to allow constant evaluation of `char_traits`. We now have `__constexpr_memmove`, which `copy_n` just forwards to. We can call `__constexpr_memmove` directly, avoiding a bunch of instantiations. This reduces the time it takes to include `<string>` from 321ms to 285ms.
1 parent 95a834a commit 2096f37

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

libcxx/include/__string/char_traits.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#ifndef _LIBCPP___STRING_CHAR_TRAITS_H
1010
#define _LIBCPP___STRING_CHAR_TRAITS_H
1111

12-
#include <__algorithm/copy_n.h>
1312
#include <__algorithm/fill_n.h>
1413
#include <__algorithm/find_end.h>
1514
#include <__algorithm/find_first_of.h>
@@ -144,7 +143,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char> {
144143
copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
145144
_LIBCPP_ASSERT_NON_OVERLAPPING_RANGES(!std::__is_pointer_in_range(__s1, __s1 + __n, __s2),
146145
"char_traits::copy: source and destination ranges overlap");
147-
std::copy_n(__s2, __n, __s1);
146+
std::__constexpr_memmove(__s1, __s2, __element_count(__n));
148147
return __s1;
149148
}
150149

@@ -221,7 +220,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<wchar_t> {
221220
copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
222221
_LIBCPP_ASSERT_NON_OVERLAPPING_RANGES(!std::__is_pointer_in_range(__s1, __s1 + __n, __s2),
223222
"char_traits::copy: source and destination ranges overlap");
224-
std::copy_n(__s2, __n, __s1);
223+
std::__constexpr_memmove(__s1, __s2, __element_count(__n));
225224
return __s1;
226225
}
227226

@@ -287,7 +286,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char8_t> {
287286
copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
288287
_LIBCPP_ASSERT_NON_OVERLAPPING_RANGES(!std::__is_pointer_in_range(__s1, __s1 + __n, __s2),
289288
"char_traits::copy: source and destination ranges overlap");
290-
std::copy_n(__s2, __n, __s1);
289+
std::__constexpr_memmove(__s1, __s2, __element_count(__n));
291290
return __s1;
292291
}
293292

@@ -366,7 +365,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char16_t> {
366365
copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
367366
_LIBCPP_ASSERT_NON_OVERLAPPING_RANGES(!std::__is_pointer_in_range(__s1, __s1 + __n, __s2),
368367
"char_traits::copy: source and destination ranges overlap");
369-
std::copy_n(__s2, __n, __s1);
368+
std::__constexpr_memmove(__s1, __s2, __element_count(__n));
370369
return __s1;
371370
}
372371

@@ -454,7 +453,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char32_t> {
454453

455454
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static char_type*
456455
copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
457-
std::copy_n(__s2, __n, __s1);
456+
std::__constexpr_memmove(__s1, __s2, __element_count(__n));
458457
return __s1;
459458
}
460459

0 commit comments

Comments
 (0)