]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/benches/lib.rs
Rollup merge of #85464 - steffahn:fix_ub_in_ptr_swap_docs, r=dtolnay
[rust.git] / compiler / rustc_middle / benches / lib.rs
1 #![feature(test)]
2
3 extern crate test;
4
5 use test::Bencher;
6
7 // Static/dynamic method dispatch
8
9 struct Struct {
10     field: isize,
11 }
12
13 trait Trait {
14     fn method(&self) -> isize;
15 }
16
17 impl Trait for Struct {
18     fn method(&self) -> isize {
19         self.field
20     }
21 }
22
23 #[bench]
24 fn trait_vtable_method_call(b: &mut Bencher) {
25     let s = Struct { field: 10 };
26     let t = &s as &dyn Trait;
27     b.iter(|| t.method());
28 }
29
30 #[bench]
31 fn trait_static_method_call(b: &mut Bencher) {
32     let s = Struct { field: 10 };
33     b.iter(|| s.method());
34 }
35
36 // Overhead of various match forms
37
38 #[bench]
39 fn option_some(b: &mut Bencher) {
40     let x = Some(10);
41     b.iter(|| match x {
42         Some(y) => y,
43         None => 11,
44     });
45 }
46
47 #[bench]
48 fn vec_pattern(b: &mut Bencher) {
49     let x = [1, 2, 3, 4, 5, 6];
50     b.iter(|| match x {
51         [1, 2, 3, ..] => 10,
52         _ => 11,
53     });
54 }