Skip to content

Commit 2c507d3

Browse files
committed
Use a more lightweight benchmarking library
Criterion is a slow and giant dependency that pulls in more giant dependencies. There is no particularly compelling reason to use it here over a more lightweight implementation, and removing it also improves cold dev build times by 25% (because all dev-dependencies are built in both bench and test modes and there is no way to control this; see rust-lang/cargo#1596). This also adds the ‘handwritten’ benchmark implementation test to the test runner and fixes one of its tests, which had been silently failing.
1 parent cdcea4e commit 2c507d3

File tree

7 files changed

+271
-323
lines changed

7 files changed

+271
-323
lines changed

Cargo.toml

+6-18
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ license = "MIT OR Apache-2.0"
1111
publish = false # Use `release.sh`
1212
documentation = "https://docs.rs/modular-bitfield"
1313
repository = "https://github.com/Robbepop/modular-bitfield"
14-
rust-version = "1.56.0"
14+
rust-version = "1.66.0"
1515
version = "0.12.0-pre"
1616

1717
[package]
@@ -34,37 +34,25 @@ modular-bitfield-impl = { path = "impl", version = "0.12.0-pre" }
3434
static_assertions = "1.1"
3535

3636
[dev-dependencies]
37-
# To run benchmarks, these packages need to be present, but until
38-
# <https://github.com/rust-lang/cargo/issues/1596> is fixed, there is no way to
39-
# have conditional dev-dependencies. Criterion is a fat dependency with its own
40-
# fat dependencies and this causes dev builds to take longer than they need to.
41-
# Until the benches can be rewritten to use libtest or similar just disable
42-
# building these crates, since they are practically never used. (Using a regular
43-
# conditional feature would work, but that would cause this feature and these
44-
# packages to show up as optional runtime dependencies, which they never
45-
# actually are.)
46-
# bitfield = "0.19"
47-
# criterion = "0.5"
37+
bitfield = "0.19"
38+
tiny-bench = "0.4"
4839
trybuild = "1.0"
4940

5041
[[bench]]
5142
name = "benchmarks"
52-
path = "benches/benchmarks.rs"
5343
harness = false
5444

5545
[[bench]]
5646
name = "cmp_handwritten"
57-
path = "benches/cmp_handwritten.rs"
5847
harness = false
5948

6049
[[bench]]
6150
name = "cmp_bitfield_crate"
62-
path = "benches/cmp_bitfield_crate.rs"
6351
harness = false
6452

65-
[[bin]]
66-
name = "playground"
67-
path = "playground.rs"
53+
[[test]]
54+
name = "benches_handwritten"
55+
path = "benches/utils/handwritten.rs"
6856

6957
[profile.bench]
7058
codegen-units = 1

benches/benchmarks.rs

+113-163
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22

33
mod utils;
44

5-
use criterion::{black_box, criterion_group, criterion_main, Criterion};
65
use modular_bitfield::{
76
bitfield,
87
specifiers::{B12, B13, B16, B3, B32, B36, B4, B6, B7, B8, B9},
98
};
10-
use utils::repeat;
11-
12-
criterion_group!(bench_get, bench_get_variants);
13-
criterion_group!(bench_set, bench_set_variants);
14-
criterion_main!(bench_get, bench_set);
9+
use utils::*;
1510

1611
#[bitfield]
1712
pub struct Color {
@@ -87,22 +82,19 @@ pub struct Complex {
8782
e: B32, // 4th, .., 7th
8883
}
8984

90-
fn bench_set_variants(c: &mut Criterion) {
91-
let mut g = c.benchmark_group("set_variants");
92-
g.bench_function("Color", |b| {
93-
let mut input = Color::new();
94-
b.iter(|| {
95-
repeat(|| {
96-
black_box(&mut input).set_r(1);
97-
black_box(&mut input).set_g(1);
98-
black_box(&mut input).set_b(1);
99-
black_box(&mut input).set_a(1);
100-
})
85+
fn bench_set_variants() {
86+
one_shot("set - Color", &Color::new, |mut input| {
87+
repeat(|| {
88+
black_box(&mut input).set_r(1);
89+
black_box(&mut input).set_g(1);
90+
black_box(&mut input).set_b(1);
91+
black_box(&mut input).set_a(1);
10192
});
10293
});
103-
g.bench_function("SingleBitsInSingleByte", |b| {
104-
let mut input = SingleBitsInSingleByte::new();
105-
b.iter(|| {
94+
one_shot(
95+
"set - SingleBitsInSingleByte",
96+
&SingleBitsInSingleByte::new,
97+
|mut input| {
10698
repeat(|| {
10799
black_box(&mut input).set_b0(true);
108100
black_box(&mut input).set_b1(true);
@@ -112,105 +104,82 @@ fn bench_set_variants(c: &mut Criterion) {
112104
black_box(&mut input).set_b5(true);
113105
black_box(&mut input).set_b6(true);
114106
black_box(&mut input).set_b7(true);
115-
})
116-
});
117-
});
118-
g.bench_function("TwoHalfBytes", |b| {
119-
let mut input = TwoHalfBytes::new();
120-
b.iter(|| {
121-
repeat(|| {
122-
black_box(&mut input).set_h0(1);
123-
black_box(&mut input).set_h1(1);
124-
})
107+
});
108+
},
109+
);
110+
one_shot("set - TwoHalfBytes", &TwoHalfBytes::new, |mut input| {
111+
repeat(|| {
112+
black_box(&mut input).set_h0(1);
113+
black_box(&mut input).set_h1(1);
125114
});
126115
});
127-
g.bench_function("SingleBitAndRest", |b| {
128-
let mut input = SingleBitAndRest::new();
129-
b.iter(|| {
116+
one_shot(
117+
"set - SingleBitAndRest",
118+
&SingleBitAndRest::new,
119+
|mut input| {
130120
repeat(|| {
131121
black_box(&mut input).set_head(true);
132122
black_box(&mut input).set_rest(1);
133-
})
123+
});
124+
},
125+
);
126+
one_shot("set - B7B1", &B7B1::new, |mut input| {
127+
repeat(|| {
128+
black_box(&mut input).set_b7(1);
129+
black_box(&mut input).set_b1(true);
134130
});
135131
});
136-
g.bench_function("B7B1", |b| {
137-
let mut input = B7B1::new();
138-
b.iter(|| {
139-
repeat(|| {
140-
black_box(&mut input).set_b7(1);
141-
black_box(&mut input).set_b1(true);
142-
})
132+
one_shot("set - B3B1B4", &B3B1B4::new, |mut input| {
133+
repeat(|| {
134+
black_box(&mut input).set_b3(1);
135+
black_box(&mut input).set_b1(true);
136+
black_box(&mut input).set_b4(1);
143137
});
144138
});
145-
g.bench_function("B3B1B4", |b| {
146-
let mut input = B3B1B4::new();
147-
b.iter(|| {
148-
repeat(|| {
149-
black_box(&mut input).set_b3(1);
150-
black_box(&mut input).set_b1(true);
151-
black_box(&mut input).set_b4(1);
152-
})
139+
one_shot("set - TwoHalfWords", &TwoHalfWords::new, |mut input| {
140+
repeat(|| {
141+
black_box(&mut input).set_fst(1);
142+
black_box(&mut input).set_snd(1);
153143
});
154144
});
155-
g.bench_function("TwoHalfWords", |b| {
156-
let mut input = TwoHalfWords::new();
157-
b.iter(|| {
158-
repeat(|| {
159-
black_box(&mut input).set_fst(1);
160-
black_box(&mut input).set_snd(1);
161-
})
162-
});
163-
});
164-
g.bench_function("B6B12B6", |b| {
165-
let mut input = B6B12B6::new();
166-
b.iter(|| {
167-
repeat(|| {
168-
black_box(&mut input).set_front(1);
169-
black_box(&mut input).set_middle(1);
170-
black_box(&mut input).set_back(1);
171-
})
145+
one_shot("set - B6B12B6", &B6B12B6::new, |mut input| {
146+
repeat(|| {
147+
black_box(&mut input).set_front(1);
148+
black_box(&mut input).set_middle(1);
149+
black_box(&mut input).set_back(1);
172150
});
173151
});
174-
g.bench_function("B6B36B6", |b| {
175-
let mut input = B6B36B6::new();
176-
b.iter(|| {
177-
repeat(|| {
178-
black_box(&mut input).set_front(1);
179-
black_box(&mut input).set_middle(1);
180-
black_box(&mut input).set_back(1);
181-
})
152+
one_shot("set - B6B36B6", &B6B36B6::new, |mut input| {
153+
repeat(|| {
154+
black_box(&mut input).set_front(1);
155+
black_box(&mut input).set_middle(1);
156+
black_box(&mut input).set_back(1);
182157
});
183158
});
184-
g.bench_function("Complex", |b| {
185-
let mut input = Complex::new();
186-
b.iter(|| {
187-
repeat(|| {
188-
black_box(&mut input).set_a(1);
189-
black_box(&mut input).set_b(1);
190-
black_box(&mut input).set_c(1);
191-
black_box(&mut input).set_d(1);
192-
black_box(&mut input).set_e(1);
193-
})
159+
one_shot("set - Complex", &Complex::new, |mut input| {
160+
repeat(|| {
161+
black_box(&mut input).set_a(1);
162+
black_box(&mut input).set_b(1);
163+
black_box(&mut input).set_c(1);
164+
black_box(&mut input).set_d(1);
165+
black_box(&mut input).set_e(1);
194166
});
195167
});
196168
}
197169

198-
fn bench_get_variants(c: &mut Criterion) {
199-
let mut g = c.benchmark_group("get");
200-
g.bench_function("Color", |b| {
201-
let input = Color::new();
202-
b.iter(|| {
203-
repeat(|| {
204-
black_box(input.r());
205-
black_box(input.g());
206-
black_box(input.b());
207-
black_box(input.a());
208-
})
170+
fn bench_get_variants() {
171+
one_shot("get - Color", &Color::new, |input| {
172+
repeat(|| {
173+
black_box(input.r());
174+
black_box(input.g());
175+
black_box(input.b());
176+
black_box(input.a());
209177
});
210178
});
211-
g.bench_function("SingleBitsInSingleByte", |b| {
212-
let input = SingleBitsInSingleByte::new();
213-
b.iter(|| {
179+
one_shot(
180+
"get - SingleBitsInSingleByte",
181+
&SingleBitsInSingleByte::new,
182+
|input| {
214183
repeat(|| {
215184
black_box(input.b0());
216185
black_box(input.b1());
@@ -220,85 +189,66 @@ fn bench_get_variants(c: &mut Criterion) {
220189
black_box(input.b5());
221190
black_box(input.b6());
222191
black_box(input.b7());
223-
})
192+
});
193+
},
194+
);
195+
one_shot("get - TwoHalfBytes", &TwoHalfBytes::new, |input| {
196+
repeat(|| {
197+
black_box(input.h0());
198+
black_box(input.h1());
224199
});
225200
});
226-
g.bench_function("TwoHalfBytes", |b| {
227-
let input = TwoHalfBytes::new();
228-
b.iter(|| {
229-
repeat(|| {
230-
black_box(input.h0());
231-
black_box(input.h1());
232-
})
201+
one_shot("get - SingleBitAndRest", &SingleBitAndRest::new, |input| {
202+
repeat(|| {
203+
black_box(input.head());
204+
black_box(input.rest());
233205
});
234206
});
235-
g.bench_function("SingleBitAndRest", |b| {
236-
let input = SingleBitAndRest::new();
237-
b.iter(|| {
238-
repeat(|| {
239-
black_box(input.head());
240-
black_box(input.rest());
241-
})
242-
});
243-
});
244-
g.bench_function("B7B1", |b| {
245-
let input = B7B1::new();
246-
b.iter(|| {
247-
repeat(|| {
248-
black_box(input.b7());
249-
black_box(input.b1());
250-
})
207+
one_shot("get - B7B1", &B7B1::new, |input| {
208+
repeat(|| {
209+
black_box(input.b7());
210+
black_box(input.b1());
251211
});
252212
});
253-
g.bench_function("B3B1B4", |b| {
254-
let input = B3B1B4::new();
255-
b.iter(|| {
256-
repeat(|| {
257-
black_box(input.b3());
258-
black_box(input.b1());
259-
black_box(input.b4());
260-
})
213+
one_shot("get - B3B1B4", &B3B1B4::new, |input| {
214+
repeat(|| {
215+
black_box(input.b3());
216+
black_box(input.b1());
217+
black_box(input.b4());
261218
});
262219
});
263-
g.bench_function("TwoHalfWords", |b| {
264-
let input = TwoHalfWords::new();
265-
b.iter(|| {
266-
repeat(|| {
267-
black_box(input.fst());
268-
black_box(input.snd());
269-
})
220+
one_shot("get - TwoHalfWords", &TwoHalfWords::new, |input| {
221+
repeat(|| {
222+
black_box(input.fst());
223+
black_box(input.snd());
270224
});
271225
});
272-
g.bench_function("B6B12B6", |b| {
273-
let input = B6B12B6::new();
274-
b.iter(|| {
275-
repeat(|| {
276-
black_box(input.front());
277-
black_box(input.middle());
278-
black_box(input.back());
279-
})
226+
one_shot("get - B6B12B6", &B6B12B6::new, |input| {
227+
repeat(|| {
228+
black_box(input.front());
229+
black_box(input.middle());
230+
black_box(input.back());
280231
});
281232
});
282-
g.bench_function("B6B36B6", |b| {
283-
let input = B6B36B6::new();
284-
b.iter(|| {
285-
repeat(|| {
286-
black_box(input.front());
287-
black_box(input.middle());
288-
black_box(input.back());
289-
})
233+
one_shot("get - B6B36B6", &B6B36B6::new, |input| {
234+
repeat(|| {
235+
black_box(input.front());
236+
black_box(input.middle());
237+
black_box(input.back());
290238
});
291239
});
292-
g.bench_function("Complex", |b| {
293-
let input = Complex::new();
294-
b.iter(|| {
295-
repeat(|| {
296-
black_box(input.a());
297-
black_box(input.b());
298-
black_box(input.c());
299-
black_box(input.d());
300-
black_box(input.e());
301-
})
240+
one_shot("get - Complex", &Complex::new, |input| {
241+
repeat(|| {
242+
black_box(input.a());
243+
black_box(input.b());
244+
black_box(input.c());
245+
black_box(input.d());
246+
black_box(input.e());
302247
});
303248
});
304249
}
250+
251+
fn main() {
252+
bench_set_variants();
253+
bench_get_variants();
254+
}

0 commit comments

Comments
 (0)