Skip to content

Commit

Permalink
[libc++][NFC] Use __constexpr_memmove instead of copy_n in <__string/…
Browse files Browse the repository at this point in the history
…char_traits.h> (llvm#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.
  • Loading branch information
philnik777 authored Mar 21, 2024
1 parent 95a834a commit 2096f37
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions libcxx/include/__string/char_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#ifndef _LIBCPP___STRING_CHAR_TRAITS_H
#define _LIBCPP___STRING_CHAR_TRAITS_H

#include <__algorithm/copy_n.h>
#include <__algorithm/fill_n.h>
#include <__algorithm/find_end.h>
#include <__algorithm/find_first_of.h>
Expand Down Expand Up @@ -144,7 +143,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char> {
copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
_LIBCPP_ASSERT_NON_OVERLAPPING_RANGES(!std::__is_pointer_in_range(__s1, __s1 + __n, __s2),
"char_traits::copy: source and destination ranges overlap");
std::copy_n(__s2, __n, __s1);
std::__constexpr_memmove(__s1, __s2, __element_count(__n));
return __s1;
}

Expand Down Expand Up @@ -221,7 +220,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<wchar_t> {
copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
_LIBCPP_ASSERT_NON_OVERLAPPING_RANGES(!std::__is_pointer_in_range(__s1, __s1 + __n, __s2),
"char_traits::copy: source and destination ranges overlap");
std::copy_n(__s2, __n, __s1);
std::__constexpr_memmove(__s1, __s2, __element_count(__n));
return __s1;
}

Expand Down Expand Up @@ -287,7 +286,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char8_t> {
copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
_LIBCPP_ASSERT_NON_OVERLAPPING_RANGES(!std::__is_pointer_in_range(__s1, __s1 + __n, __s2),
"char_traits::copy: source and destination ranges overlap");
std::copy_n(__s2, __n, __s1);
std::__constexpr_memmove(__s1, __s2, __element_count(__n));
return __s1;
}

Expand Down Expand Up @@ -366,7 +365,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char16_t> {
copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
_LIBCPP_ASSERT_NON_OVERLAPPING_RANGES(!std::__is_pointer_in_range(__s1, __s1 + __n, __s2),
"char_traits::copy: source and destination ranges overlap");
std::copy_n(__s2, __n, __s1);
std::__constexpr_memmove(__s1, __s2, __element_count(__n));
return __s1;
}

Expand Down Expand Up @@ -454,7 +453,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char32_t> {

_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static char_type*
copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
std::copy_n(__s2, __n, __s1);
std::__constexpr_memmove(__s1, __s2, __element_count(__n));
return __s1;
}

Expand Down

0 comments on commit 2096f37

Please sign in to comment.