[libcxx-commits] [libcxx] [libc++] Optimize most of the __tree search algorithms (PR #155245)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Thu Aug 28 08:52:31 PDT 2025


================
@@ -702,6 +703,41 @@ public:
 #  endif
 };
 
+#  if _LIBCPP_STD_VER >= 14
+template <class _CP, class _Key, class _Compare>
+struct __three_way_comparator<__map_value_compare<_Key, _CP, _Compare>, _CP, _CP> {
+  __three_way_comparator<_Compare, _Key, _Key> __comp_;
+
+  __three_way_comparator(const __map_value_compare<_Key, _CP, _Compare>& __comp) : __comp_(__comp.key_comp()) {}
+
+  _LIBCPP_HIDE_FROM_ABI auto operator()(const _CP& __lhs, const _CP& __rhs) const {
+    return __comp_(__lhs.first, __rhs.first);
----------------
ldionne wrote:

`__map_value_compare` is basically an instance of the `on` combinator:

```c++
auto on(func, transform) {
  return [](auto ...args) {
    return func(invoke(transform, args)...);
  };
};
```

I think we have something else that is extremely similar to that called `_ProjectedPred` (via `__make_projected`). I don't think we need to do that right now, but as a follow-up I'd like us to generalize this to use the same mechanism. The goal is for predicates created via `__make_projected` to also be picked up by this optimization, since we use `__make_projected` in many algorithms that use comparisons. That should be done in a separate patch though.

https://github.com/llvm/llvm-project/pull/155245


More information about the libcxx-commits mailing list