[libcxx-commits] [libcxx] [libc++] Optimize ranges::equal for vector<bool>::iterator (PR #121084)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 19 09:02:11 PST 2025


================
@@ -787,15 +671,36 @@ private:
   template <class _Dp>
   _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, false>
       rotate(__bit_iterator<_Dp, false>, __bit_iterator<_Dp, false>, __bit_iterator<_Dp, false>);
-  template <class _Dp, bool _IC1, bool _IC2>
-  _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
-      __equal_aligned(__bit_iterator<_Dp, _IC1>, __bit_iterator<_Dp, _IC1>, __bit_iterator<_Dp, _IC2>);
-  template <class _Dp, bool _IC1, bool _IC2>
+  template <class _Dp, bool _IsConst1, bool _IsConst2>
----------------
ldionne wrote:

We should refactor this whole thing using the "iterator core access" [pattern](https://www.boost.org/doc/libs/1_76_0/libs/iterator/doc/iterator_facade.html#id25). We do something like

```
struct __bit_iterator_access;

template <class _Cp, bool _IsConst, typename _Cp::__storage_type>
class __bit_iterator {
  // ... all the normal stuff

  friend class __bit_iterator_access;
};

struct __bit_iterator_access {
  template <class _Cp, bool _IsConst, typename _Cp::__storage_type>
  static typename _Cp::__storage_type& __seg(__bit_iterator<_Cp, _IsConst, ...> __iterator) {
    return __iterator.__seg_;
  }

  // etc...
  // you can have typedefs here too if that's helpful
};
```

Now, all the algorithms who need to access privates of `__bit_iterator` can simply use `__bit_iterator_access::whatever` to access it, and we can get rid of the large number of friends declared in `__bit_iterator`.

This shouldn't be done in this patch, it can be pursued later as a cleanup.

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


More information about the libcxx-commits mailing list