Skip to content

Commit ac329cd

Browse files
authored
Rollup merge of rust-lang#39501 - phungleson:libcorebench, r=alexcrichton
Extract libcore benchmarks to a separate folder Fix rust-lang#39484 r? @alexcrichton since you seem to know about this :) Thanks!
2 parents b3518af + 34f444d commit ac329cd

File tree

21 files changed

+667
-486
lines changed

21 files changed

+667
-486
lines changed

src/libcore/Cargo.toml

+3-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ bench = false
1313
name = "coretest"
1414
path = "../libcoretest/lib.rs"
1515

16-
# FIXME: need to extract benchmarks to a separate crate
17-
#[[bench]]
18-
#name = "coretest"
19-
#path = "../libcoretest/lib.rs"
16+
[[bench]]
17+
name = "corebench"
18+
path = "../libcore/bench/lib.rs"

src/libcore/bench/any.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use core::any::*;
12+
use test::{Bencher, black_box};
13+
14+
#[bench]
15+
fn bench_downcast_ref(b: &mut Bencher) {
16+
b.iter(|| {
17+
let mut x = 0;
18+
let mut y = &mut x as &mut Any;
19+
black_box(&mut y);
20+
black_box(y.downcast_ref::<isize>() == Some(&0));
21+
});
22+
}

src/libcore/bench/hash/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
mod sip;

src/libcore/bench/hash/sip.rs

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(deprecated)]
12+
13+
use core::hash::*;
14+
use test::{Bencher, black_box};
15+
16+
fn hash_bytes<H: Hasher>(mut s: H, x: &[u8]) -> u64 {
17+
Hasher::write(&mut s, x);
18+
s.finish()
19+
}
20+
21+
fn hash_with<H: Hasher, T: Hash>(mut st: H, x: &T) -> u64 {
22+
x.hash(&mut st);
23+
st.finish()
24+
}
25+
26+
fn hash<T: Hash>(x: &T) -> u64 {
27+
hash_with(SipHasher::new(), x)
28+
}
29+
30+
#[bench]
31+
fn bench_str_under_8_bytes(b: &mut Bencher) {
32+
let s = "foo";
33+
b.iter(|| {
34+
assert_eq!(hash(&s), 16262950014981195938);
35+
})
36+
}
37+
38+
#[bench]
39+
fn bench_str_of_8_bytes(b: &mut Bencher) {
40+
let s = "foobar78";
41+
b.iter(|| {
42+
assert_eq!(hash(&s), 4898293253460910787);
43+
})
44+
}
45+
46+
#[bench]
47+
fn bench_str_over_8_bytes(b: &mut Bencher) {
48+
let s = "foobarbaz0";
49+
b.iter(|| {
50+
assert_eq!(hash(&s), 10581415515220175264);
51+
})
52+
}
53+
54+
#[bench]
55+
fn bench_long_str(b: &mut Bencher) {
56+
let s = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor \
57+
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud \
58+
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute \
59+
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla \
60+
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui \
61+
officia deserunt mollit anim id est laborum.";
62+
b.iter(|| {
63+
assert_eq!(hash(&s), 17717065544121360093);
64+
})
65+
}
66+
67+
#[bench]
68+
fn bench_u32(b: &mut Bencher) {
69+
let u = 162629500u32;
70+
let u = black_box(u);
71+
b.iter(|| {
72+
hash(&u)
73+
});
74+
b.bytes = 8;
75+
}
76+
77+
#[bench]
78+
fn bench_u32_keyed(b: &mut Bencher) {
79+
let u = 162629500u32;
80+
let u = black_box(u);
81+
let k1 = black_box(0x1);
82+
let k2 = black_box(0x2);
83+
b.iter(|| {
84+
hash_with(SipHasher::new_with_keys(k1, k2), &u)
85+
});
86+
b.bytes = 8;
87+
}
88+
89+
#[bench]
90+
fn bench_u64(b: &mut Bencher) {
91+
let u = 16262950014981195938u64;
92+
let u = black_box(u);
93+
b.iter(|| {
94+
hash(&u)
95+
});
96+
b.bytes = 8;
97+
}
98+
99+
#[bench]
100+
fn bench_bytes_4(b: &mut Bencher) {
101+
let data = black_box([b' '; 4]);
102+
b.iter(|| {
103+
hash_bytes(SipHasher::default(), &data)
104+
});
105+
b.bytes = 4;
106+
}
107+
108+
#[bench]
109+
fn bench_bytes_7(b: &mut Bencher) {
110+
let data = black_box([b' '; 7]);
111+
b.iter(|| {
112+
hash_bytes(SipHasher::default(), &data)
113+
});
114+
b.bytes = 7;
115+
}
116+
117+
#[bench]
118+
fn bench_bytes_8(b: &mut Bencher) {
119+
let data = black_box([b' '; 8]);
120+
b.iter(|| {
121+
hash_bytes(SipHasher::default(), &data)
122+
});
123+
b.bytes = 8;
124+
}
125+
126+
#[bench]
127+
fn bench_bytes_a_16(b: &mut Bencher) {
128+
let data = black_box([b' '; 16]);
129+
b.iter(|| {
130+
hash_bytes(SipHasher::default(), &data)
131+
});
132+
b.bytes = 16;
133+
}
134+
135+
#[bench]
136+
fn bench_bytes_b_32(b: &mut Bencher) {
137+
let data = black_box([b' '; 32]);
138+
b.iter(|| {
139+
hash_bytes(SipHasher::default(), &data)
140+
});
141+
b.bytes = 32;
142+
}
143+
144+
#[bench]
145+
fn bench_bytes_c_128(b: &mut Bencher) {
146+
let data = black_box([b' '; 128]);
147+
b.iter(|| {
148+
hash_bytes(SipHasher::default(), &data)
149+
});
150+
b.bytes = 128;
151+
}

src/libcore/bench/iter.rs

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use core::iter::*;
12+
use test::{Bencher, black_box};
13+
14+
#[bench]
15+
fn bench_rposition(b: &mut Bencher) {
16+
let it: Vec<usize> = (0..300).collect();
17+
b.iter(|| {
18+
it.iter().rposition(|&x| x <= 150);
19+
});
20+
}
21+
22+
#[bench]
23+
fn bench_skip_while(b: &mut Bencher) {
24+
b.iter(|| {
25+
let it = 0..100;
26+
let mut sum = 0;
27+
it.skip_while(|&x| { sum += x; sum < 4000 }).all(|_| true);
28+
});
29+
}
30+
31+
#[bench]
32+
fn bench_multiple_take(b: &mut Bencher) {
33+
let mut it = (0..42).cycle();
34+
b.iter(|| {
35+
let n = it.next().unwrap();
36+
for _ in 0..n {
37+
it.clone().take(it.next().unwrap()).all(|_| true);
38+
}
39+
});
40+
}
41+
42+
fn scatter(x: i32) -> i32 { (x * 31) % 127 }
43+
44+
#[bench]
45+
fn bench_max_by_key(b: &mut Bencher) {
46+
b.iter(|| {
47+
let it = 0..100;
48+
it.max_by_key(|&x| scatter(x))
49+
})
50+
}
51+
52+
// http://www.reddit.com/r/rust/comments/31syce/using_iterators_to_find_the_index_of_the_min_or/
53+
#[bench]
54+
fn bench_max_by_key2(b: &mut Bencher) {
55+
fn max_index_iter(array: &[i32]) -> usize {
56+
array.iter().enumerate().max_by_key(|&(_, item)| item).unwrap().0
57+
}
58+
59+
let mut data = vec![0; 1638];
60+
data[514] = 9999;
61+
62+
b.iter(|| max_index_iter(&data));
63+
}
64+
65+
#[bench]
66+
fn bench_max(b: &mut Bencher) {
67+
b.iter(|| {
68+
let it = 0..100;
69+
it.map(scatter).max()
70+
})
71+
}
72+
73+
pub fn copy_zip(xs: &[u8], ys: &mut [u8]) {
74+
for (a, b) in ys.iter_mut().zip(xs) {
75+
*a = *b;
76+
}
77+
}
78+
79+
pub fn add_zip(xs: &[f32], ys: &mut [f32]) {
80+
for (a, b) in ys.iter_mut().zip(xs) {
81+
*a += *b;
82+
}
83+
}
84+
85+
#[bench]
86+
fn bench_zip_copy(b: &mut Bencher) {
87+
let source = vec![0u8; 16 * 1024];
88+
let mut dst = black_box(vec![0u8; 16 * 1024]);
89+
b.iter(|| {
90+
copy_zip(&source, &mut dst)
91+
})
92+
}
93+
94+
#[bench]
95+
fn bench_zip_add(b: &mut Bencher) {
96+
let source = vec![1.; 16 * 1024];
97+
let mut dst = vec![0.; 16 * 1024];
98+
b.iter(|| {
99+
add_zip(&source, &mut dst)
100+
});
101+
}

src/libcore/bench/lib.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![deny(warnings)]
12+
13+
#![feature(flt2dec)]
14+
#![feature(slice_patterns)]
15+
#![feature(test)]
16+
17+
extern crate core;
18+
extern crate test;
19+
20+
mod any;
21+
mod hash;
22+
mod iter;
23+
mod mem;
24+
mod num;
25+
mod ops;

src/libcore/bench/mem.rs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use test::Bencher;
12+
13+
// FIXME #13642 (these benchmarks should be in another place)
14+
// Completely miscellaneous language-construct benchmarks.
15+
// Static/dynamic method dispatch
16+
17+
struct Struct {
18+
field: isize
19+
}
20+
21+
trait Trait {
22+
fn method(&self) -> isize;
23+
}
24+
25+
impl Trait for Struct {
26+
fn method(&self) -> isize {
27+
self.field
28+
}
29+
}
30+
31+
#[bench]
32+
fn trait_vtable_method_call(b: &mut Bencher) {
33+
let s = Struct { field: 10 };
34+
let t = &s as &Trait;
35+
b.iter(|| {
36+
t.method()
37+
});
38+
}
39+
40+
#[bench]
41+
fn trait_static_method_call(b: &mut Bencher) {
42+
let s = Struct { field: 10 };
43+
b.iter(|| {
44+
s.method()
45+
});
46+
}
47+
48+
// Overhead of various match forms
49+
50+
#[bench]
51+
fn match_option_some(b: &mut Bencher) {
52+
let x = Some(10);
53+
b.iter(|| {
54+
match x {
55+
Some(y) => y,
56+
None => 11
57+
}
58+
});
59+
}
60+
61+
#[bench]
62+
fn match_vec_pattern(b: &mut Bencher) {
63+
let x = [1,2,3,4,5,6];
64+
b.iter(|| {
65+
match x {
66+
[1,2,3,..] => 10,
67+
_ => 11,
68+
}
69+
});
70+
}

0 commit comments

Comments
 (0)