[libcxx-commits] [libcxx] has `basic_string` call `basic_string_view`'s assume-valid constructor (PR #105863)
Christopher Di Bella via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Aug 23 10:32:39 PDT 2024
https://github.com/cjdb created https://github.com/llvm/llvm-project/pull/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.
This was originally #105441, but I somehow messed up and rebased an older version of LLVM onto ToT.
>From 7bf49eecf09d9d46bc86921721e545b7f86ed62c Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Tue, 20 Aug 2024 21:55:12 +0000
Subject: [PATCH 1/3] has `basic_string` call `basic_string_view`'s
assume-valid constructor
`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.
---
libcxx/include/string | 12 ++++++------
libcxx/include/string_view | 3 +++
2 files changed, 9 insertions(+), 6 deletions(-)
diff --git a/libcxx/include/string b/libcxx/include/string
index 6e93a6230cc2c0..cdc1afedbdf52f 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -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&
@@ -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 {
@@ -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 {
@@ -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
diff --git a/libcxx/include/string_view b/libcxx/include/string_view
index 2a03ee99e9ab52..c468dc6fc0ab7c 100644
--- a/libcxx/include/string_view
+++ b/libcxx/include/string_view
@@ -689,6 +689,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);
>From fd0fb626c27789fecf358211bc60206d2d71c644 Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Wed, 21 Aug 2024 17:06:26 +0000
Subject: [PATCH 2/3] updates formatting
---
libcxx/include/string_view | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/string_view b/libcxx/include/string_view
index c468dc6fc0ab7c..fbfbef959521a0 100644
--- a/libcxx/include/string_view
+++ b/libcxx/include/string_view
@@ -690,7 +690,7 @@ private:
const value_type* __data_;
size_type __size_;
- template<class, class, class>
+ template <class, class, class>
friend class basic_string;
};
_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(basic_string_view);
>From 4f6f58f025a0cef90d13efb539f1812cbf4bdf28 Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Fri, 23 Aug 2024 17:22:53 +0000
Subject: [PATCH 3/3] adds header
---
libcxx/include/string_view | 1 +
1 file changed, 1 insertion(+)
diff --git a/libcxx/include/string_view b/libcxx/include/string_view
index fbfbef959521a0..c81dbc9873b128 100644
--- a/libcxx/include/string_view
+++ b/libcxx/include/string_view
@@ -212,6 +212,7 @@ namespace std {
#include <__functional/unary_function.h>
#include <__fwd/ostream.h>
#include <__fwd/string_view.h>
+#include <__fwd/string.h>
#include <__iterator/bounded_iter.h>
#include <__iterator/concepts.h>
#include <__iterator/iterator_traits.h>
More information about the libcxx-commits
mailing list