[libcxx-commits] [libcxx] [libc++] Speed up and refactor vector<bool> move-assignment operator [2/3] (PR #119817)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Fri Mar 14 11:50:57 PDT 2025


https://github.com/winner245 updated https://github.com/llvm/llvm-project/pull/119817

>From b419761a6ebb9e639ab028b159a6024390f57bac Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Fri, 14 Mar 2025 14:50:23 -0400
Subject: [PATCH] Enhance tests for copy/move-assignment operators

---
 .../vector.bool/assign_copy.pass.cpp          |  64 ++++++++---
 .../vector.bool/assign_move.pass.cpp          | 103 +++++++++++-------
 2 files changed, 108 insertions(+), 59 deletions(-)

diff --git a/libcxx/test/std/containers/sequences/vector.bool/assign_copy.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/assign_copy.pass.cpp
index 2144478601897..7bf8bdb9c6993 100644
--- a/libcxx/test/std/containers/sequences/vector.bool/assign_copy.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector.bool/assign_copy.pass.cpp
@@ -7,39 +7,69 @@
 //===----------------------------------------------------------------------===//
 
 // <vector>
+// vector<bool>
 
 // vector& operator=(const vector& c);
 
-#include <vector>
 #include <cassert>
-#include "test_macros.h"
-#include "test_allocator.h"
+#include <vector>
+
 #include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_macros.h"
 
-TEST_CONSTEXPR_CXX20 bool tests() {
-  {
-    std::vector<bool, test_allocator<bool> > l(3, true, test_allocator<bool>(5));
-    std::vector<bool, test_allocator<bool> > l2(l, test_allocator<bool>(3));
+TEST_CONSTEXPR_CXX20 void test_copy_assignment(unsigned N) {
+  //
+  // Test with insufficient space where reallocation occurs during assignment
+  //
+  { // POCCA = true_type, thus copy-assign the allocator
+    std::vector<bool, other_allocator<bool> > l(N, true, other_allocator<bool>(5));
+    std::vector<bool, other_allocator<bool> > l2(other_allocator<bool>(3));
     l2 = l;
     assert(l2 == l);
-    assert(l2.get_allocator() == test_allocator<bool>(3));
+    assert(l2.get_allocator() == other_allocator<bool>(5));
   }
-  {
-    std::vector<bool, other_allocator<bool> > l(3, true, other_allocator<bool>(5));
-    std::vector<bool, other_allocator<bool> > l2(l, other_allocator<bool>(3));
+  { // POCCA = false_type, thus allocator is unchanged
+    std::vector<bool, test_allocator<bool> > l(N + 64, true, test_allocator<bool>(5));
+    std::vector<bool, test_allocator<bool> > l2(10, false, test_allocator<bool>(3));
     l2 = l;
     assert(l2 == l);
-    assert(l2.get_allocator() == other_allocator<bool>(5));
+    assert(l2.get_allocator() == test_allocator<bool>(3));
   }
-#if TEST_STD_VER >= 11
-  {
-    std::vector<bool, min_allocator<bool> > l(3, true, min_allocator<bool>());
-    std::vector<bool, min_allocator<bool> > l2(l, min_allocator<bool>());
+  { // Stateless allocator
+    std::vector<bool, min_allocator<bool> > l(N + 64, true, min_allocator<bool>());
+    std::vector<bool, min_allocator<bool> > l2(N / 2, false, min_allocator<bool>());
     l2 = l;
     assert(l2 == l);
     assert(l2.get_allocator() == min_allocator<bool>());
   }
-#endif
+
+  //
+  // Test with sufficient size where no reallocation occurs during assignment
+  //
+  { // POCCA = false_type, thus allocator is unchanged
+    std::vector<bool, test_allocator<bool> > l(N, true, test_allocator<bool>(5));
+    std::vector<bool, test_allocator<bool> > l2(N + 64, false, test_allocator<bool>(3));
+    l2 = l;
+    assert(l2 == l);
+    assert(l2.get_allocator() == test_allocator<bool>(3));
+  }
+  { // POCCA = true_type, thus copy-assign the allocator
+    std::vector<bool, other_allocator<bool> > l(N, true, other_allocator<bool>(5));
+    std::vector<bool, other_allocator<bool> > l2(N * 2, false, other_allocator<bool>(3));
+    l2.reserve(5);
+    l2 = l;
+    assert(l2 == l);
+    assert(l2.get_allocator() == other_allocator<bool>(5));
+  }
+}
+
+TEST_CONSTEXPR_CXX20 bool tests() {
+  test_copy_assignment(3);
+  test_copy_assignment(18);
+  test_copy_assignment(33);
+  test_copy_assignment(65);
+  test_copy_assignment(299);
 
   return true;
 }
diff --git a/libcxx/test/std/containers/sequences/vector.bool/assign_move.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/assign_move.pass.cpp
index 48ef5e3a8c262..8791380b134c7 100644
--- a/libcxx/test/std/containers/sequences/vector.bool/assign_move.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector.bool/assign_move.pass.cpp
@@ -9,68 +9,87 @@
 // UNSUPPORTED: c++03
 
 // <vector>
+// vector<bool>
 
 // vector& operator=(vector&& c);
 
-#include <vector>
 #include <cassert>
-#include "test_macros.h"
-#include "test_allocator.h"
+#include <vector>
+
 #include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_macros.h"
 
-TEST_CONSTEXPR_CXX20 bool tests() {
-  {
-    std::vector<bool, test_allocator<bool> > l(test_allocator<bool>(5));
-    std::vector<bool, test_allocator<bool> > lo(test_allocator<bool>(5));
-    for (int i = 1; i <= 3; ++i) {
-      l.push_back(i);
-      lo.push_back(i);
-    }
-    std::vector<bool, test_allocator<bool> > l2(test_allocator<bool>(5));
+TEST_CONSTEXPR_CXX20 void test_move_assignment(unsigned N) {
+  //
+  // Testing for container move where either POCMA = true_type or the allocators compare equal
+  //
+  { // Test with POCMA = true_type
+    std::vector<bool, other_allocator<bool> > l(N, true, other_allocator<bool>(5));
+    std::vector<bool, other_allocator<bool> > lo(N, true, other_allocator<bool>(5));
+    std::vector<bool, other_allocator<bool> > l2(N + 10, false, other_allocator<bool>(42));
     l2 = std::move(l);
     assert(l2 == lo);
-    LIBCPP_ASSERT(l.empty());
+    LIBCPP_ASSERT(l.empty()); // After move, source vector is in a vliad but unspecified state. libc++ leaves it empty.
     assert(l2.get_allocator() == lo.get_allocator());
   }
-  {
-    std::vector<bool, test_allocator<bool> > l(test_allocator<bool>(5));
-    std::vector<bool, test_allocator<bool> > lo(test_allocator<bool>(5));
-    for (int i = 1; i <= 3; ++i) {
-      l.push_back(i);
-      lo.push_back(i);
-    }
-    std::vector<bool, test_allocator<bool> > l2(test_allocator<bool>(6));
+  { // Test with POCMA = false_type and allocators compare equal
+    std::vector<bool, test_allocator<bool> > l(N, true, test_allocator<bool>(5));
+    std::vector<bool, test_allocator<bool> > lo(N, true, test_allocator<bool>(5));
+    std::vector<bool, test_allocator<bool> > l2(N + 10, false, test_allocator<bool>(5));
     l2 = std::move(l);
     assert(l2 == lo);
-    assert(!l.empty());
-    assert(l2.get_allocator() == test_allocator<bool>(6));
+    LIBCPP_ASSERT(l.empty());
+    assert(l2.get_allocator() == lo.get_allocator());
   }
-  {
-    std::vector<bool, other_allocator<bool> > l(other_allocator<bool>(5));
-    std::vector<bool, other_allocator<bool> > lo(other_allocator<bool>(5));
-    for (int i = 1; i <= 3; ++i) {
-      l.push_back(i);
-      lo.push_back(i);
-    }
-    std::vector<bool, other_allocator<bool> > l2(other_allocator<bool>(6));
+  { // Test with POCMA = false_type and allocators compare equal
+    std::vector<bool, min_allocator<bool> > l(N, true, min_allocator<bool>{});
+    std::vector<bool, min_allocator<bool> > lo(N, true, min_allocator<bool>{});
+    std::vector<bool, min_allocator<bool> > l2(N + 10, false, min_allocator<bool>{});
     l2 = std::move(l);
     assert(l2 == lo);
-    assert(l.empty());
+    LIBCPP_ASSERT(l.empty());
     assert(l2.get_allocator() == lo.get_allocator());
   }
-  {
-    std::vector<bool, min_allocator<bool> > l(min_allocator<bool>{});
-    std::vector<bool, min_allocator<bool> > lo(min_allocator<bool>{});
-    for (int i = 1; i <= 3; ++i) {
-      l.push_back(i);
-      lo.push_back(i);
-    }
-    std::vector<bool, min_allocator<bool> > l2(min_allocator<bool>{});
+
+  //
+  // Testing for element-wise move where POCMA = false_type and allocators compare unequal
+  //
+  { // Test with reallocation during the element-wise move due to empty destination vector.
+    std::vector<bool, test_allocator<bool> > l(N, true, test_allocator<bool>(5));
+    std::vector<bool, test_allocator<bool> > lo(N, true, test_allocator<bool>(5));
+    std::vector<bool, test_allocator<bool> > l2(test_allocator<bool>(42));
     l2 = std::move(l);
     assert(l2 == lo);
-    assert(l.empty());
-    assert(l2.get_allocator() == lo.get_allocator());
+    LIBCPP_ASSERT(!l.empty());
+    assert(l2.get_allocator() == test_allocator<bool>(42));
+  }
+  { // Test with reallocation occurs during the element-wise move due to insufficient destination space.
+    std::vector<bool, test_allocator<bool> > l(N + 64, true, test_allocator<bool>(5));
+    std::vector<bool, test_allocator<bool> > lo(N + 64, true, test_allocator<bool>(5));
+    std::vector<bool, test_allocator<bool> > l2(10, false, test_allocator<bool>(42));
+    l2 = std::move(l);
+    assert(l2 == lo);
+    LIBCPP_ASSERT(!l.empty());
+    assert(l2.get_allocator() == test_allocator<bool>(42));
   }
+  { // Test without reallocation where source vector elements fit within destination size.
+    std::vector<bool, test_allocator<bool> > l(N, true, test_allocator<bool>(5));
+    std::vector<bool, test_allocator<bool> > lo(N, true, test_allocator<bool>(5));
+    std::vector<bool, test_allocator<bool> > l2(N * 2, false, test_allocator<bool>(42));
+    l2 = std::move(l);
+    assert(l2 == lo);
+    LIBCPP_ASSERT(!l.empty());
+    assert(l2.get_allocator() == test_allocator<bool>(42));
+  }
+}
+
+TEST_CONSTEXPR_CXX20 bool tests() {
+  test_move_assignment(3);
+  test_move_assignment(18);
+  test_move_assignment(33);
+  test_move_assignment(65);
+  test_move_assignment(299);
 
   return true;
 }



More information about the libcxx-commits mailing list