[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:10 PST 2025


================
@@ -55,14 +55,28 @@ TEST_CONSTEXPR_CXX20 void test_bititer_with_custom_sized_types() {
     std::vector<bool, Alloc> in(100, false, Alloc(1));
     std::vector<bool, Alloc> expected(100, true, Alloc(1));
     std::fill(in.begin(), in.end(), true);
-    assert(in == expected);
+
+    // FIXME: We are currently forced to use the standard `std::equal()` for general `input_iterator`s instead of
+    // the optimized version for `__bit_iterator`s. This is due to a recently discovered issue #126369 where the
+    // optimization fails to correctly compare `vector<bool>`s with small integral storage types. Once the issue is
+    // fixed, we should revert to the optimized version by uncommenting the last line in this code block.
+    using It = cpp17_input_iterator<std::vector<bool, Alloc>::iterator>;
+    assert(in.size() == expected.size() && std::equal(It(in.begin()), It(in.end()), It(expected.begin())));
+    // assert(in == expected); // FIXME: Uncomment this line once issue #126369 is fixed.
----------------
ldionne wrote:

This patch technically regresses functionality for `vector<bool>::operator==`. Instead of "breaking" it, I would suggest that we change https://github.com/llvm/llvm-project/blob/main/libcxx/include/__vector/comparison.h#L27 to:

```c++
template <class _Tp, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI bool
operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y) {
  ... slow but correct implementation ...

  // FIXME: Once issue #126369 has been resolved, we can revert to using std::equal here, which is a lot faster.
  // const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
  // return __sz == __y.size() && std::equal(__x.begin(), __x.end(), __y.begin());
}
```

That way, you don't need to modify the tests here and the library will still be functionally correct, although `operator==` will be slower.

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


More information about the libcxx-commits mailing list