[libcxx-commits] [libcxx] [libc++] Speed-up vector<bool> range-based operations [3/3] (PR #120134)
Peng Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Jan 24 20:40:35 PST 2025
https://github.com/winner245 updated https://github.com/llvm/llvm-project/pull/120134
>From a4f49104f532816d394b1fe60f011948c9655144 Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Mon, 16 Dec 2024 13:30:56 -0500
Subject: [PATCH] Speed-up range operations in vector<bool>
---
libcxx/include/__algorithm/copy.h | 80 ++++++++++++
libcxx/include/__bit_reference | 14 +++
libcxx/include/__vector/vector_bool.h | 1 -
.../containers/ContainerBenchmarks.h | 118 +++++++++++++++---
.../benchmarks/containers/deque.bench.cpp | 12 +-
.../vector_bool_operations.bench.cpp | 109 ++++++++++++++++
.../containers/vector_operations.bench.cpp | 25 ++--
.../alg.copy/copy.pass.cpp | 33 +++++
.../test/std/containers/from_range_helpers.h | 63 +++++++---
9 files changed, 403 insertions(+), 52 deletions(-)
create mode 100644 libcxx/test/benchmarks/containers/vector_bool_operations.bench.cpp
diff --git a/libcxx/include/__algorithm/copy.h b/libcxx/include/__algorithm/copy.h
index 962aa90059d574..fa15870ba72848 100644
--- a/libcxx/include/__algorithm/copy.h
+++ b/libcxx/include/__algorithm/copy.h
@@ -13,10 +13,13 @@
#include <__algorithm/for_each_segment.h>
#include <__algorithm/min.h>
#include <__config>
+#include <__fwd/bit_reference.h>
+#include <__iterator/distance.h>
#include <__iterator/iterator_traits.h>
#include <__iterator/segmented_iterator.h>
#include <__type_traits/common_type.h>
#include <__type_traits/enable_if.h>
+#include <__type_traits/is_convertible.h>
#include <__utility/move.h>
#include <__utility/pair.h>
@@ -32,6 +35,25 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template <class _InIter, class _Sent, class _OutIter>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter> __copy(_InIter, _Sent, _OutIter);
+template <class _InIter,
+ class _Sent,
+ __enable_if_t<__has_input_iterator_category<_InIter>::value &&
+ !__has_random_access_iterator_category<_InIter>::value,
+ int> = 0>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 typename iterator_traits<_InIter>::difference_type
+__iter_sent_distance(_InIter __first, _Sent __last) {
+ typename iterator_traits<_InIter>::difference_type __r(0);
+ for (; __first != __last; ++__first)
+ ++__r;
+ return __r;
+}
+
+template <class _InIter, class _Sent, __enable_if_t<__has_random_access_iterator_category<_InIter>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 typename iterator_traits<_InIter>::difference_type
+__iter_sent_distance(_InIter __first, _Sent __last) {
+ return static_cast<typename iterator_traits<_InIter>::difference_type>(__last - __first);
+}
+
struct __copy_impl {
template <class _InIter, class _Sent, class _OutIter>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
@@ -95,6 +117,64 @@ struct __copy_impl {
}
}
+ template <class _InIter,
+ class _Sent,
+ class _Cp,
+ __enable_if_t<(__has_forward_iterator_category<_InIter>::value ||
+ __has_iterator_concept_convertible_to<_InIter, forward_iterator_tag>::value) &&
+ is_convertible<typename iterator_traits<_InIter>::value_type, bool>::value,
+ int> = 0>
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, __bit_iterator<_Cp, false> >
+ operator()(_InIter __first, _Sent __last, __bit_iterator<_Cp, false> __result) const {
+ using _It = __bit_iterator<_Cp, false>;
+ using __storage_type = typename _It::__storage_type;
+#if _LIBCPP_STD_VER >= 20
+ __storage_type __n = static_cast<__storage_type>(std::ranges::distance(__first, __last));
+#else
+ __storage_type __n = static_cast<__storage_type>(std::__iter_sent_distance(__first, __last));
+#endif
+ const unsigned __bits_per_word = _It::__bits_per_word;
+
+ if (__first != __last) {
+ // do first partial word, if present
+ if (__result.__ctz_ != 0) {
+ __storage_type __clz = static_cast<__storage_type>(__bits_per_word - __result.__ctz_);
+ __storage_type __dn = std::min(__clz, __n);
+ __storage_type __w = *__result.__seg_;
+ __storage_type __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz - __dn));
+ __w &= ~__m;
+ for (__storage_type __i = 0; __i < __dn; ++__i, ++__first)
+ __w |= static_cast<__storage_type>(*__first) << __result.__ctz_++;
+ *__result.__seg_ = __w;
+ if (__result.__ctz_ == __bits_per_word) {
+ __result.__ctz_ = 0;
+ ++__result.__seg_;
+ }
+ __n -= __dn;
+ }
+ }
+ // do middle whole words, if present
+ __storage_type __nw = __n / __bits_per_word;
+ __n -= __nw * __bits_per_word;
+ for (; __nw; --__nw) {
+ __storage_type __w = 0;
+ for (__storage_type __i = 0; __i < __bits_per_word; ++__i, ++__first)
+ __w |= static_cast<__storage_type>(*__first) << __i;
+ *__result.__seg_++ = __w;
+ }
+ // do last partial word, if present
+ if (__n) {
+ __storage_type __w = 0;
+ for (__storage_type __i = 0; __i < __n; ++__i, ++__first)
+ __w |= static_cast<__storage_type>(*__first) << __i;
+ __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
+ *__result.__seg_ &= ~__m;
+ *__result.__seg_ |= __w;
+ __result.__ctz_ = __n;
+ }
+ return std::make_pair(std::move(__first), std::move(__result));
+ }
+
// At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer.
template <class _In, class _Out, __enable_if_t<__can_lower_copy_assignment_to_memmove<_In, _Out>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*>
diff --git a/libcxx/include/__bit_reference b/libcxx/include/__bit_reference
index 67abb023122edf..7d548169aff211 100644
--- a/libcxx/include/__bit_reference
+++ b/libcxx/include/__bit_reference
@@ -10,6 +10,7 @@
#ifndef _LIBCPP___BIT_REFERENCE
#define _LIBCPP___BIT_REFERENCE
+#include <__algorithm/copy.h>
#include <__algorithm/copy_n.h>
#include <__algorithm/min.h>
#include <__bit/countr.h>
@@ -22,8 +23,11 @@
#include <__memory/construct_at.h>
#include <__memory/pointer_traits.h>
#include <__type_traits/conditional.h>
+#include <__type_traits/enable_if.h>
#include <__type_traits/is_constant_evaluated.h>
+#include <__type_traits/is_convertible.h>
#include <__type_traits/void_t.h>
+#include <__utility/pair.h>
#include <__utility/swap.h>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -978,6 +982,16 @@ private:
template <class _Dp>
friend struct __bit_array;
+ template <class _InIter,
+ class _Sent,
+ class _Dp,
+ __enable_if_t<(__has_forward_iterator_category<_InIter>::value ||
+ __has_iterator_concept_convertible_to<_InIter, forward_iterator_tag>::value) &&
+ is_convertible<typename iterator_traits<_InIter>::value_type, bool>::value,
+ int> >
+ _LIBCPP_CONSTEXPR_SINCE_CXX14 friend pair<_InIter, __bit_iterator<_Dp, false> >
+ __copy_impl::operator()(_InIter __first, _Sent __last, __bit_iterator<_Dp, false> __result) const;
+
template <bool _FillVal, class _Dp>
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend void
__fill_n_bool(__bit_iterator<_Dp, false> __first, typename __size_difference_type_traits<_Dp>::size_type __n);
diff --git a/libcxx/include/__vector/vector_bool.h b/libcxx/include/__vector/vector_bool.h
index 4f1c442ce0be8d..8d11b6f15af679 100644
--- a/libcxx/include/__vector/vector_bool.h
+++ b/libcxx/include/__vector/vector_bool.h
@@ -171,7 +171,6 @@ class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator> {
if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
auto __n = static_cast<size_type>(ranges::distance(__range));
__init_with_size(ranges::begin(__range), ranges::end(__range), __n);
-
} else {
__init_with_sentinel(ranges::begin(__range), ranges::end(__range));
}
diff --git a/libcxx/test/benchmarks/containers/ContainerBenchmarks.h b/libcxx/test/benchmarks/containers/ContainerBenchmarks.h
index 5fc8981619672c..8147db2af1ff87 100644
--- a/libcxx/test/benchmarks/containers/ContainerBenchmarks.h
+++ b/libcxx/test/benchmarks/containers/ContainerBenchmarks.h
@@ -10,11 +10,13 @@
#ifndef BENCHMARK_CONTAINER_BENCHMARKS_H
#define BENCHMARK_CONTAINER_BENCHMARKS_H
+#include <__type_traits/type_identity.h>
#include <cassert>
#include <iterator>
#include <utility>
#include "benchmark/benchmark.h"
+#include "../../std/containers/from_range_helpers.h"
#include "../Utilities.h"
#include "test_iterators.h"
@@ -51,16 +53,57 @@ void BM_Assignment(benchmark::State& st, Container) {
}
}
+template <class Container, class Generator, class InputIter = decltype(std::declval<Generator>()(0).begin())>
+void BM_AssignIterIter(benchmark::State& st, Generator gen, InputIter = {}) {
+ using T = typename Container::value_type;
+ auto size = st.range(0);
+ auto in1 = gen(size);
+ auto in2 = gen(size);
+ DoNotOptimizeData(in1);
+ DoNotOptimizeData(in2);
+ Container c(in1.begin(), in1.end());
+ DoNotOptimizeData(c);
+ bool toggle = false;
+ for (auto _ : st) {
+ std::vector<T>& in = toggle ? in1 : in2;
+ auto first = in.begin();
+ auto last = in.end();
+ c.assign(InputIter(first), InputIter(last));
+ toggle = !toggle;
+ DoNotOptimizeData(c);
+ }
+}
+
+template <typename Container, class Generator, class Range = std::__type_identity_t<Container>>
+void BM_AssignRange(benchmark::State& st, Generator gen, Range = {}) {
+ auto size = st.range(0);
+ auto in1 = gen(size);
+ auto in2 = gen(size);
+ DoNotOptimizeData(in1);
+ DoNotOptimizeData(in2);
+ Range rg1(std::ranges::begin(in1), std::ranges::end(in1));
+ Range rg2(std::ranges::begin(in2), std::ranges::end(in2));
+ Container c(std::from_range, rg1);
+ DoNotOptimizeData(c);
+ bool toggle = false;
+ for (auto _ : st) {
+ auto& rg = toggle ? rg1 : rg2;
+ c.assign_range(rg);
+ toggle = !toggle;
+ DoNotOptimizeData(c);
+ }
+}
+
template <std::size_t... sz, typename Container, typename GenInputs>
void BM_AssignInputIterIter(benchmark::State& st, Container c, GenInputs gen) {
auto v = gen(1, sz...);
c.resize(st.range(0), v[0]);
auto in = gen(st.range(1), sz...);
- benchmark::DoNotOptimize(&in);
- benchmark::DoNotOptimize(&c);
+ DoNotOptimizeData(in);
+ DoNotOptimizeData(c);
for (auto _ : st) {
c.assign(cpp17_input_iterator(in.begin()), cpp17_input_iterator(in.end()));
- benchmark::ClobberMemory();
+ DoNotOptimizeData(c);
}
}
@@ -73,24 +116,25 @@ void BM_ConstructSizeValue(benchmark::State& st, Container, typename Container::
}
}
-template <class Container, class GenInputs>
-void BM_ConstructIterIter(benchmark::State& st, Container, GenInputs gen) {
- auto in = gen(st.range(0));
- const auto begin = in.begin();
- const auto end = in.end();
- benchmark::DoNotOptimize(&in);
+template <class Container, class GenInputs, class InputIter = decltype(std::declval<GenInputs>()(0).begin())>
+void BM_ConstructIterIter(benchmark::State& st, GenInputs gen, InputIter = {}) {
+ auto in = gen(st.range(0));
+ DoNotOptimizeData(in);
+ const auto begin = InputIter(in.begin());
+ const auto end = InputIter(in.end());
while (st.KeepRunning()) {
- Container c(begin, end);
+ Container c(begin, end); // we assume the destructor doesn't dominate the benchmark
DoNotOptimizeData(c);
}
}
-template <class Container, class GenInputs>
-void BM_ConstructFromRange(benchmark::State& st, Container, GenInputs gen) {
+template <class Container, class GenInputs, class Range = std::__type_identity_t<Container>>
+void BM_ConstructFromRange(benchmark::State& st, GenInputs gen, Range = {}) {
auto in = gen(st.range(0));
- benchmark::DoNotOptimize(&in);
+ DoNotOptimizeData(in);
+ Range rg(std::ranges::begin(in), std::ranges::end(in));
while (st.KeepRunning()) {
- Container c(std::from_range, in);
+ Container c(std::from_range, rg); // we assume the destructor doesn't dominate the benchmark
DoNotOptimizeData(c);
}
}
@@ -108,6 +152,52 @@ void BM_Pushback_no_grow(benchmark::State& state, Container c) {
}
}
+template <class Container, class GenInputs, class InputIter = decltype(std::declval<GenInputs>()(0).begin())>
+void BM_InsertIterIterIter(benchmark::State& st, GenInputs gen, InputIter = {}) {
+ auto in = gen(st.range(0));
+ DoNotOptimizeData(in);
+ const auto beg = InputIter(in.begin());
+ const auto end = InputIter(in.end());
+ const unsigned size = 100;
+ Container c(size);
+ DoNotOptimizeData(c);
+ for (auto _ : st) {
+ c.insert(c.begin(), beg, end);
+ DoNotOptimizeData(c);
+ c.erase(c.begin() + size, c.end()); // avoid growing indefinitely
+ }
+}
+
+template <class Container, class GenInputs, class Range = std::__type_identity_t<Container>>
+void BM_InsertRange(benchmark::State& st, GenInputs gen, Range = {}) {
+ auto in = gen(st.range(0));
+ DoNotOptimizeData(in);
+ Range rg(std::ranges::begin(in), std::ranges::end(in));
+ const unsigned size = 100;
+ Container c(size);
+ DoNotOptimizeData(c);
+ for (auto _ : st) {
+ c.insert_range(c.begin(), rg);
+ DoNotOptimizeData(c);
+ c.erase(c.begin() + size, c.end()); // avoid growing indefinitely
+ }
+}
+
+template <class Container, class GenInputs, class Range = std::__type_identity_t<Container>>
+void BM_AppendRange(benchmark::State& st, GenInputs gen, Range = {}) {
+ auto in = gen(st.range(0));
+ DoNotOptimizeData(in);
+ Range rg(std::ranges::begin(in), std::ranges::end(in));
+ const unsigned size = 100;
+ Container c(size);
+ DoNotOptimizeData(c);
+ for (auto _ : st) {
+ c.append_range(rg);
+ DoNotOptimizeData(c);
+ c.erase(c.begin() + size, c.end()); // avoid growing indefinitely
+ }
+}
+
template <class Container, class GenInputs>
void BM_InsertValue(benchmark::State& st, Container c, GenInputs gen) {
auto in = gen(st.range(0));
diff --git a/libcxx/test/benchmarks/containers/deque.bench.cpp b/libcxx/test/benchmarks/containers/deque.bench.cpp
index 7ff1093a9391ca..2a576831b88e8e 100644
--- a/libcxx/test/benchmarks/containers/deque.bench.cpp
+++ b/libcxx/test/benchmarks/containers/deque.bench.cpp
@@ -24,22 +24,22 @@ BENCHMARK_CAPTURE(BM_ConstructSize, deque_byte, std::deque<unsigned char>{})->Ar
BENCHMARK_CAPTURE(BM_ConstructSizeValue, deque_byte, std::deque<unsigned char>{}, 0)->Arg(5140480);
-BENCHMARK_CAPTURE(BM_ConstructIterIter, deque_char, std::deque<char>{}, getRandomIntegerInputs<char>)
+BENCHMARK_CAPTURE(BM_ConstructIterIter<std::deque<char>>, deque_char, getRandomIntegerInputs<char>)
->Arg(TestNumInputs);
-BENCHMARK_CAPTURE(BM_ConstructIterIter, deque_size_t, std::deque<size_t>{}, getRandomIntegerInputs<size_t>)
+BENCHMARK_CAPTURE(BM_ConstructIterIter<std::deque<size_t>>, deque_size_t, getRandomIntegerInputs<size_t>)
->Arg(TestNumInputs);
-BENCHMARK_CAPTURE(BM_ConstructIterIter, deque_string, std::deque<std::string>{}, getRandomStringInputs)
+BENCHMARK_CAPTURE(BM_ConstructIterIter<std::deque<std::string>>, deque_string, getRandomStringInputs)
->Arg(TestNumInputs);
-BENCHMARK_CAPTURE(BM_ConstructFromRange, deque_char, std::deque<char>{}, getRandomIntegerInputs<char>)
+BENCHMARK_CAPTURE(BM_ConstructFromRange<std::deque<char>>, deque_char, getRandomIntegerInputs<char>)
->Arg(TestNumInputs);
-BENCHMARK_CAPTURE(BM_ConstructFromRange, deque_size_t, std::deque<size_t>{}, getRandomIntegerInputs<size_t>)
+BENCHMARK_CAPTURE(BM_ConstructFromRange<std::deque<size_t>>, deque_size_t, getRandomIntegerInputs<size_t>)
->Arg(TestNumInputs);
-BENCHMARK_CAPTURE(BM_ConstructFromRange, deque_string, std::deque<std::string>{}, getRandomStringInputs)
+BENCHMARK_CAPTURE(BM_ConstructFromRange<std::deque<std::string>>, deque_string, getRandomStringInputs)
->Arg(TestNumInputs);
BENCHMARK_CAPTURE(BM_erase_iter_in_middle, deque_int, std::deque<int>{}, getRandomIntegerInputs<int>)
diff --git a/libcxx/test/benchmarks/containers/vector_bool_operations.bench.cpp b/libcxx/test/benchmarks/containers/vector_bool_operations.bench.cpp
new file mode 100644
index 00000000000000..913aef77f2b1e0
--- /dev/null
+++ b/libcxx/test/benchmarks/containers/vector_bool_operations.bench.cpp
@@ -0,0 +1,109 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+#include <cstdint>
+#include <cstdlib>
+#include <cstring>
+#include <deque>
+#include <functional>
+#include <memory>
+#include <string>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+#include "ContainerBenchmarks.h"
+#include "../../std/containers/from_range_helpers.h"
+#include "../GenerateInput.h"
+#include "test_iterators.h"
+
+using namespace ContainerBenchmarks;
+
+using vb_iter = std::vector<bool>::iterator;
+
+// Benchmarks for forward_iterator or forward_range
+
+BENCHMARK_CAPTURE(BM_ConstructIterIter<std::vector<bool>>,
+ forward_iterator,
+ getRandomIntegerInputs<bool>,
+ forward_iterator<vb_iter>())
+ ->Arg(5140480);
+
+BENCHMARK_CAPTURE(BM_ConstructFromRange<std::vector<bool>>,
+ forward_range,
+ getRandomIntegerInputs<bool>,
+ forward_range_wrapper<vb_iter>())
+ ->Arg(5140480);
+
+BENCHMARK_CAPTURE(BM_ConstructIterIter<std::vector<bool>>, forward_iterator, getRandomIntegerInputs<bool>)
+ ->Arg(5140480);
+
+BENCHMARK_CAPTURE(
+ BM_AssignRange<std::vector<bool>>, forward_range, getRandomIntegerInputs<bool>, forward_range_wrapper<vb_iter>())
+ ->Arg(5140480);
+
+BENCHMARK_CAPTURE(BM_InsertIterIterIter<std::vector<bool>>,
+ forward_iterator,
+ getRandomIntegerInputs<bool>,
+ forward_iterator<vb_iter>())
+ ->Arg(5140480);
+
+BENCHMARK_CAPTURE(
+ BM_InsertRange<std::vector<bool>>, forward_range, getRandomIntegerInputs<bool>, forward_range_wrapper<vb_iter>())
+ ->Arg(5140480);
+
+BENCHMARK_CAPTURE(
+ BM_AppendRange<std::vector<bool>>, forward_range, getRandomIntegerInputs<bool>, forward_range_wrapper<vb_iter>())
+ ->Arg(5140480);
+
+// Benchmarks for random_access_iterator or random_access_range
+
+BENCHMARK_CAPTURE(BM_ConstructIterIter<std::vector<bool>>,
+ random_access_iterator,
+ getRandomIntegerInputs<bool>,
+ random_access_iterator<vb_iter>())
+ ->Arg(5140480);
+
+BENCHMARK_CAPTURE(BM_ConstructFromRange<std::vector<bool>>,
+ random_access_range,
+ getRandomIntegerInputs<bool>,
+ random_access_range_wrapper<vb_iter>())
+ ->Arg(5140480);
+
+BENCHMARK_CAPTURE(BM_AssignIterIter<std::vector<bool>>,
+ random_access_iterator,
+ getRandomIntegerInputs<bool>,
+ random_access_iterator<vb_iter>())
+ ->Arg(5140480);
+
+BENCHMARK_CAPTURE(BM_AssignRange<std::vector<bool>>,
+ random_access_range,
+ getRandomIntegerInputs<bool>,
+ random_access_range_wrapper<vb_iter>())
+ ->Arg(5140480);
+
+BENCHMARK_CAPTURE(BM_InsertIterIterIter<std::vector<bool>>,
+ random_access_iterator,
+ getRandomIntegerInputs<bool>,
+ random_access_iterator<vb_iter>())
+ ->Arg(5140480);
+
+BENCHMARK_CAPTURE(BM_InsertRange<std::vector<bool>>,
+ random_access_range,
+ getRandomIntegerInputs<bool>,
+ random_access_range_wrapper<vb_iter>())
+ ->Arg(5140480);
+
+BENCHMARK_CAPTURE(BM_AppendRange<std::vector<bool>>,
+ random_access_range,
+ getRandomIntegerInputs<bool>,
+ random_access_range_wrapper<vb_iter>())
+ ->Arg(5140480);
+
+BENCHMARK_MAIN();
diff --git a/libcxx/test/benchmarks/containers/vector_operations.bench.cpp b/libcxx/test/benchmarks/containers/vector_operations.bench.cpp
index 1cd754ca7e7803..b6b7c4366e2728 100644
--- a/libcxx/test/benchmarks/containers/vector_operations.bench.cpp
+++ b/libcxx/test/benchmarks/containers/vector_operations.bench.cpp
@@ -33,22 +33,22 @@ BENCHMARK_CAPTURE(BM_Assignment, vector_int, std::vector<int>{})->Arg(5140480);
BENCHMARK_CAPTURE(BM_ConstructSizeValue, vector_byte, std::vector<unsigned char>{}, 0)->Arg(5140480);
-BENCHMARK_CAPTURE(BM_ConstructIterIter, vector_char, std::vector<char>{}, getRandomIntegerInputs<char>)
+BENCHMARK_CAPTURE(BM_ConstructIterIter<std::vector<char>>, vector_char, getRandomIntegerInputs<char>)
->Arg(TestNumInputs);
-BENCHMARK_CAPTURE(BM_ConstructIterIter, vector_size_t, std::vector<size_t>{}, getRandomIntegerInputs<size_t>)
+BENCHMARK_CAPTURE(BM_ConstructIterIter<std::vector<size_t>>, vector_size_t, getRandomIntegerInputs<size_t>)
->Arg(TestNumInputs);
-BENCHMARK_CAPTURE(BM_ConstructIterIter, vector_string, std::vector<std::string>{}, getRandomStringInputs)
+BENCHMARK_CAPTURE(BM_ConstructIterIter<std::vector<std::string>>, vector_string, getRandomStringInputs)
->Arg(TestNumInputs);
-BENCHMARK_CAPTURE(BM_ConstructFromRange, vector_char, std::vector<char>{}, getRandomIntegerInputs<char>)
+BENCHMARK_CAPTURE(BM_ConstructFromRange<std::vector<char>>, vector_char, getRandomIntegerInputs<char>)
->Arg(TestNumInputs);
-BENCHMARK_CAPTURE(BM_ConstructFromRange, vector_size_t, std::vector<size_t>{}, getRandomIntegerInputs<size_t>)
+BENCHMARK_CAPTURE(BM_ConstructFromRange<std::vector<size_t>>, vector_size_t, getRandomIntegerInputs<size_t>)
->Arg(TestNumInputs);
-BENCHMARK_CAPTURE(BM_ConstructFromRange, vector_string, std::vector<std::string>{}, getRandomStringInputs)
+BENCHMARK_CAPTURE(BM_ConstructFromRange<std::vector<std::string>>, vector_string, getRandomStringInputs)
->Arg(TestNumInputs);
BENCHMARK_CAPTURE(BM_Pushback_no_grow, vector_int, std::vector<int>{})->Arg(TestNumInputs);
@@ -78,18 +78,9 @@ BENCHMARK(bm_grow<std::string>);
BENCHMARK(bm_grow<std::unique_ptr<int>>);
BENCHMARK(bm_grow<std::deque<int>>);
-BENCHMARK_CAPTURE(BM_AssignInputIterIter, vector_int, std::vector<int>{}, getRandomIntegerInputs<int>)
- ->Args({TestNumInputs, TestNumInputs});
+BENCHMARK_CAPTURE(BM_AssignIterIter<std::vector<int>>, vector_int, getRandomIntegerInputs<int>)->Arg(10240);
-BENCHMARK_CAPTURE(
- BM_AssignInputIterIter<32>, vector_string, std::vector<std::string>{}, getRandomStringInputsWithLength)
- ->Args({TestNumInputs, TestNumInputs});
-
-BENCHMARK_CAPTURE(BM_AssignInputIterIter<100>,
- vector_vector_int,
- std::vector<std::vector<int>>{},
- getRandomIntegerInputsWithLength<int>)
- ->Args({TestNumInputs, TestNumInputs});
+BENCHMARK_CAPTURE(BM_AssignIterIter<std::vector<std::string>>, vector_string, getRandomStringInputs)->Arg(10240);
BENCHMARK_CAPTURE(BM_Insert_InputIterIter_NoRealloc, vector_int, std::vector<int>(100, 1), getRandomIntegerInputs<int>)
->Arg(514048);
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/copy.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/copy.pass.cpp
index b5f0a32b986a03..179fa25c15c0cd 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/copy.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/copy.pass.cpp
@@ -13,7 +13,9 @@
// copy(InIter first, InIter last, OutIter result);
#include <algorithm>
+#include <array>
#include <cassert>
+#include <vector>
#include "test_macros.h"
#include "test_iterators.h"
@@ -59,6 +61,29 @@ struct TestInIters {
}
};
+template <std::size_t N>
+struct TestFwdIterInBitIterOut {
+ std::array<bool, N> in = {};
+ template <class FwdIter>
+ TEST_CONSTEXPR_CXX20 void operator()() {
+ for (std::size_t i = 0; i < in.size(); i += 2)
+ in[i] = true;
+
+ { // Test with full bytes
+ std::vector<bool> out(N);
+ std::copy(FwdIter(in.data()), FwdIter(in.data() + N), out.begin());
+ for (std::size_t i = 0; i < N; ++i)
+ assert(out[i] == static_cast<bool>(in[i]));
+ }
+ { // Test with partial bytes in both front and back
+ std::vector<bool> out(N + 8);
+ std::copy(FwdIter(in.data()), FwdIter(in.data() + N), out.begin() + 4);
+ for (std::size_t i = 0; i < N; ++i)
+ assert(out[i + 4] == static_cast<bool>(in[i]));
+ }
+ }
+};
+
TEST_CONSTEXPR_CXX20 bool test() {
types::for_each(types::cpp17_input_iterator_list<int*>(), TestInIters());
@@ -78,6 +103,14 @@ TEST_CONSTEXPR_CXX20 bool test() {
assert(std::equal(a, a + 10, expected));
}
+ { // Test std::copy() with forward_iterator-pair input and vector<bool>::iterator output
+ types::for_each(types::forward_iterator_list<bool*>(), TestFwdIterInBitIterOut<8>());
+ types::for_each(types::forward_iterator_list<bool*>(), TestFwdIterInBitIterOut<16>());
+ types::for_each(types::forward_iterator_list<bool*>(), TestFwdIterInBitIterOut<32>());
+ types::for_each(types::forward_iterator_list<bool*>(), TestFwdIterInBitIterOut<64>());
+ types::for_each(types::forward_iterator_list<bool*>(), TestFwdIterInBitIterOut<256>());
+ }
+
return true;
}
diff --git a/libcxx/test/std/containers/from_range_helpers.h b/libcxx/test/std/containers/from_range_helpers.h
index e17ea247618bc2..3b703dbe4d9b74 100644
--- a/libcxx/test/std/containers/from_range_helpers.h
+++ b/libcxx/test/std/containers/from_range_helpers.h
@@ -50,8 +50,46 @@ constexpr auto wrap_input(std::vector<T>& input) {
return std::ranges::subrange(std::move(b), std::move(e));
}
+template <class Iter, class Sent = Iter>
+class forward_range_wrapper {
+public:
+ using _Iter = forward_iterator<Iter>;
+ using _Sent = sentinel_wrapper<_Iter>;
+
+ forward_range_wrapper() = default;
+ forward_range_wrapper(Iter begin, Iter end) : begin_(std::move(begin)), end_(std::move(end)) {}
+ _Iter begin() { return _Iter(std::move(begin_)); }
+ _Sent end() { return _Sent(_Iter(std::move(end_))); }
+
+private:
+ Iter begin_;
+ Sent end_;
+};
+
+template <class Iter, class Sent>
+forward_range_wrapper(Iter, Sent) -> forward_range_wrapper<Iter, Sent>;
+
+template <class Iter, class Sent = Iter>
+class random_access_range_wrapper {
+public:
+ using _Iter = cpp20_random_access_iterator<Iter>;
+ using _Sent = sized_sentinel<_Iter>;
+
+ random_access_range_wrapper() = default;
+ random_access_range_wrapper(Iter begin, Iter end) : begin_(std::move(begin)), end_(std::move(end)) {}
+ _Iter begin() { return _Iter(std::move(begin_)); }
+ _Sent end() { return _Sent(_Iter(std::move(end_))); }
+
+private:
+ Iter begin_;
+ Sent end_;
+};
+
+template <class Iter, class Sent>
+random_access_range_wrapper(Iter, Sent) -> random_access_range_wrapper<Iter, Sent>;
+
struct KeyValue {
- int key; // Only the key is considered for equality comparison.
+ int key; // Only the key is considered for equality comparison.
char value; // Allows distinguishing equivalent instances.
bool operator<(const KeyValue& other) const { return key < other.key; }
@@ -60,17 +98,15 @@ struct KeyValue {
template <>
struct std::hash<KeyValue> {
- std::size_t operator()(const KeyValue& kv) const {
- return kv.key;
- }
+ std::size_t operator()(const KeyValue& kv) const { return kv.key; }
};
#if !defined(TEST_HAS_NO_EXCEPTIONS)
template <class T>
struct ThrowingAllocator {
- using value_type = T;
- using char_type = T;
+ using value_type = T;
+ using char_type = T;
using is_always_equal = std::false_type;
ThrowingAllocator() = default;
@@ -90,14 +126,13 @@ struct ThrowingAllocator {
template <class T, class Func>
constexpr void for_all_iterators_and_allocators(Func f) {
- using Iterators = types::type_list<
- cpp20_input_iterator<T*>,
- forward_iterator<T*>,
- bidirectional_iterator<T*>,
- random_access_iterator<T*>,
- contiguous_iterator<T*>,
- T*
- >;
+ using Iterators =
+ types::type_list< cpp20_input_iterator<T*>,
+ forward_iterator<T*>,
+ bidirectional_iterator<T*>,
+ random_access_iterator<T*>,
+ contiguous_iterator<T*>,
+ T* >;
types::for_each(Iterators{}, [=]<class Iter>() {
f.template operator()<Iter, sentinel_wrapper<Iter>, std::allocator<T>>();
More information about the libcxx-commits
mailing list