Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix benchmarks in library/core with black_box #107598

Merged
merged 1 commit into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions library/core/benches/char/methods.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
use test::Bencher;
use test::{black_box, Bencher};

const CHARS: [char; 9] = ['0', 'x', '2', '5', 'A', 'f', '7', '8', '9'];
const RADIX: [u32; 5] = [2, 8, 10, 16, 32];

#[bench]
fn bench_to_digit_radix_2(b: &mut Bencher) {
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_digit(2)).min())
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| black_box(c).to_digit(2)).min())
}

#[bench]
fn bench_to_digit_radix_10(b: &mut Bencher) {
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_digit(10)).min())
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| black_box(c).to_digit(10)).min())
}

#[bench]
fn bench_to_digit_radix_16(b: &mut Bencher) {
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_digit(16)).min())
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| black_box(c).to_digit(16)).min())
}

#[bench]
fn bench_to_digit_radix_36(b: &mut Bencher) {
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_digit(36)).min())
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| black_box(c).to_digit(36)).min())
}

#[bench]
Expand All @@ -31,47 +31,59 @@ fn bench_to_digit_radix_var(b: &mut Bencher) {
.cycle()
.zip(RADIX.iter().cycle())
.take(10_000)
.map(|(c, radix)| c.to_digit(*radix))
.map(|(c, radix)| black_box(c).to_digit(*radix))
.min()
})
}

#[bench]
fn bench_to_ascii_uppercase(b: &mut Bencher) {
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_ascii_uppercase()).min())
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| black_box(c).to_ascii_uppercase()).min())
}

#[bench]
fn bench_to_ascii_lowercase(b: &mut Bencher) {
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| c.to_ascii_lowercase()).min())
b.iter(|| CHARS.iter().cycle().take(10_000).map(|c| black_box(c).to_ascii_lowercase()).min())
}

#[bench]
fn bench_ascii_mix_to_uppercase(b: &mut Bencher) {
b.iter(|| (0..=255).cycle().take(10_000).map(|b| char::from(b).to_uppercase()).count())
b.iter(|| {
(0..=255).cycle().take(10_000).map(|b| black_box(char::from(b)).to_uppercase()).count()
})
}

#[bench]
fn bench_ascii_mix_to_lowercase(b: &mut Bencher) {
b.iter(|| (0..=255).cycle().take(10_000).map(|b| char::from(b).to_lowercase()).count())
b.iter(|| {
(0..=255).cycle().take(10_000).map(|b| black_box(char::from(b)).to_lowercase()).count()
})
}

#[bench]
fn bench_ascii_char_to_uppercase(b: &mut Bencher) {
b.iter(|| (0..=127).cycle().take(10_000).map(|b| char::from(b).to_uppercase()).count())
b.iter(|| {
(0..=127).cycle().take(10_000).map(|b| black_box(char::from(b)).to_uppercase()).count()
})
}

#[bench]
fn bench_ascii_char_to_lowercase(b: &mut Bencher) {
b.iter(|| (0..=127).cycle().take(10_000).map(|b| char::from(b).to_lowercase()).count())
b.iter(|| {
(0..=127).cycle().take(10_000).map(|b| black_box(char::from(b)).to_lowercase()).count()
})
}

#[bench]
fn bench_non_ascii_char_to_uppercase(b: &mut Bencher) {
b.iter(|| (128..=255).cycle().take(10_000).map(|b| char::from(b).to_uppercase()).count())
b.iter(|| {
(128..=255).cycle().take(10_000).map(|b| black_box(char::from(b)).to_uppercase()).count()
})
}

#[bench]
fn bench_non_ascii_char_to_lowercase(b: &mut Bencher) {
b.iter(|| (128..=255).cycle().take(10_000).map(|b| char::from(b).to_lowercase()).count())
b.iter(|| {
(128..=255).cycle().take(10_000).map(|b| black_box(char::from(b)).to_lowercase()).count()
})
}
18 changes: 9 additions & 9 deletions library/core/benches/num/flt2dec/strategy/dragon.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use super::super::*;
use core::num::flt2dec::strategy::dragon::*;
use std::mem::MaybeUninit;
use test::Bencher;
use test::{black_box, Bencher};

#[bench]
fn bench_small_shortest(b: &mut Bencher) {
let decoded = decode_finite(3.141592f64);
let mut buf = [MaybeUninit::new(0); MAX_SIG_DIGITS];
b.iter(|| {
format_shortest(&decoded, &mut buf);
format_shortest(black_box(&decoded), &mut buf);
});
}

Expand All @@ -17,7 +17,7 @@ fn bench_big_shortest(b: &mut Bencher) {
let decoded = decode_finite(f64::MAX);
let mut buf = [MaybeUninit::new(0); MAX_SIG_DIGITS];
b.iter(|| {
format_shortest(&decoded, &mut buf);
format_shortest(black_box(&decoded), &mut buf);
});
}

Expand All @@ -26,7 +26,7 @@ fn bench_small_exact_3(b: &mut Bencher) {
let decoded = decode_finite(3.141592f64);
let mut buf = [MaybeUninit::new(0); 3];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
format_exact(black_box(&decoded), &mut buf, i16::MIN);
});
}

Expand All @@ -35,7 +35,7 @@ fn bench_big_exact_3(b: &mut Bencher) {
let decoded = decode_finite(f64::MAX);
let mut buf = [MaybeUninit::new(0); 3];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
format_exact(black_box(&decoded), &mut buf, i16::MIN);
});
}

Expand All @@ -44,7 +44,7 @@ fn bench_small_exact_12(b: &mut Bencher) {
let decoded = decode_finite(3.141592f64);
let mut buf = [MaybeUninit::new(0); 12];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
format_exact(black_box(&decoded), &mut buf, i16::MIN);
});
}

Expand All @@ -53,7 +53,7 @@ fn bench_big_exact_12(b: &mut Bencher) {
let decoded = decode_finite(f64::MAX);
let mut buf = [MaybeUninit::new(0); 12];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
format_exact(black_box(&decoded), &mut buf, i16::MIN);
});
}

Expand All @@ -62,7 +62,7 @@ fn bench_small_exact_inf(b: &mut Bencher) {
let decoded = decode_finite(3.141592f64);
let mut buf = [MaybeUninit::new(0); 1024];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
format_exact(black_box(&decoded), &mut buf, i16::MIN);
});
}

Expand All @@ -71,6 +71,6 @@ fn bench_big_exact_inf(b: &mut Bencher) {
let decoded = decode_finite(f64::MAX);
let mut buf = [MaybeUninit::new(0); 1024];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
format_exact(black_box(&decoded), &mut buf, i16::MIN);
});
}
18 changes: 9 additions & 9 deletions library/core/benches/num/flt2dec/strategy/grisu.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::super::*;
use core::num::flt2dec::strategy::grisu::*;
use std::mem::MaybeUninit;
use test::Bencher;
use test::{black_box, Bencher};

pub fn decode_finite<T: DecodableFloat>(v: T) -> Decoded {
match decode(v).1 {
Expand All @@ -15,7 +15,7 @@ fn bench_small_shortest(b: &mut Bencher) {
let decoded = decode_finite(3.141592f64);
let mut buf = [MaybeUninit::new(0); MAX_SIG_DIGITS];
b.iter(|| {
format_shortest(&decoded, &mut buf);
format_shortest(black_box(&decoded), &mut buf);
});
}

Expand All @@ -24,7 +24,7 @@ fn bench_big_shortest(b: &mut Bencher) {
let decoded = decode_finite(f64::MAX);
let mut buf = [MaybeUninit::new(0); MAX_SIG_DIGITS];
b.iter(|| {
format_shortest(&decoded, &mut buf);
format_shortest(black_box(&decoded), &mut buf);
});
}

Expand All @@ -33,7 +33,7 @@ fn bench_small_exact_3(b: &mut Bencher) {
let decoded = decode_finite(3.141592f64);
let mut buf = [MaybeUninit::new(0); 3];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
format_exact(black_box(&decoded), &mut buf, i16::MIN);
});
}

Expand All @@ -42,7 +42,7 @@ fn bench_big_exact_3(b: &mut Bencher) {
let decoded = decode_finite(f64::MAX);
let mut buf = [MaybeUninit::new(0); 3];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
format_exact(black_box(&decoded), &mut buf, i16::MIN);
});
}

Expand All @@ -51,7 +51,7 @@ fn bench_small_exact_12(b: &mut Bencher) {
let decoded = decode_finite(3.141592f64);
let mut buf = [MaybeUninit::new(0); 12];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
format_exact(black_box(&decoded), &mut buf, i16::MIN);
});
}

Expand All @@ -60,7 +60,7 @@ fn bench_big_exact_12(b: &mut Bencher) {
let decoded = decode_finite(f64::MAX);
let mut buf = [MaybeUninit::new(0); 12];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
format_exact(black_box(&decoded), &mut buf, i16::MIN);
});
}

Expand All @@ -69,7 +69,7 @@ fn bench_small_exact_inf(b: &mut Bencher) {
let decoded = decode_finite(3.141592f64);
let mut buf = [MaybeUninit::new(0); 1024];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
format_exact(black_box(&decoded), &mut buf, i16::MIN);
});
}

Expand All @@ -78,6 +78,6 @@ fn bench_big_exact_inf(b: &mut Bencher) {
let decoded = decode_finite(f64::MAX);
let mut buf = [MaybeUninit::new(0); 1024];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
format_exact(black_box(&decoded), &mut buf, i16::MIN);
});
}