Skip to content

Conversation

philnik777
Copy link
Contributor

@philnik777 philnik777 commented Oct 2, 2025

Benchmark                   Baseline    Candidate    Difference    % Difference
------------------------  ----------  -----------  ------------  --------------
BM_vector_bool_copy_ctor       56.93        10.93        -46.00          -80.80
BM_vector_bool_size_ctor        7.99         8.01          0.02            0.22

@philnik777 philnik777 requested a review from a team as a code owner October 2, 2025 14:11
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Oct 2, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 2, 2025

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/161672.diff

2 Files Affected:

  • (modified) libcxx/include/__vector/vector_bool.h (+6-3)
  • (modified) libcxx/test/benchmarks/containers/sequence/vector_bool.bench.cpp (+10)
diff --git a/libcxx/include/__vector/vector_bool.h b/libcxx/include/__vector/vector_bool.h
index 66f5fd9498eec..1f45ee6ab7d64 100644
--- a/libcxx/include/__vector/vector_bool.h
+++ b/libcxx/include/__vector/vector_bool.h
@@ -11,6 +11,7 @@
 
 #include <__algorithm/copy.h>
 #include <__algorithm/copy_backward.h>
+#include <__algorithm/copy_n.h>
 #include <__algorithm/fill_n.h>
 #include <__algorithm/iterator_operations.h>
 #include <__algorithm/max.h>
@@ -701,7 +702,8 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(const vector& __v
       __alloc_(__storage_traits::select_on_container_copy_construction(__v.__alloc_)) {
   if (__v.size() > 0) {
     __vallocate(__v.size());
-    __construct_at_end(__v.begin(), __v.end(), __v.size());
+    std::copy_n(__v.__begin_, __external_cap_to_internal(__v.size()), __begin_);
+    __size_ = __v.size();
   }
 }
 
@@ -710,7 +712,8 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(const vector& __v
     : __begin_(nullptr), __size_(0), __cap_(0), __alloc_(__a) {
   if (__v.size() > 0) {
     __vallocate(__v.size());
-    __construct_at_end(__v.begin(), __v.end(), __v.size());
+    std::copy_n(__v.__begin_, __external_cap_to_internal(__v.size()), __begin_);
+    __size_ = __v.size();
   }
 }
 
@@ -723,7 +726,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>& vector<bool, _Allocator>
         __vdeallocate();
         __vallocate(__v.__size_);
       }
-      std::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_);
+      std::copy_n(__v.__begin_, __external_cap_to_internal(__v.__size_), __begin_);
     }
     __size_ = __v.__size_;
   }
diff --git a/libcxx/test/benchmarks/containers/sequence/vector_bool.bench.cpp b/libcxx/test/benchmarks/containers/sequence/vector_bool.bench.cpp
index 6ecb268208cc8..de1eb41989c00 100644
--- a/libcxx/test/benchmarks/containers/sequence/vector_bool.bench.cpp
+++ b/libcxx/test/benchmarks/containers/sequence/vector_bool.bench.cpp
@@ -9,6 +9,16 @@
 #include <benchmark/benchmark.h>
 #include <vector>
 
+static void BM_vector_bool_copy_ctor(benchmark::State& state) {
+  std::vector<bool> vec(100, true);
+
+  for (auto _ : state) {
+    benchmark::DoNotOptimize(vec);
+    benchmark::DoNotOptimize(auto(vec));
+  }
+}
+BENCHMARK(BM_vector_bool_copy_ctor);
+
 static void BM_vector_bool_size_ctor(benchmark::State& state) {
   for (auto _ : state) {
     std::vector<bool> vec(100, true);

@philnik777 philnik777 force-pushed the optimize_vector_bool_copy branch from d8c6288 to 7279ae3 Compare October 6, 2025 10:58
Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, and IMO it simplifies the code.

@philnik777 philnik777 force-pushed the optimize_vector_bool_copy branch from 7279ae3 to 81596a7 Compare October 7, 2025 08:44
@philnik777 philnik777 merged commit a798a10 into llvm:main Oct 7, 2025
67 of 74 checks passed
@philnik777 philnik777 deleted the optimize_vector_bool_copy branch October 7, 2025 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants