Skip to content

Commit 81c929a

Browse files
committed
Speed-up range operations in vector<bool>
1 parent 93c2577 commit 81c929a

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

libcxx/include/__algorithm/copy.h

+56
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <__algorithm/for_each_segment.h>
1414
#include <__algorithm/min.h>
1515
#include <__config>
16+
#include <__fwd/bit_reference.h>
17+
#include <__iterator/distance.h>
1618
#include <__iterator/iterator_traits.h>
1719
#include <__iterator/segmented_iterator.h>
1820
#include <__type_traits/common_type.h>
@@ -103,6 +105,60 @@ struct __copy_impl {
103105
}
104106
};
105107

108+
template <class _InIter, class _Sent, class _Cp>
109+
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI pair<_InIter, __bit_iterator<_Cp, false> >
110+
__copy_bool(_InIter __first, _Sent __last, __bit_iterator<_Cp, false> __result) {
111+
using _It = __bit_iterator<_Cp, false>;
112+
using __storage_type = typename _It::__storage_type;
113+
__storage_type __n = static_cast<__storage_type>(std::distance(__first, __last));
114+
const unsigned __bits_per_word = _It::__bits_per_word;
115+
116+
if (__n) {
117+
// do first partial word, if present
118+
if (__result.__ctz_ != 0) {
119+
__storage_type __clz = static_cast<__storage_type>(__bits_per_word - __result.__ctz_);
120+
__storage_type __dn = std::min(__clz, __n);
121+
__storage_type __w = *__result.__seg_;
122+
__storage_type __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz - __dn));
123+
__w &= ~__m;
124+
for (__storage_type __i = 0; __i < __dn; ++__i, ++__first)
125+
__w |= static_cast<__storage_type>(*__first) << __result.__ctz_++;
126+
*__result.__seg_ = __w;
127+
if (__result.__ctz_ == __bits_per_word) {
128+
__result.__ctz_ = 0;
129+
++__result.__seg_;
130+
}
131+
__n -= __dn;
132+
}
133+
}
134+
// do middle whole words, if present
135+
__storage_type __nw = __n / __bits_per_word;
136+
__n -= __nw * __bits_per_word;
137+
for (; __nw; --__nw) {
138+
__storage_type __w = 0;
139+
for (__storage_type __i = 0; __i < __bits_per_word; ++__i, ++__first)
140+
__w |= static_cast<__storage_type>(*__first) << __i;
141+
*__result.__seg_++ = __w;
142+
}
143+
// do last partial word, if present
144+
if (__n) {
145+
__storage_type __w = 0;
146+
for (__storage_type __i = 0; __i < __n; ++__i, ++__first)
147+
__w |= static_cast<__storage_type>(*__first) << __i;
148+
__storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
149+
*__result.__seg_ &= ~__m;
150+
*__result.__seg_ |= __w;
151+
__result.__ctz_ = __n;
152+
}
153+
return std::make_pair(std::move(__first), std::move(__result));
154+
}
155+
156+
template <class _InIter, class _Cp, __enable_if_t<__has_forward_iterator_category<_InIter>::value, int> = 0>
157+
pair<_InIter, __bit_iterator<_Cp, false> > inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
158+
__copy(_InIter __first, _InIter __last, __bit_iterator<_Cp, false> __result) {
159+
return std::__copy_bool(std::move(__first), std::move(__last), std::move(__result));
160+
}
161+
106162
template <class _InIter, class _Sent, class _OutIter>
107163
pair<_InIter, _OutIter> inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
108164
__copy(_InIter __first, _Sent __last, _OutIter __result) {

libcxx/include/__bit_reference

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ifndef _LIBCPP___BIT_REFERENCE
1111
#define _LIBCPP___BIT_REFERENCE
1212

13+
#include <__algorithm/copy.h>
1314
#include <__algorithm/copy_n.h>
1415
#include <__algorithm/min.h>
1516
#include <__bit/countr.h>
@@ -22,6 +23,7 @@
2223
#include <__memory/pointer_traits.h>
2324
#include <__type_traits/conditional.h>
2425
#include <__type_traits/is_constant_evaluated.h>
26+
#include <__utility/pair.h>
2527
#include <__utility/swap.h>
2628

2729
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -964,6 +966,10 @@ private:
964966
template <class _Dp>
965967
friend struct __bit_array;
966968

969+
template <class _InIter, class _Sent, class _Dp>
970+
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI friend pair<_InIter, __bit_iterator<_Dp, false> >
971+
__copy_bool(_InIter __first, _Sent __last, __bit_iterator<_Dp, false> __result);
972+
967973
template <bool _FillVal, class _Dp>
968974
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend void
969975
__fill_n_bool(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n);

libcxx/test/benchmarks/containers/ContainerBenchmarks.h

+58
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,30 @@ void BM_Assignment(benchmark::State& st, Container) {
5151
}
5252
}
5353

54+
template <class Container, class GenInputs>
55+
void BM_assign_iter_iter(benchmark::State& st, Container c, GenInputs gen) {
56+
auto in = gen(st.range(0));
57+
auto beg = in.begin();
58+
auto end = in.end();
59+
for (auto _ : st) {
60+
c.assign(beg, end);
61+
DoNotOptimizeData(c);
62+
DoNotOptimizeData(in);
63+
benchmark::ClobberMemory();
64+
}
65+
}
66+
67+
template <std::size_t... sz, typename Container, typename GenInputs>
68+
void BM_assign_range(benchmark::State& st, Container c, GenInputs gen) {
69+
auto in = gen(st.range(0));
70+
for (auto _ : st) {
71+
c.assign_range(in);
72+
DoNotOptimizeData(c);
73+
DoNotOptimizeData(in);
74+
benchmark::ClobberMemory();
75+
}
76+
}
77+
5478
template <std::size_t... sz, typename Container, typename GenInputs>
5579
void BM_AssignInputIterIter(benchmark::State& st, Container c, GenInputs gen) {
5680
auto v = gen(1, sz...);
@@ -108,6 +132,40 @@ void BM_Pushback_no_grow(benchmark::State& state, Container c) {
108132
}
109133
}
110134

135+
template <class Container, class GenInputs>
136+
void BM_insert_iter_iter_iter(benchmark::State& st, Container c, GenInputs gen) {
137+
auto in = gen(st.range(0));
138+
const auto beg = in.begin();
139+
const auto end = in.end();
140+
for (auto _ : st) {
141+
c.resize(100);
142+
c.insert(c.begin() + 50, beg, end);
143+
DoNotOptimizeData(c);
144+
benchmark::ClobberMemory();
145+
}
146+
}
147+
148+
template <class Container, class GenInputs>
149+
void BM_insert_range(benchmark::State& st, Container c, GenInputs gen) {
150+
auto in = gen(st.range(0));
151+
for (auto _ : st) {
152+
c.resize(100);
153+
c.insert_range(c.begin() + 50, in);
154+
DoNotOptimizeData(c);
155+
benchmark::ClobberMemory();
156+
}
157+
}
158+
159+
template <class Container, class GenInputs>
160+
void BM_append_range(benchmark::State& st, Container c, GenInputs gen) {
161+
auto in = gen(st.range(0));
162+
for (auto _ : st) {
163+
c.append_range(in);
164+
DoNotOptimizeData(c);
165+
benchmark::ClobberMemory();
166+
}
167+
}
168+
111169
template <class Container, class GenInputs>
112170
void BM_InsertValue(benchmark::State& st, Container c, GenInputs gen) {
113171
auto in = gen(st.range(0));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10+
11+
#include <cstdint>
12+
#include <cstdlib>
13+
#include <cstring>
14+
#include <deque>
15+
#include <functional>
16+
#include <memory>
17+
#include <string>
18+
#include <vector>
19+
20+
#include "benchmark/benchmark.h"
21+
#include "ContainerBenchmarks.h"
22+
#include "../GenerateInput.h"
23+
24+
using namespace ContainerBenchmarks;
25+
26+
BENCHMARK_CAPTURE(BM_ConstructIterIter, vector_bool, std::vector<bool>{}, getRandomIntegerInputs<bool>)->Arg(5140480);
27+
BENCHMARK_CAPTURE(BM_ConstructFromRange, vector_bool, std::vector<bool>{}, getRandomIntegerInputs<bool>)->Arg(5140480);
28+
29+
BENCHMARK_CAPTURE(BM_assign_iter_iter, vector_bool, std::vector<bool>{}, getRandomIntegerInputs<bool>)->Arg(5140480);
30+
BENCHMARK_CAPTURE(BM_assign_range, vector_bool, std::vector<bool>{}, getRandomIntegerInputs<bool>)->Arg(5140480);
31+
32+
BENCHMARK_CAPTURE(BM_insert_iter_iter_iter, vector_bool, std::vector<bool>{}, getRandomIntegerInputs<bool>)
33+
->Arg(5140480);
34+
BENCHMARK_CAPTURE(BM_insert_range, vector_bool, std::vector<bool>{}, getRandomIntegerInputs<bool>)->Arg(5140480);
35+
BENCHMARK_CAPTURE(BM_append_range, vector_bool, std::vector<bool>{}, getRandomIntegerInputs<bool>)->Arg(5140480);
36+
37+
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)