Skip to content

Commit b98a2ef

Browse files
committed
Speed-up range operations in vector<bool>
1 parent f270c9a commit b98a2ef

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

Diff for: libcxx/include/__algorithm/copy.h

+50
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>
@@ -95,6 +97,54 @@ struct __copy_impl {
9597
}
9698
}
9799

100+
template <class _InIter, class _Cp, __enable_if_t<__has_forward_iterator_category<_InIter>::value, int> = 0>
101+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_InIter, __bit_iterator<_Cp, false>>
102+
operator()(_InIter __first, _InIter __last, __bit_iterator<_Cp, false> __result) {
103+
using _It = __bit_iterator<_Cp, false>;
104+
using __storage_type = typename _It::__storage_type;
105+
__storage_type __n = static_cast<__storage_type>(std::distance(__first, __last));
106+
const unsigned __bits_per_word = _It::__bits_per_word;
107+
108+
if (__n) {
109+
// do first partial word, if present
110+
if (__result.__ctz_ != 0) {
111+
__storage_type __clz = static_cast<__storage_type>(__bits_per_word - __result.__ctz_);
112+
__storage_type __dn = std::min(__clz, __n);
113+
__storage_type __w = *__result.__seg_;
114+
__storage_type __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz - __dn));
115+
__w &= ~__m;
116+
for (__storage_type __i = 0; __i < __dn; ++__i, ++__first)
117+
__w |= static_cast<__storage_type>(*__first) << __result.__ctz_++;
118+
*__result.__seg_ = __w;
119+
if (__result.__ctz_ == __bits_per_word) {
120+
__result.__ctz_ = 0;
121+
++__result.__seg_;
122+
}
123+
__n -= __dn;
124+
}
125+
}
126+
// do middle whole words, if present
127+
__storage_type __nw = __n / __bits_per_word;
128+
__n -= __nw * __bits_per_word;
129+
for (; __nw; --__nw) {
130+
__storage_type __w = 0;
131+
for (__storage_type __i = 0; __i < __bits_per_word; ++__i, ++__first)
132+
__w |= static_cast<__storage_type>(*__first) << __i;
133+
*__result.__seg_++ = __w;
134+
}
135+
// do last partial word, if present
136+
if (__n) {
137+
__storage_type __w = 0;
138+
for (__storage_type __i = 0; __i < __n; ++__i, ++__first)
139+
__w |= static_cast<__storage_type>(*__first) << __i;
140+
__storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
141+
*__result.__seg_ &= ~__m;
142+
*__result.__seg_ |= __w;
143+
__result.__ctz_ = __n;
144+
}
145+
return std::make_pair(std::move(__first), std::move(__result));
146+
}
147+
98148
// At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer.
99149
template <class _In, class _Out, __enable_if_t<__can_lower_copy_assignment_to_memmove<_In, _Out>::value, int> = 0>
100150
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*>

Diff for: libcxx/include/__bit_reference

+3
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/fill_n.h>
1516
#include <__algorithm/min.h>
@@ -970,6 +971,8 @@ private:
970971
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend void
971972
__fill_n_bool(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n);
972973

974+
friend struct __copy_impl;
975+
973976
template <class _Dp, bool _IC>
974977
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, false> __copy_aligned(
975978
__bit_iterator<_Dp, _IC> __first, __bit_iterator<_Dp, _IC> __last, __bit_iterator<_Dp, false> __result);

Diff for: 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_IterIter(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_IterIter(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_IterIter, 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_IterIter, 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)