Skip to content

Commit f1ea31a

Browse files
Various cleanups (#770)
* Remove redundant imports * Remove unused trait * Do not use deprecated "cargo-clippy" feature check Lints starting with "clippy::" are already recognized as being applicable to Clippy only. * Replace objects instead of recreating them `.clone_into()` avoids a whole object deallocation and reallocation. Flagged by recent Clippy. * Define generic parameter bound in one place only Flagged by recent Clippy. * Use Unix end-of-line convention for Rust source files * Add missing documentation comment
1 parent 9f1db4a commit f1ea31a

34 files changed

+1380
-1425
lines changed
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
#[macro_use]
2-
extern crate criterion_bencher_compat;
3-
4-
use criterion_bencher_compat::Bencher;
5-
6-
fn a(bench: &mut Bencher) {
7-
bench.iter(|| {
8-
(0..1000).fold(0, |x, y| x + y)
9-
})
10-
}
11-
12-
fn b(bench: &mut Bencher) {
13-
const N: usize = 1024;
14-
bench.iter(|| {
15-
vec![0u8; N]
16-
});
17-
18-
bench.bytes = N as u64;
19-
}
20-
21-
benchmark_group!(benches, a, b);
1+
#[macro_use]
2+
extern crate criterion_bencher_compat;
3+
4+
use criterion_bencher_compat::Bencher;
5+
6+
fn a(bench: &mut Bencher) {
7+
bench.iter(|| {
8+
(0..1000).fold(0, |x, y| x + y)
9+
})
10+
}
11+
12+
fn b(bench: &mut Bencher) {
13+
const N: usize = 1024;
14+
bench.iter(|| {
15+
vec![0u8; N]
16+
});
17+
18+
bench.bytes = N as u64;
19+
}
20+
21+
benchmark_group!(benches, a, b);
2222
benchmark_main!(benches);

benches/benchmarks/sampling_mode.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
use criterion::{criterion_group, Criterion, SamplingMode};
2-
use std::thread::sleep;
3-
use std::time::Duration;
4-
5-
fn sampling_mode_tests(c: &mut Criterion) {
6-
let mut group = c.benchmark_group("sampling_mode");
7-
8-
group.sampling_mode(SamplingMode::Auto);
9-
group.bench_function("Auto", |bencher| {
10-
bencher.iter(|| sleep(Duration::from_millis(0)))
11-
});
12-
13-
group.sampling_mode(SamplingMode::Linear);
14-
group.bench_function("Linear", |bencher| {
15-
bencher.iter(|| sleep(Duration::from_millis(0)))
16-
});
17-
18-
group.sampling_mode(SamplingMode::Flat);
19-
group.bench_function("Flat", |bencher| {
20-
bencher.iter(|| sleep(Duration::from_millis(10)))
21-
});
22-
23-
group.finish();
24-
}
25-
26-
criterion_group!(benches, sampling_mode_tests,);
1+
use criterion::{criterion_group, Criterion, SamplingMode};
2+
use std::thread::sleep;
3+
use std::time::Duration;
4+
5+
fn sampling_mode_tests(c: &mut Criterion) {
6+
let mut group = c.benchmark_group("sampling_mode");
7+
8+
group.sampling_mode(SamplingMode::Auto);
9+
group.bench_function("Auto", |bencher| {
10+
bencher.iter(|| sleep(Duration::from_millis(0)))
11+
});
12+
13+
group.sampling_mode(SamplingMode::Linear);
14+
group.bench_function("Linear", |bencher| {
15+
bencher.iter(|| sleep(Duration::from_millis(0)))
16+
});
17+
18+
group.sampling_mode(SamplingMode::Flat);
19+
group.bench_function("Flat", |bencher| {
20+
bencher.iter(|| sleep(Duration::from_millis(10)))
21+
});
22+
23+
group.finish();
24+
}
25+
26+
criterion_group!(benches, sampling_mode_tests,);

macro/benches/test_macro_bench.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
#![feature(custom_test_frameworks)]
2-
#![test_runner(criterion::runner)]
3-
4-
use criterion::{Criterion, black_box};
5-
use criterion_macro::criterion;
6-
7-
fn fibonacci(n: u64) -> u64 {
8-
match n {
9-
0 | 1 => 1,
10-
n => fibonacci(n - 1) + fibonacci(n - 2),
11-
}
12-
}
13-
14-
fn custom_criterion() -> Criterion {
15-
Criterion::default()
16-
.sample_size(50)
17-
}
18-
19-
#[criterion]
20-
fn bench_simple(c: &mut Criterion) {
21-
c.bench_function("Fibonacci-Simple", |b| b.iter(|| fibonacci(black_box(10))));
22-
}
23-
24-
#[criterion(custom_criterion())]
25-
fn bench_custom(c: &mut Criterion) {
26-
c.bench_function("Fibonacci-Custom", |b| b.iter(|| fibonacci(black_box(20))));
1+
#![feature(custom_test_frameworks)]
2+
#![test_runner(criterion::runner)]
3+
4+
use criterion::{Criterion, black_box};
5+
use criterion_macro::criterion;
6+
7+
fn fibonacci(n: u64) -> u64 {
8+
match n {
9+
0 | 1 => 1,
10+
n => fibonacci(n - 1) + fibonacci(n - 2),
11+
}
12+
}
13+
14+
fn custom_criterion() -> Criterion {
15+
Criterion::default()
16+
.sample_size(50)
17+
}
18+
19+
#[criterion]
20+
fn bench_simple(c: &mut Criterion) {
21+
c.bench_function("Fibonacci-Simple", |b| b.iter(|| fibonacci(black_box(10))));
22+
}
23+
24+
#[criterion(custom_criterion())]
25+
fn bench_custom(c: &mut Criterion) {
26+
c.bench_function("Fibonacci-Custom", |b| b.iter(|| fibonacci(black_box(20))));
2727
}

macro/src/lib.rs

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
1-
extern crate proc_macro;
2-
use proc_macro::TokenStream;
3-
use proc_macro2::{Ident, TokenTree};
4-
use quote::quote_spanned;
5-
6-
#[proc_macro_attribute]
7-
pub fn criterion(attr: TokenStream, item: TokenStream) -> TokenStream {
8-
let attr = proc_macro2::TokenStream::from(attr);
9-
let item = proc_macro2::TokenStream::from(item);
10-
11-
let span = proc_macro2::Span::call_site();
12-
13-
let init = if stream_length(attr.clone()) != 0 {
14-
attr
15-
}
16-
else {
17-
quote_spanned!(span=> criterion::Criterion::default())
18-
};
19-
20-
let function_name = find_name(item.clone());
21-
let wrapped_name = Ident::new(&format!("criterion_wrapped_{}", function_name.to_string()), span);
22-
23-
let output = quote_spanned!(span=>
24-
#[test_case]
25-
pub fn #wrapped_name() {
26-
#item
27-
28-
let mut c = #init.configure_from_args();
29-
#function_name(&mut c);
30-
}
31-
);
32-
33-
output.into()
34-
}
35-
36-
fn stream_length(stream: proc_macro2::TokenStream) -> usize {
37-
stream.into_iter().count()
38-
}
39-
40-
fn find_name(stream: proc_macro2::TokenStream) -> Ident {
41-
let mut iter = stream.into_iter();
42-
while let Some(tok) = iter.next() {
43-
if let TokenTree::Ident(ident) = tok {
44-
if ident == "fn" {
45-
break;
46-
}
47-
}
48-
}
49-
50-
if let Some(TokenTree::Ident(name)) = iter.next() {
51-
name
52-
}
53-
else {
54-
panic!("Unable to find function name")
55-
}
1+
extern crate proc_macro;
2+
use proc_macro::TokenStream;
3+
use proc_macro2::{Ident, TokenTree};
4+
use quote::quote_spanned;
5+
6+
#[proc_macro_attribute]
7+
pub fn criterion(attr: TokenStream, item: TokenStream) -> TokenStream {
8+
let attr = proc_macro2::TokenStream::from(attr);
9+
let item = proc_macro2::TokenStream::from(item);
10+
11+
let span = proc_macro2::Span::call_site();
12+
13+
let init = if stream_length(attr.clone()) != 0 {
14+
attr
15+
}
16+
else {
17+
quote_spanned!(span=> criterion::Criterion::default())
18+
};
19+
20+
let function_name = find_name(item.clone());
21+
let wrapped_name = Ident::new(&format!("criterion_wrapped_{}", function_name.to_string()), span);
22+
23+
let output = quote_spanned!(span=>
24+
#[test_case]
25+
pub fn #wrapped_name() {
26+
#item
27+
28+
let mut c = #init.configure_from_args();
29+
#function_name(&mut c);
30+
}
31+
);
32+
33+
output.into()
34+
}
35+
36+
fn stream_length(stream: proc_macro2::TokenStream) -> usize {
37+
stream.into_iter().count()
38+
}
39+
40+
fn find_name(stream: proc_macro2::TokenStream) -> Ident {
41+
let mut iter = stream.into_iter();
42+
while let Some(tok) = iter.next() {
43+
if let TokenTree::Ident(ident) = tok {
44+
if ident == "fn" {
45+
break;
46+
}
47+
}
48+
}
49+
50+
if let Some(TokenTree::Ident(name)) = iter.next() {
51+
name
52+
}
53+
else {
54+
panic!("Unable to find function name")
55+
}
5656
}

plot/src/data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ where
155155
{
156156
type Scale = (f64, f64, f64, f64, f64);
157157

158-
#[cfg_attr(feature = "cargo-clippy", allow(clippy::many_single_char_names))]
158+
#[allow(clippy::many_single_char_names)]
159159
fn append_to(self, buffer: &mut Vec<u8>, scale: (f64, f64, f64, f64, f64)) {
160160
let (a, b, c, d, e) = self;
161161

plot/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,10 @@
366366
#![deny(bare_trait_objects)]
367367
// This lint has lots of false positives ATM, see
368368
// https://github.com/Manishearth/rust-clippy/issues/761
369-
#![cfg_attr(feature = "cargo-clippy", allow(clippy::new_without_default))]
369+
#![allow(clippy::new_without_default)]
370370
// False positives with images
371-
#![cfg_attr(feature = "cargo-clippy", allow(clippy::doc_markdown))]
372-
#![cfg_attr(feature = "cargo-clippy", allow(clippy::many_single_char_names))]
371+
#![allow(clippy::doc_markdown)]
372+
#![allow(clippy::many_single_char_names)]
373373

374374
extern crate cast;
375375
#[macro_use]

plot/src/proxy.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::borrow::Cow;
77
use std::path::Path;
88

99
/// Generic constructor for `Font`
10-
#[cfg_attr(feature = "cargo-clippy", allow(clippy::inline_always))]
10+
#[allow(clippy::inline_always)]
1111
#[inline(always)]
1212
pub fn Font<S>(string: S) -> FontType
1313
where
@@ -17,7 +17,7 @@ where
1717
}
1818

1919
/// Generic constructor for `Label`
20-
#[cfg_attr(feature = "cargo-clippy", allow(clippy::inline_always))]
20+
#[allow(clippy::inline_always)]
2121
#[inline(always)]
2222
pub fn Label<S>(string: S) -> LabelType
2323
where
@@ -27,7 +27,7 @@ where
2727
}
2828

2929
/// Generic constructor for `Title`
30-
#[cfg_attr(feature = "cargo-clippy", allow(clippy::inline_always))]
30+
#[allow(clippy::inline_always)]
3131
#[inline(always)]
3232
pub fn Title<S>(string: S) -> TitleType
3333
where
@@ -37,7 +37,7 @@ where
3737
}
3838

3939
/// Generic constructor for `Output`
40-
#[cfg_attr(feature = "cargo-clippy", allow(clippy::inline_always))]
40+
#[allow(clippy::inline_always)]
4141
#[inline(always)]
4242
pub fn Output<P>(path: P) -> OutputType
4343
where

src/analysis/compare.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::report::BenchmarkId;
1212
use crate::{fs, Criterion, SavedSample};
1313

1414
// Common comparison procedure
15-
#[cfg_attr(feature = "cargo-clippy", allow(clippy::type_complexity))]
15+
#[allow(clippy::type_complexity)]
1616
pub(crate) fn common<M: Measurement>(
1717
id: &BenchmarkId,
1818
avg_times: &Sample<f64>,

0 commit comments

Comments
 (0)