Skip to content

Commit

Permalink
[libc++] Call basic_string_view's assume-valid constructor from basic…
Browse files Browse the repository at this point in the history
…_string operations (llvm#105863)

`basic_string` frequently calls `basic_string_view(data(), size())`,
which accounts for ~15% of the observed overhead when hardening is
enabled. This commit removes unnecessary checks when `basic_string` is
known to already have valid data, by bypassing the public constructor,
so that we eliminate that overhead.
  • Loading branch information
cjdb committed Aug 26, 2024
1 parent ee737c3 commit e04d124
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 6 additions & 6 deletions libcxx/include/string
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ public:
}

_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 operator __self_view() const _NOEXCEPT {
return __self_view(data(), size());
return __self_view(typename __self_view::__assume_valid(), data(), size());
}

_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS basic_string&
Expand Down Expand Up @@ -1822,7 +1822,7 @@ public:

#if _LIBCPP_STD_VER >= 20
constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(__self_view __sv) const noexcept {
return __self_view(data(), size()).starts_with(__sv);
return __self_view(typename __self_view::__assume_valid(), data(), size()).starts_with(__sv);
}

constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(value_type __c) const noexcept {
Expand All @@ -1834,7 +1834,7 @@ public:
}

constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(__self_view __sv) const noexcept {
return __self_view(data(), size()).ends_with(__sv);
return __self_view(typename __self_view::__assume_valid(), data(), size()).ends_with(__sv);
}

constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(value_type __c) const noexcept {
Expand All @@ -1848,15 +1848,15 @@ public:

#if _LIBCPP_STD_VER >= 23
constexpr _LIBCPP_HIDE_FROM_ABI bool contains(__self_view __sv) const noexcept {
return __self_view(data(), size()).contains(__sv);
return __self_view(typename __self_view::__assume_valid(), data(), size()).contains(__sv);
}

constexpr _LIBCPP_HIDE_FROM_ABI bool contains(value_type __c) const noexcept {
return __self_view(data(), size()).contains(__c);
return __self_view(typename __self_view::__assume_valid(), data(), size()).contains(__c);
}

constexpr _LIBCPP_HIDE_FROM_ABI bool contains(const value_type* __s) const {
return __self_view(data(), size()).contains(__s);
return __self_view(typename __self_view::__assume_valid(), data(), size()).contains(__s);
}
#endif

Expand Down
4 changes: 4 additions & 0 deletions libcxx/include/string_view
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ namespace std {
#include <__functional/hash.h>
#include <__functional/unary_function.h>
#include <__fwd/ostream.h>
#include <__fwd/string.h>
#include <__fwd/string_view.h>
#include <__iterator/bounded_iter.h>
#include <__iterator/concepts.h>
Expand Down Expand Up @@ -689,6 +690,9 @@ private:

const value_type* __data_;
size_type __size_;

template <class, class, class>
friend class basic_string;
};
_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(basic_string_view);

Expand Down

0 comments on commit e04d124

Please sign in to comment.