[libcxx-commits] [libcxx] [DRAFT] [libc++] Add `__is_transparently_comparable_v` specializations for `basic_string_view` (PR #195871)
Michael Levine via libcxx-commits
libcxx-commits at lists.llvm.org
Tue May 5 10:06:46 PDT 2026
https://github.com/levinem updated https://github.com/llvm/llvm-project/pull/195871
>From a25cc50d919a949e3403f91b499f2db76a0f463b Mon Sep 17 00:00:00 2001
From: mlevine55 <mlevine55 at bloomberg.net>
Date: Tue, 5 May 2026 11:15:47 -0400
Subject: [PATCH 1/4] Added the optimizations
Signed-off-by: mlevine55 <mlevine55 at bloomberg.net>
---
libcxx/include/string | 23 +++++++++++++++
libcxx/include/string_view | 27 ++++++++++++++++++
..._transparently_comparable.compile.pass.cpp | 28 +++++++++++++++++++
3 files changed, 78 insertions(+)
diff --git a/libcxx/include/string b/libcxx/include/string
index a00b824e5150a..82deee9085027 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -2599,6 +2599,29 @@ template <class _Comparator, class _CharT, class _Traits, class _Alloc, size_t _
inline const bool __is_transparently_comparable_v<_Comparator, _CharT[_Np], basic_string<_CharT, _Traits, _Alloc> > =
__is_transparently_comparable_v<_Comparator, basic_string<_CharT, _Traits, _Alloc>, const _CharT*>;
+template <class _Comparator, class _CharT, class _Traits, class _Alloc>
+inline const bool
+ __is_transparently_comparable_v<_Comparator,
+ basic_string<_CharT, _Traits, _Alloc>,
+ basic_string_view<_CharT, _Traits> > =
+ __desugars_to_v<__less_tag,
+ _Comparator,
+ basic_string<_CharT, _Traits, _Alloc>,
+ basic_string<_CharT, _Traits, _Alloc> > ||
+ __desugars_to_v<__greater_tag,
+ _Comparator,
+ basic_string<_CharT, _Traits, _Alloc>,
+ basic_string<_CharT, _Traits, _Alloc> >;
+
+template <class _Comparator, class _CharT, class _Traits, class _Alloc>
+inline const bool
+ __is_transparently_comparable_v<_Comparator,
+ basic_string_view<_CharT, _Traits>,
+ basic_string<_CharT, _Traits, _Alloc> > =
+ __is_transparently_comparable_v<_Comparator,
+ basic_string<_CharT, _Traits, _Alloc>,
+ basic_string_view<_CharT, _Traits> >;
+
# if _LIBCPP_STD_VER >= 17
template <class _InputIterator,
class _CharT = __iterator_value_type<_InputIterator>,
diff --git a/libcxx/include/string_view b/libcxx/include/string_view
index 5dd04a9ba8479..0ea8b5f3965f5 100644
--- a/libcxx/include/string_view
+++ b/libcxx/include/string_view
@@ -217,6 +217,7 @@ namespace std {
# include <__cstddef/ptrdiff_t.h>
# include <__cstddef/size_t.h>
# include <__functional/hash.h>
+# include <__functional/is_transparent.h>
# include <__functional/unary_function.h>
# include <__fwd/ostream.h>
# include <__fwd/string.h>
@@ -234,11 +235,13 @@ namespace std {
# include <__ranges/size.h>
# include <__string/char_traits.h>
# include <__type_traits/is_array.h>
+# include <__type_traits/desugars_to.h>
# include <__type_traits/is_convertible.h>
# include <__type_traits/is_same.h>
# include <__type_traits/is_standard_layout.h>
# include <__type_traits/is_trivially_constructible.h>
# include <__type_traits/is_trivially_copyable.h>
+# include <__type_traits/remove_cv.h>
# include <__type_traits/remove_cvref.h>
# include <__type_traits/remove_reference.h>
# include <__type_traits/type_identity.h>
@@ -933,6 +936,30 @@ template <>
struct hash<basic_string_view<wchar_t, char_traits<wchar_t> > > : __string_view_hash<wchar_t> {};
# endif
+template <class _Comparator, class _CharT2, class _CharT, class _Traits>
+inline const bool __is_transparently_comparable_v<_Comparator, basic_string_view<_CharT, _Traits>, _CharT2*> =
+ is_same<_CharT, __remove_cv_t<_CharT2> >::value &&
+ (__desugars_to_v<__less_tag,
+ _Comparator,
+ basic_string_view<_CharT, _Traits>,
+ basic_string_view<_CharT, _Traits> > ||
+ __desugars_to_v<__greater_tag,
+ _Comparator,
+ basic_string_view<_CharT, _Traits>,
+ basic_string_view<_CharT, _Traits> >);
+
+template <class _Comparator, class _CharT2, class _CharT, class _Traits>
+inline const bool __is_transparently_comparable_v<_Comparator, _CharT2*, basic_string_view<_CharT, _Traits> > =
+ __is_transparently_comparable_v<_Comparator, basic_string_view<_CharT, _Traits>, _CharT2*>;
+
+template <class _Comparator, class _CharT, class _Traits, size_t _Np>
+inline const bool __is_transparently_comparable_v<_Comparator, basic_string_view<_CharT, _Traits>, _CharT[_Np]> =
+ __is_transparently_comparable_v<_Comparator, basic_string_view<_CharT, _Traits>, const _CharT*>;
+
+template <class _Comparator, class _CharT, class _Traits, size_t _Np>
+inline const bool __is_transparently_comparable_v<_Comparator, _CharT[_Np], basic_string_view<_CharT, _Traits> > =
+ __is_transparently_comparable_v<_Comparator, basic_string_view<_CharT, _Traits>, const _CharT*>;
+
# if _LIBCPP_STD_VER >= 14
inline namespace literals {
inline namespace string_view_literals {
diff --git a/libcxx/test/libcxx/type_traits/is_transparently_comparable.compile.pass.cpp b/libcxx/test/libcxx/type_traits/is_transparently_comparable.compile.pass.cpp
index 8ab8cb0be8418..870a77cf33abc 100644
--- a/libcxx/test/libcxx/type_traits/is_transparently_comparable.compile.pass.cpp
+++ b/libcxx/test/libcxx/type_traits/is_transparently_comparable.compile.pass.cpp
@@ -10,8 +10,10 @@
#include <functional>
#include <string>
+#include <string_view>
#include <__type_traits/desugars_to.h>
+// basic_string with char pointers/arrays
static_assert(std::__is_transparently_comparable_v<std::less<std::string>, std::string, const char*>, "");
static_assert(std::__is_transparently_comparable_v<std::less<std::string>, std::string, char*>, "");
static_assert(std::__is_transparently_comparable_v<std::less<std::string>, std::string, char[5]>, "");
@@ -30,3 +32,29 @@ static_assert(
static_assert(
!std::__is_transparently_comparable_v<std::less<std::reference_wrapper<std::string> >, char const*, std::string>,
"");
+
+// basic_string_view with char pointers/arrays
+static_assert(
+ std::__is_transparently_comparable_v<std::less<std::string_view>, std::string_view, const char*>, "");
+static_assert(std::__is_transparently_comparable_v<std::less<std::string_view>, std::string_view, char*>, "");
+static_assert(std::__is_transparently_comparable_v<std::less<std::string_view>, std::string_view, char[5]>, "");
+
+static_assert(
+ std::__is_transparently_comparable_v<std::less<std::string_view>, const char*, std::string_view>, "");
+static_assert(std::__is_transparently_comparable_v<std::less<std::string_view>, char*, std::string_view>, "");
+static_assert(std::__is_transparently_comparable_v<std::less<std::string_view>, char[5], std::string_view>, "");
+
+static_assert(
+ !std::__is_transparently_comparable_v<std::less<std::reference_wrapper<std::string_view> >,
+ std::string_view,
+ const char*>,
+ "");
+static_assert(
+ !std::__is_transparently_comparable_v<std::less<std::reference_wrapper<std::string_view> >,
+ const char*,
+ std::string_view>,
+ "");
+
+// Cross-type: basic_string key with basic_string_view argument
+static_assert(std::__is_transparently_comparable_v<std::less<std::string>, std::string, std::string_view>, "");
+static_assert(std::__is_transparently_comparable_v<std::less<std::string>, std::string_view, std::string>, "");
>From e803867b437041b50e15d2f2fdabfe2a72a74a81 Mon Sep 17 00:00:00 2001
From: mlevine55 <mlevine55 at bloomberg.net>
Date: Tue, 5 May 2026 12:25:09 -0400
Subject: [PATCH 2/4] clang-format
Signed-off-by: mlevine55 <mlevine55 at bloomberg.net>
---
..._transparently_comparable.compile.pass.cpp | 24 ++++++++-----------
1 file changed, 10 insertions(+), 14 deletions(-)
diff --git a/libcxx/test/libcxx/type_traits/is_transparently_comparable.compile.pass.cpp b/libcxx/test/libcxx/type_traits/is_transparently_comparable.compile.pass.cpp
index 870a77cf33abc..d02f5713ce777 100644
--- a/libcxx/test/libcxx/type_traits/is_transparently_comparable.compile.pass.cpp
+++ b/libcxx/test/libcxx/type_traits/is_transparently_comparable.compile.pass.cpp
@@ -34,26 +34,22 @@ static_assert(
"");
// basic_string_view with char pointers/arrays
-static_assert(
- std::__is_transparently_comparable_v<std::less<std::string_view>, std::string_view, const char*>, "");
+static_assert(std::__is_transparently_comparable_v<std::less<std::string_view>, std::string_view, const char*>, "");
static_assert(std::__is_transparently_comparable_v<std::less<std::string_view>, std::string_view, char*>, "");
static_assert(std::__is_transparently_comparable_v<std::less<std::string_view>, std::string_view, char[5]>, "");
-static_assert(
- std::__is_transparently_comparable_v<std::less<std::string_view>, const char*, std::string_view>, "");
+static_assert(std::__is_transparently_comparable_v<std::less<std::string_view>, const char*, std::string_view>, "");
static_assert(std::__is_transparently_comparable_v<std::less<std::string_view>, char*, std::string_view>, "");
static_assert(std::__is_transparently_comparable_v<std::less<std::string_view>, char[5], std::string_view>, "");
-static_assert(
- !std::__is_transparently_comparable_v<std::less<std::reference_wrapper<std::string_view> >,
- std::string_view,
- const char*>,
- "");
-static_assert(
- !std::__is_transparently_comparable_v<std::less<std::reference_wrapper<std::string_view> >,
- const char*,
- std::string_view>,
- "");
+static_assert(!std::__is_transparently_comparable_v<std::less<std::reference_wrapper<std::string_view> >,
+ std::string_view,
+ const char*>,
+ "");
+static_assert(!std::__is_transparently_comparable_v<std::less<std::reference_wrapper<std::string_view> >,
+ const char*,
+ std::string_view>,
+ "");
// Cross-type: basic_string key with basic_string_view argument
static_assert(std::__is_transparently_comparable_v<std::less<std::string>, std::string, std::string_view>, "");
>From 19ff6be3b4f2b26bced5d8df434574906875d0f0 Mon Sep 17 00:00:00 2001
From: mlevine55 <mlevine55 at bloomberg.net>
Date: Tue, 5 May 2026 12:29:15 -0400
Subject: [PATCH 3/4] fix indendating and include ordering
Signed-off-by: mlevine55 <mlevine55 at bloomberg.net>
---
libcxx/include/string | 34 ++++++++++++++++------------------
libcxx/include/string_view | 2 +-
2 files changed, 17 insertions(+), 19 deletions(-)
diff --git a/libcxx/include/string b/libcxx/include/string
index 82deee9085027..1b3459752a88c 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -2600,27 +2600,25 @@ inline const bool __is_transparently_comparable_v<_Comparator, _CharT[_Np], basi
__is_transparently_comparable_v<_Comparator, basic_string<_CharT, _Traits, _Alloc>, const _CharT*>;
template <class _Comparator, class _CharT, class _Traits, class _Alloc>
-inline const bool
- __is_transparently_comparable_v<_Comparator,
- basic_string<_CharT, _Traits, _Alloc>,
- basic_string_view<_CharT, _Traits> > =
- __desugars_to_v<__less_tag,
- _Comparator,
- basic_string<_CharT, _Traits, _Alloc>,
- basic_string<_CharT, _Traits, _Alloc> > ||
- __desugars_to_v<__greater_tag,
- _Comparator,
- basic_string<_CharT, _Traits, _Alloc>,
- basic_string<_CharT, _Traits, _Alloc> >;
+inline const bool __is_transparently_comparable_v<_Comparator,
+ basic_string<_CharT, _Traits, _Alloc>,
+ basic_string_view<_CharT, _Traits> > =
+ __desugars_to_v<__less_tag,
+ _Comparator,
+ basic_string<_CharT, _Traits, _Alloc>,
+ basic_string<_CharT, _Traits, _Alloc> > ||
+ __desugars_to_v<__greater_tag,
+ _Comparator,
+ basic_string<_CharT, _Traits, _Alloc>,
+ basic_string<_CharT, _Traits, _Alloc> >;
template <class _Comparator, class _CharT, class _Traits, class _Alloc>
-inline const bool
+inline const bool __is_transparently_comparable_v<_Comparator,
+ basic_string_view<_CharT, _Traits>,
+ basic_string<_CharT, _Traits, _Alloc> > =
__is_transparently_comparable_v<_Comparator,
- basic_string_view<_CharT, _Traits>,
- basic_string<_CharT, _Traits, _Alloc> > =
- __is_transparently_comparable_v<_Comparator,
- basic_string<_CharT, _Traits, _Alloc>,
- basic_string_view<_CharT, _Traits> >;
+ basic_string<_CharT, _Traits, _Alloc>,
+ basic_string_view<_CharT, _Traits> >;
# if _LIBCPP_STD_VER >= 17
template <class _InputIterator,
diff --git a/libcxx/include/string_view b/libcxx/include/string_view
index 0ea8b5f3965f5..6dc3af94bb99f 100644
--- a/libcxx/include/string_view
+++ b/libcxx/include/string_view
@@ -234,8 +234,8 @@ namespace std {
# include <__ranges/enable_view.h>
# include <__ranges/size.h>
# include <__string/char_traits.h>
-# include <__type_traits/is_array.h>
# include <__type_traits/desugars_to.h>
+# include <__type_traits/is_array.h>
# include <__type_traits/is_convertible.h>
# include <__type_traits/is_same.h>
# include <__type_traits/is_standard_layout.h>
>From 45689c31a8987a44112a294d5d91c5642bb0843f Mon Sep 17 00:00:00 2001
From: mlevine55 <mlevine55 at bloomberg.net>
Date: Tue, 5 May 2026 13:06:13 -0400
Subject: [PATCH 4/4] added tests and benchmarks
Signed-off-by: mlevine55 <mlevine55 at bloomberg.net>
---
.../containers/associative/map.bench.cpp | 37 ++++++++++++
...kup_with_transparently_comparable.pass.cpp | 57 +++++++++++++++++++
2 files changed, 94 insertions(+)
diff --git a/libcxx/test/benchmarks/containers/associative/map.bench.cpp b/libcxx/test/benchmarks/containers/associative/map.bench.cpp
index cc9ffd857caf2..0228136875fb4 100644
--- a/libcxx/test/benchmarks/containers/associative/map.bench.cpp
+++ b/libcxx/test/benchmarks/containers/associative/map.bench.cpp
@@ -10,6 +10,7 @@
#include <map>
#include <string>
+#include <string_view>
#include <utility>
#include "associative_container_benchmarks.h"
@@ -29,6 +30,42 @@ static void BM_map_find_string_literal(benchmark::State& state) {
BENCHMARK(BM_map_find_string_literal);
+// Benchmark: find() with string_view vs find() with string (constructed from
+// string_view). Demonstrates the benefit of __is_transparently_comparable_v
+// for basic_string_view: the optimized path avoids constructing a temporary
+// std::string (and its potential heap allocation for keys beyond SSO).
+
+static void BM_map_find_string_view(benchmark::State& state) {
+ std::map<std::string, int> map;
+ map.emplace("Something very very long to show a long string situation", 1);
+ map.emplace("Something Else", 2);
+
+ std::string_view sv = "Something very very long to show a long string situation";
+
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(map);
+ benchmark::DoNotOptimize(map.find(sv));
+ }
+}
+
+BENCHMARK(BM_map_find_string_view);
+
+static void BM_map_find_string_constructed_from_view(benchmark::State& state) {
+ std::map<std::string, int> map;
+ map.emplace("Something very very long to show a long string situation", 1);
+ map.emplace("Something Else", 2);
+
+ std::string_view sv = "Something very very long to show a long string situation";
+
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(map);
+ // Simulates the non-optimized path: explicitly construct a std::string
+ benchmark::DoNotOptimize(map.find(std::string(sv)));
+ }
+}
+
+BENCHMARK(BM_map_find_string_constructed_from_view);
+
template <class K, class V>
struct support::adapt_operations<std::map<K, V>> {
using ValueType = typename std::map<K, V>::value_type;
diff --git a/libcxx/test/libcxx/containers/associative/lookup_with_transparently_comparable.pass.cpp b/libcxx/test/libcxx/containers/associative/lookup_with_transparently_comparable.pass.cpp
index 219df94168b55..fef6b91ab5166 100644
--- a/libcxx/test/libcxx/containers/associative/lookup_with_transparently_comparable.pass.cpp
+++ b/libcxx/test/libcxx/containers/associative/lookup_with_transparently_comparable.pass.cpp
@@ -20,6 +20,7 @@
#include <map>
#include <set>
#include <string>
+#include <string_view>
#include "count_new.h"
#include "test_macros.h"
@@ -140,5 +141,61 @@ int main(int, char**) {
}
}
+ // string_view lookups should also not allocate (tests the new string_view
+ // __is_transparently_comparable_v specializations)
+ {
+ std::map<std::string, int> c;
+ std::string_view key = "long-string-to-exceed-SSO-buffer";
+ c[std::string(key)] = 1;
+
+ // find
+ {
+ globalMemCounter.reset();
+ (void)c.find(key);
+ assert(globalMemCounter.checkNewCalledEq(0));
+ }
+ {
+ const std::map<std::string, int>& cc = c;
+ globalMemCounter.reset();
+ (void)cc.find(key);
+ assert(globalMemCounter.checkNewCalledEq(0));
+ }
+
+#if TEST_STD_VER >= 20
+ // contains
+ {
+ globalMemCounter.reset();
+ (void)c.contains(key);
+ assert(globalMemCounter.checkNewCalledEq(0));
+ }
+#endif
+
+ // at
+ {
+ globalMemCounter.reset();
+ (void)c.at(key);
+ assert(globalMemCounter.checkNewCalledEq(0));
+ }
+ {
+ const std::map<std::string, int>& cc = c;
+ globalMemCounter.reset();
+ (void)cc.at(key);
+ assert(globalMemCounter.checkNewCalledEq(0));
+ }
+ }
+
+ // string_view lookups with std::greater
+ {
+ std::map<std::string, int, std::greater<std::string>> c;
+ std::string_view key = "long-string-to-exceed-SSO-buffer";
+ c[std::string(key)] = 1;
+
+ {
+ globalMemCounter.reset();
+ (void)c.find(key);
+ assert(globalMemCounter.checkNewCalledEq(0));
+ }
+ }
+
return 0;
}
More information about the libcxx-commits
mailing list