/
collect_with_extend.rs
192 lines (166 loc) · 5.58 KB
/
collect_with_extend.rs
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use orx_concurrent_bag::*;
use std::time::Duration;
#[allow(dead_code)]
#[derive(Clone, Copy)]
struct LargeData {
a: [i32; 64],
}
#[allow(dead_code)]
fn compute_data_i32(i: usize, j: usize) -> i32 {
(i * 100000 + j) as i32
}
#[allow(dead_code)]
fn compute_large_data(i: usize, j: usize) -> LargeData {
let mut a = [0i32; 64];
#[allow(clippy::needless_range_loop)]
for k in 0..64 {
if k == i {
a[k] = (i - j) as i32;
} else if k == j {
a[k] = (j + i) as i32;
} else {
a[k] = (i + j + k) as i32;
}
}
LargeData { a }
}
fn with_concurrent_bag<T: Sync, P: PinnedVec<T>>(
num_threads: usize,
num_items_per_thread: usize,
do_sleep: bool,
compute: fn(usize, usize) -> T,
batch_size: usize,
bag: ConcurrentBag<T, P>,
) -> ConcurrentBag<T, P> {
let bag_ref = &bag;
std::thread::scope(|s| {
for i in 0..num_threads {
s.spawn(move || {
sleep(do_sleep, i);
for j in (0..num_items_per_thread).step_by(batch_size) {
let into_iter =
(j..(j + batch_size)).map(|j| std::hint::black_box(compute(i, j)));
bag_ref.extend(into_iter);
}
});
}
});
bag
}
fn with_rayon<T: Send + Sync + Clone + Copy>(
num_threads: usize,
num_items_per_thread: usize,
do_sleep: bool,
compute: fn(usize, usize) -> T,
) -> Vec<T> {
use rayon::prelude::*;
let result: Vec<_> = (0..num_threads)
.into_par_iter()
.flat_map(|i| {
sleep(do_sleep, i);
(0..num_items_per_thread)
.map(move |j| std::hint::black_box(compute(i, j)))
.collect::<Vec<_>>()
})
.collect();
result
}
fn sleep(do_sleep: bool, i: usize) {
if do_sleep {
let modulus = i % 3;
let nanoseconds = match modulus {
0 => 0,
1 => 10 + (i % 11) * 4,
_ => 20 - (i % 5) * 3,
} as u64;
let duration = Duration::from_nanos(nanoseconds);
std::thread::sleep(duration);
}
}
fn bench_grow(c: &mut Criterion) {
let thread_info: [(usize, usize); 2] = [(8, 16384), (8, 65536)];
let workload_info = [false, true];
let treatments: Vec<_> = workload_info
.iter()
.flat_map(|&w| thread_info.iter().map(move |(a, b)| (*a, *b, w)))
.collect();
let mut group = c.benchmark_group("grow");
for (num_threads, num_items_per_thread, add_workload) in treatments {
let treatment = format!(
"workload={},num_threads={},num_items_per_thread-type=[{}]",
add_workload, num_threads, num_items_per_thread
);
let max_len = num_threads * num_items_per_thread;
// rayon
group.bench_with_input(BenchmarkId::new("with_rayon", &treatment), &(), |b, _| {
b.iter(|| {
black_box(with_rayon(
black_box(num_threads),
black_box(num_items_per_thread),
add_workload,
compute_large_data,
))
})
});
// ConcurrentBag
let batch_sizes = vec![1, 2, 4, 16, 64, num_items_per_thread];
for batch_size in batch_sizes {
let name = |pinned_type: &str| {
format!(
"with_concurrent_bag({}) | batch-size={}",
pinned_type, batch_size
)
};
group.bench_with_input(
BenchmarkId::new(name("Doubling"), &treatment),
&(),
|b, _| {
b.iter(|| {
black_box(with_concurrent_bag(
black_box(num_threads),
black_box(num_items_per_thread),
add_workload,
compute_large_data,
batch_size,
ConcurrentBag::with_doubling_growth(),
))
})
},
);
let fragment_size = 2usize.pow(12);
let num_linear_fragments = (max_len / fragment_size) + 1;
group.bench_with_input(
BenchmarkId::new(name("Linear(12)"), &treatment),
&(),
|b, _| {
b.iter(|| {
black_box(with_concurrent_bag(
black_box(num_threads),
black_box(num_items_per_thread),
add_workload,
compute_large_data,
batch_size,
ConcurrentBag::with_linear_growth(12, num_linear_fragments),
))
})
},
);
group.bench_with_input(BenchmarkId::new(name("Fixed"), &treatment), &(), |b, _| {
b.iter(|| {
black_box(with_concurrent_bag(
black_box(num_threads),
black_box(num_items_per_thread),
add_workload,
compute_large_data,
batch_size,
ConcurrentBag::with_fixed_capacity(num_threads * num_items_per_thread),
))
})
});
}
}
group.finish();
}
criterion_group!(benches, bench_grow);
criterion_main!(benches);