Skip to content

Commit

Permalink
Various cleanups (#770)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
samueltardieu committed Apr 1, 2024
1 parent 9f1db4a commit f1ea31a
Show file tree
Hide file tree
Showing 34 changed files with 1,380 additions and 1,425 deletions.
42 changes: 21 additions & 21 deletions bencher_compat/benches/bencher_example.rs
@@ -1,22 +1,22 @@
#[macro_use]
extern crate criterion_bencher_compat;

use criterion_bencher_compat::Bencher;

fn a(bench: &mut Bencher) {
bench.iter(|| {
(0..1000).fold(0, |x, y| x + y)
})
}

fn b(bench: &mut Bencher) {
const N: usize = 1024;
bench.iter(|| {
vec![0u8; N]
});

bench.bytes = N as u64;
}

benchmark_group!(benches, a, b);
#[macro_use]
extern crate criterion_bencher_compat;

use criterion_bencher_compat::Bencher;

fn a(bench: &mut Bencher) {
bench.iter(|| {
(0..1000).fold(0, |x, y| x + y)
})
}

fn b(bench: &mut Bencher) {
const N: usize = 1024;
bench.iter(|| {
vec![0u8; N]
});

bench.bytes = N as u64;
}

benchmark_group!(benches, a, b);
benchmark_main!(benches);
52 changes: 26 additions & 26 deletions benches/benchmarks/sampling_mode.rs
@@ -1,26 +1,26 @@
use criterion::{criterion_group, Criterion, SamplingMode};
use std::thread::sleep;
use std::time::Duration;

fn sampling_mode_tests(c: &mut Criterion) {
let mut group = c.benchmark_group("sampling_mode");

group.sampling_mode(SamplingMode::Auto);
group.bench_function("Auto", |bencher| {
bencher.iter(|| sleep(Duration::from_millis(0)))
});

group.sampling_mode(SamplingMode::Linear);
group.bench_function("Linear", |bencher| {
bencher.iter(|| sleep(Duration::from_millis(0)))
});

group.sampling_mode(SamplingMode::Flat);
group.bench_function("Flat", |bencher| {
bencher.iter(|| sleep(Duration::from_millis(10)))
});

group.finish();
}

criterion_group!(benches, sampling_mode_tests,);
use criterion::{criterion_group, Criterion, SamplingMode};
use std::thread::sleep;
use std::time::Duration;

fn sampling_mode_tests(c: &mut Criterion) {
let mut group = c.benchmark_group("sampling_mode");

group.sampling_mode(SamplingMode::Auto);
group.bench_function("Auto", |bencher| {
bencher.iter(|| sleep(Duration::from_millis(0)))
});

group.sampling_mode(SamplingMode::Linear);
group.bench_function("Linear", |bencher| {
bencher.iter(|| sleep(Duration::from_millis(0)))
});

group.sampling_mode(SamplingMode::Flat);
group.bench_function("Flat", |bencher| {
bencher.iter(|| sleep(Duration::from_millis(10)))
});

group.finish();
}

criterion_group!(benches, sampling_mode_tests,);
52 changes: 26 additions & 26 deletions macro/benches/test_macro_bench.rs
@@ -1,27 +1,27 @@
#![feature(custom_test_frameworks)]
#![test_runner(criterion::runner)]

use criterion::{Criterion, black_box};
use criterion_macro::criterion;

fn fibonacci(n: u64) -> u64 {
match n {
0 | 1 => 1,
n => fibonacci(n - 1) + fibonacci(n - 2),
}
}

fn custom_criterion() -> Criterion {
Criterion::default()
.sample_size(50)
}

#[criterion]
fn bench_simple(c: &mut Criterion) {
c.bench_function("Fibonacci-Simple", |b| b.iter(|| fibonacci(black_box(10))));
}

#[criterion(custom_criterion())]
fn bench_custom(c: &mut Criterion) {
c.bench_function("Fibonacci-Custom", |b| b.iter(|| fibonacci(black_box(20))));
#![feature(custom_test_frameworks)]
#![test_runner(criterion::runner)]

use criterion::{Criterion, black_box};
use criterion_macro::criterion;

fn fibonacci(n: u64) -> u64 {
match n {
0 | 1 => 1,
n => fibonacci(n - 1) + fibonacci(n - 2),
}
}

fn custom_criterion() -> Criterion {
Criterion::default()
.sample_size(50)
}

#[criterion]
fn bench_simple(c: &mut Criterion) {
c.bench_function("Fibonacci-Simple", |b| b.iter(|| fibonacci(black_box(10))));
}

#[criterion(custom_criterion())]
fn bench_custom(c: &mut Criterion) {
c.bench_function("Fibonacci-Custom", |b| b.iter(|| fibonacci(black_box(20))));
}
110 changes: 55 additions & 55 deletions macro/src/lib.rs
@@ -1,56 +1,56 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro2::{Ident, TokenTree};
use quote::quote_spanned;

#[proc_macro_attribute]
pub fn criterion(attr: TokenStream, item: TokenStream) -> TokenStream {
let attr = proc_macro2::TokenStream::from(attr);
let item = proc_macro2::TokenStream::from(item);

let span = proc_macro2::Span::call_site();

let init = if stream_length(attr.clone()) != 0 {
attr
}
else {
quote_spanned!(span=> criterion::Criterion::default())
};

let function_name = find_name(item.clone());
let wrapped_name = Ident::new(&format!("criterion_wrapped_{}", function_name.to_string()), span);

let output = quote_spanned!(span=>
#[test_case]
pub fn #wrapped_name() {
#item

let mut c = #init.configure_from_args();
#function_name(&mut c);
}
);

output.into()
}

fn stream_length(stream: proc_macro2::TokenStream) -> usize {
stream.into_iter().count()
}

fn find_name(stream: proc_macro2::TokenStream) -> Ident {
let mut iter = stream.into_iter();
while let Some(tok) = iter.next() {
if let TokenTree::Ident(ident) = tok {
if ident == "fn" {
break;
}
}
}

if let Some(TokenTree::Ident(name)) = iter.next() {
name
}
else {
panic!("Unable to find function name")
}
extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro2::{Ident, TokenTree};
use quote::quote_spanned;

#[proc_macro_attribute]
pub fn criterion(attr: TokenStream, item: TokenStream) -> TokenStream {
let attr = proc_macro2::TokenStream::from(attr);
let item = proc_macro2::TokenStream::from(item);

let span = proc_macro2::Span::call_site();

let init = if stream_length(attr.clone()) != 0 {
attr
}
else {
quote_spanned!(span=> criterion::Criterion::default())
};

let function_name = find_name(item.clone());
let wrapped_name = Ident::new(&format!("criterion_wrapped_{}", function_name.to_string()), span);

let output = quote_spanned!(span=>
#[test_case]
pub fn #wrapped_name() {
#item

let mut c = #init.configure_from_args();
#function_name(&mut c);
}
);

output.into()
}

fn stream_length(stream: proc_macro2::TokenStream) -> usize {
stream.into_iter().count()
}

fn find_name(stream: proc_macro2::TokenStream) -> Ident {
let mut iter = stream.into_iter();
while let Some(tok) = iter.next() {
if let TokenTree::Ident(ident) = tok {
if ident == "fn" {
break;
}
}
}

if let Some(TokenTree::Ident(name)) = iter.next() {
name
}
else {
panic!("Unable to find function name")
}
}
2 changes: 1 addition & 1 deletion plot/src/data.rs
Expand Up @@ -155,7 +155,7 @@ where
{
type Scale = (f64, f64, f64, f64, f64);

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

Expand Down
6 changes: 3 additions & 3 deletions plot/src/lib.rs
Expand Up @@ -366,10 +366,10 @@
#![deny(bare_trait_objects)]
// This lint has lots of false positives ATM, see
// https://github.com/Manishearth/rust-clippy/issues/761
#![cfg_attr(feature = "cargo-clippy", allow(clippy::new_without_default))]
#![allow(clippy::new_without_default)]
// False positives with images
#![cfg_attr(feature = "cargo-clippy", allow(clippy::doc_markdown))]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::many_single_char_names))]
#![allow(clippy::doc_markdown)]
#![allow(clippy::many_single_char_names)]

extern crate cast;
#[macro_use]
Expand Down
8 changes: 4 additions & 4 deletions plot/src/proxy.rs
Expand Up @@ -7,7 +7,7 @@ use std::borrow::Cow;
use std::path::Path;

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

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

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

/// Generic constructor for `Output`
#[cfg_attr(feature = "cargo-clippy", allow(clippy::inline_always))]
#[allow(clippy::inline_always)]
#[inline(always)]
pub fn Output<P>(path: P) -> OutputType
where
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/compare.rs
Expand Up @@ -12,7 +12,7 @@ use crate::report::BenchmarkId;
use crate::{fs, Criterion, SavedSample};

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

0 comments on commit f1ea31a

Please sign in to comment.