Skip to content

Commit

Permalink
Format libcore with rustfmt (including tests and benches)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Dec 7, 2019
1 parent 24d7f72 commit c737169
Show file tree
Hide file tree
Showing 49 changed files with 1,308 additions and 1,171 deletions.
2 changes: 1 addition & 1 deletion src/libcore/benches/any.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::any::*;
use test::{Bencher, black_box};
use test::{black_box, Bencher};

#[bench]
fn bench_downcast_ref(b: &mut Bencher) {
Expand Down
20 changes: 8 additions & 12 deletions src/libcore/benches/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,9 @@
//
// Therefore:
fn branchless_to_ascii_upper_case(byte: u8) -> u8 {
byte &
!(
(
byte.wrapping_add(0x1f) &
!byte.wrapping_add(0x05) &
0x80
) >> 2
)
byte & !((byte.wrapping_add(0x1f) & !byte.wrapping_add(0x05) & 0x80) >> 2)
}


macro_rules! benches {
($( fn $name: ident($arg: ident: &mut [u8]) $body: block )+ @iter $( $is_: ident, )+) => {
benches! {@
Expand Down Expand Up @@ -254,12 +246,15 @@ benches! {
}

macro_rules! repeat {
($s: expr) => { concat!($s, $s, $s, $s, $s, $s, $s, $s, $s, $s) }
($s: expr) => {
concat!($s, $s, $s, $s, $s, $s, $s, $s, $s, $s)
};
}

const SHORT: &'static str = "Alice's";
const MEDIUM: &'static str = "Alice's Adventures in Wonderland";
const LONG: &'static str = repeat!(r#"
const LONG: &'static str = repeat!(
r#"
La Guida di Bragia, a Ballad Opera for the Marionette Theatre (around 1850)
Alice's Adventures in Wonderland (1865)
Phantasmagoria and Other Poems (1869)
Expand All @@ -275,7 +270,8 @@ const LONG: &'static str = repeat!(r#"
What the Tortoise Said to Achilles (1895)
Three Sunsets and Other Poems (1898)
The Manlet (1903)[106]
"#);
"#
);

#[rustfmt::skip]
const ASCII_UPPERCASE_MAP: [u8; 256] = [
Expand Down
13 changes: 9 additions & 4 deletions src/libcore/benches/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ fn bench_to_digit_radix_36(b: &mut Bencher) {

#[bench]
fn bench_to_digit_radix_var(b: &mut Bencher) {
b.iter(|| CHARS.iter().cycle()
.zip(RADIX.iter().cycle())
.take(10_000)
.map(|(c, radix)| c.to_digit(*radix)).min())
b.iter(|| {
CHARS
.iter()
.cycle()
.zip(RADIX.iter().cycle())
.take(10_000)
.map(|(c, radix)| c.to_digit(*radix))
.min()
})
}
2 changes: 1 addition & 1 deletion src/libcore/benches/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::io::{self, Write as IoWrite};
use std::fmt::{self, Write as FmtWrite};
use std::io::{self, Write as IoWrite};
use test::Bencher;

#[bench]
Expand Down
48 changes: 15 additions & 33 deletions src/libcore/benches/hash/sip.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(deprecated)]

use core::hash::*;
use test::{Bencher, black_box};
use test::{black_box, Bencher};

fn hash_bytes<H: Hasher>(mut s: H, x: &[u8]) -> u64 {
Hasher::write(&mut s, x);
Expand Down Expand Up @@ -44,11 +44,11 @@ fn bench_str_over_8_bytes(b: &mut Bencher) {
#[bench]
fn bench_long_str(b: &mut Bencher) {
let s = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor \
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud \
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute \
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla \
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui \
officia deserunt mollit anim id est laborum.";
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud \
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute \
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla \
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui \
officia deserunt mollit anim id est laborum.";
b.iter(|| {
assert_eq!(hash(&s), 17717065544121360093);
})
Expand All @@ -58,9 +58,7 @@ officia deserunt mollit anim id est laborum.";
fn bench_u32(b: &mut Bencher) {
let u = 162629500u32;
let u = black_box(u);
b.iter(|| {
hash(&u)
});
b.iter(|| hash(&u));
b.bytes = 8;
}

Expand All @@ -70,72 +68,56 @@ fn bench_u32_keyed(b: &mut Bencher) {
let u = black_box(u);
let k1 = black_box(0x1);
let k2 = black_box(0x2);
b.iter(|| {
hash_with(SipHasher::new_with_keys(k1, k2), &u)
});
b.iter(|| hash_with(SipHasher::new_with_keys(k1, k2), &u));
b.bytes = 8;
}

#[bench]
fn bench_u64(b: &mut Bencher) {
let u = 16262950014981195938u64;
let u = black_box(u);
b.iter(|| {
hash(&u)
});
b.iter(|| hash(&u));
b.bytes = 8;
}

#[bench]
fn bench_bytes_4(b: &mut Bencher) {
let data = black_box([b' '; 4]);
b.iter(|| {
hash_bytes(SipHasher::default(), &data)
});
b.iter(|| hash_bytes(SipHasher::default(), &data));
b.bytes = 4;
}

#[bench]
fn bench_bytes_7(b: &mut Bencher) {
let data = black_box([b' '; 7]);
b.iter(|| {
hash_bytes(SipHasher::default(), &data)
});
b.iter(|| hash_bytes(SipHasher::default(), &data));
b.bytes = 7;
}

#[bench]
fn bench_bytes_8(b: &mut Bencher) {
let data = black_box([b' '; 8]);
b.iter(|| {
hash_bytes(SipHasher::default(), &data)
});
b.iter(|| hash_bytes(SipHasher::default(), &data));
b.bytes = 8;
}

#[bench]
fn bench_bytes_a_16(b: &mut Bencher) {
let data = black_box([b' '; 16]);
b.iter(|| {
hash_bytes(SipHasher::default(), &data)
});
b.iter(|| hash_bytes(SipHasher::default(), &data));
b.bytes = 16;
}

#[bench]
fn bench_bytes_b_32(b: &mut Bencher) {
let data = black_box([b' '; 32]);
b.iter(|| {
hash_bytes(SipHasher::default(), &data)
});
b.iter(|| hash_bytes(SipHasher::default(), &data));
b.bytes = 32;
}

#[bench]
fn bench_bytes_c_128(b: &mut Bencher) {
let data = black_box([b' '; 128]);
b.iter(|| {
hash_bytes(SipHasher::default(), &data)
});
b.iter(|| hash_bytes(SipHasher::default(), &data));
b.bytes = 128;
}
65 changes: 33 additions & 32 deletions src/libcore/benches/iter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::iter::*;
use test::{Bencher, black_box};
use test::{black_box, Bencher};

#[bench]
fn bench_rposition(b: &mut Bencher) {
Expand All @@ -14,7 +14,11 @@ fn bench_skip_while(b: &mut Bencher) {
b.iter(|| {
let it = 0..100;
let mut sum = 0;
it.skip_while(|&x| { sum += x; sum < 4000 }).all(|_| true);
it.skip_while(|&x| {
sum += x;
sum < 4000
})
.all(|_| true);
});
}

Expand All @@ -29,7 +33,9 @@ fn bench_multiple_take(b: &mut Bencher) {
});
}

fn scatter(x: i32) -> i32 { (x * 31) % 127 }
fn scatter(x: i32) -> i32 {
(x * 31) % 127
}

#[bench]
fn bench_max_by_key(b: &mut Bencher) {
Expand Down Expand Up @@ -76,23 +82,21 @@ pub fn add_zip(xs: &[f32], ys: &mut [f32]) {
fn bench_zip_copy(b: &mut Bencher) {
let source = vec![0u8; 16 * 1024];
let mut dst = black_box(vec![0u8; 16 * 1024]);
b.iter(|| {
copy_zip(&source, &mut dst)
})
b.iter(|| copy_zip(&source, &mut dst))
}

#[bench]
fn bench_zip_add(b: &mut Bencher) {
let source = vec![1.; 16 * 1024];
let mut dst = vec![0.; 16 * 1024];
b.iter(|| {
add_zip(&source, &mut dst)
});
b.iter(|| add_zip(&source, &mut dst));
}

/// `Iterator::for_each` implemented as a plain loop.
fn for_each_loop<I, F>(iter: I, mut f: F) where
I: Iterator, F: FnMut(I::Item)
fn for_each_loop<I, F>(iter: I, mut f: F)
where
I: Iterator,
F: FnMut(I::Item),
{
for item in iter {
f(item);
Expand All @@ -101,8 +105,10 @@ fn for_each_loop<I, F>(iter: I, mut f: F) where

/// `Iterator::for_each` implemented with `fold` for internal iteration.
/// (except when `by_ref()` effectively disables that optimization.)
fn for_each_fold<I, F>(iter: I, mut f: F) where
I: Iterator, F: FnMut(I::Item)
fn for_each_fold<I, F>(iter: I, mut f: F)
where
I: Iterator,
F: FnMut(I::Item),
{
iter.fold((), move |(), item| f(item));
}
Expand Down Expand Up @@ -137,25 +143,20 @@ fn bench_for_each_chain_ref_fold(b: &mut Bencher) {
});
}


/// Helper to benchmark `sum` for iterators taken by value which
/// can optimize `fold`, and by reference which cannot.
macro_rules! bench_sums {
($bench_sum:ident, $bench_ref_sum:ident, $iter:expr) => {
#[bench]
fn $bench_sum(b: &mut Bencher) {
b.iter(|| -> i64 {
$iter.map(black_box).sum()
});
b.iter(|| -> i64 { $iter.map(black_box).sum() });
}

#[bench]
fn $bench_ref_sum(b: &mut Bencher) {
b.iter(|| -> i64 {
$iter.map(black_box).by_ref().sum()
});
b.iter(|| -> i64 { $iter.map(black_box).by_ref().sum() });
}
}
};
}

bench_sums! {
Expand Down Expand Up @@ -286,7 +287,10 @@ fn bench_zip_then_skip(b: &mut Bencher) {
let t: Vec<_> = (0..100_000).collect();

b.iter(|| {
let s = v.iter().zip(t.iter()).skip(10000)
let s = v
.iter()
.zip(t.iter())
.skip(10000)
.take_while(|t| *t.0 < 10100)
.map(|(a, b)| *a + *b)
.sum::<u64>();
Expand All @@ -299,7 +303,10 @@ fn bench_skip_then_zip(b: &mut Bencher) {
let t: Vec<_> = (0..100_000).collect();

b.iter(|| {
let s = v.iter().skip(10000).zip(t.iter().skip(10000))
let s = v
.iter()
.skip(10000)
.zip(t.iter().skip(10000))
.take_while(|t| *t.0 < 10100)
.map(|(a, b)| *a + *b)
.sum::<u64>();
Expand All @@ -309,23 +316,17 @@ fn bench_skip_then_zip(b: &mut Bencher) {

#[bench]
fn bench_filter_count(b: &mut Bencher) {
b.iter(|| {
(0i64..1000000).map(black_box).filter(|x| x % 3 == 0).count()
})
b.iter(|| (0i64..1000000).map(black_box).filter(|x| x % 3 == 0).count())
}

#[bench]
fn bench_filter_ref_count(b: &mut Bencher) {
b.iter(|| {
(0i64..1000000).map(black_box).by_ref().filter(|x| x % 3 == 0).count()
})
b.iter(|| (0i64..1000000).map(black_box).by_ref().filter(|x| x % 3 == 0).count())
}

#[bench]
fn bench_filter_chain_count(b: &mut Bencher) {
b.iter(|| {
(0i64..1000000).chain(0..1000000).map(black_box).filter(|x| x % 3 == 0).count()
})
b.iter(|| (0i64..1000000).chain(0..1000000).map(black_box).filter(|x| x % 3 == 0).count())
}

#[bench]
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/benches/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ extern crate test;
mod any;
mod ascii;
mod char;
mod fmt;
mod hash;
mod iter;
mod num;
mod ops;
mod slice;
mod fmt;
6 changes: 3 additions & 3 deletions src/libcore/benches/num/flt2dec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ mod strategy {
mod grisu;
}

use core::num::flt2dec::MAX_SIG_DIGITS;
use core::num::flt2dec::{decode, DecodableFloat, Decoded, FullDecoded};
use std::f64;
use std::io::Write;
use std::vec::Vec;
use test::Bencher;
use core::num::flt2dec::{decode, DecodableFloat, FullDecoded, Decoded};
use core::num::flt2dec::MAX_SIG_DIGITS;

pub fn decode_finite<T: DecodableFloat>(v: T) -> Decoded {
match decode(v).1 {
FullDecoded::Finite(decoded) => decoded,
full_decoded => panic!("expected finite, got {:?} instead", full_decoded)
full_decoded => panic!("expected finite, got {:?} instead", full_decoded),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcore/benches/num/flt2dec/strategy/dragon.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{i16, f64};
use super::super::*;
use core::num::flt2dec::strategy::dragon::*;
use std::{f64, i16};
use test::Bencher;

#[bench]
Expand Down
Loading

0 comments on commit c737169

Please sign in to comment.