1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
//
// immer: immutable data structures for C++
// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
//
// This software is distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
//
#include <immer/detail/ref_count_base.hpp>
#include <nonius.h++>
#include <boost/intrusive_ptr.hpp>
#include <atomic>
#include <cstdlib>
#include <iterator>
#include <memory>
#include <utility>
#include <array>
#include <vector>
NONIUS_PARAM(N, std::size_t{1000})
constexpr auto benchmark_size = 32u;
struct object_t : immer::detail::ref_count_base<object_t>
{};
auto make_data()
{
auto objs = std::array<std::unique_ptr<object_t>, benchmark_size>();
std::generate(objs.begin(), objs.end(), [] {
return std::make_unique<object_t>();
});
auto refs = std::array<object_t*, benchmark_size>();
std::transform(objs.begin(), objs.end(), refs.begin(), [](auto& obj) {
return obj.get();
});
return make_pair(std::move(objs),
std::move(refs));
}
NONIUS_BENCHMARK("intrusive_ptr", [] (nonius::chronometer meter)
{
auto arr = std::array<boost::intrusive_ptr<object_t>, benchmark_size>{};
auto storage = std::vector<
nonius::storage_for<
std::array<boost::intrusive_ptr<object_t>, benchmark_size>>> (
meter.runs());
std::generate(arr.begin(), arr.end(), [] {
return new object_t{};
});
meter.measure([&] (int i) {
storage[i].construct(arr);
});
})
NONIUS_BENCHMARK("generic", [] (nonius::chronometer meter)
{
auto data = make_data();
auto& refs = data.second;
object_t* r[benchmark_size];
meter.measure([&] {
std::transform(refs.begin(), refs.end(), r, [] (auto& p) {
if (p) p->ref_count.fetch_add(1, std::memory_order_relaxed);
return p;
});
return r;
});
})
NONIUS_BENCHMARK("manual", [] (nonius::chronometer meter)
{
auto data = make_data();
auto& refs = data.second;
object_t* r[benchmark_size];
meter.measure([&] {
for (auto& p : refs)
if (p) p->ref_count.fetch_add(1, std::memory_order_relaxed);
std::copy(refs.begin(), refs.end(), r);
return r;
});
})
NONIUS_BENCHMARK("manual - unroll", [] (nonius::chronometer meter)
{
auto data = make_data();
auto& refs = data.second;
object_t* r[benchmark_size];
meter.measure([&] {
auto e = refs.end();
for (auto p = refs.begin(); p != e;) {
(*p++)->ref_count.fetch_add(1, std::memory_order_relaxed);
(*p++)->ref_count.fetch_add(1, std::memory_order_relaxed);
(*p++)->ref_count.fetch_add(1, std::memory_order_relaxed);
(*p++)->ref_count.fetch_add(1, std::memory_order_relaxed);
}
std::copy(refs.begin(), refs.end(), r);
return r;
});
})
NONIUS_BENCHMARK("manual - nocheck", [] (nonius::chronometer meter)
{
auto data = make_data();
auto& refs = data.second;
object_t* r[benchmark_size];
meter.measure([&] {
for (auto& p : refs)
p->ref_count.fetch_add(1, std::memory_order_relaxed);
std::copy(refs.begin(), refs.end(), r);
return r;
});
})
NONIUS_BENCHMARK("manual - constant", [] (nonius::chronometer meter)
{
auto data = make_data();
auto& refs = data.second;
object_t* r[benchmark_size];
meter.measure([&] {
for (auto i = 0u; i < benchmark_size; ++i)
refs[i]->ref_count.fetch_add(1, std::memory_order_relaxed);
std::copy(refs.begin(), refs.end(), r);
return r;
});
})
NONIUS_BENCHMARK("manual - memcopy", [] (nonius::chronometer meter)
{
auto data = make_data();
auto& refs = data.second;
object_t* r[benchmark_size];
meter.measure([&] {
for (auto& p : refs)
if (p) p->ref_count.fetch_add(1, std::memory_order_relaxed);
std::memcpy(r, &refs[0], sizeof(object_t*) * benchmark_size);
return r;
});
})
|