@@ -4,70 +4,122 @@ extern crate criterion;
4
4
use criterion:: black_box;
5
5
use criterion:: Criterion ;
6
6
7
- use annotate_snippets:: DisplayList ;
8
- use annotate_snippets:: { Annotation , AnnotationType , SourceAnnotation } ;
9
- use annotate_snippets:: { Slice , Snippet } ;
10
-
11
- use annotate_snippets:: renderers:: ascii_default:: styles:: plain:: Style as PlainStyle ;
12
- use annotate_snippets:: renderers:: ascii_default:: Renderer as AsciiRenderer ;
7
+ use annotate_snippets:: * ;
8
+ use std:: ops:: Range ;
13
9
14
10
const SOURCE : & ' static str = r#") -> Option<String> {
15
- for ann in annotations {
16
- match (ann.range.0, ann.range.1) {
17
- (None, None) => continue,
18
- (Some(start), Some(end)) if start > end_index => continue,
19
- (Some(start), Some(end)) if start >= start_index => {
20
- let label = if let Some(ref label) = ann.label {
21
- format!(" {}", label)
22
- } else {
23
- String::from("")
24
- };
11
+ for ann in annotations {
12
+ match (ann.range.0, ann.range.1) {
13
+ (None, None) => continue,
14
+ (Some(start), Some(end)) if start > end_index => continue,
15
+ (Some(start), Some(end)) if start >= start_index => {
16
+ let label = if let Some(ref label) = ann.label {
17
+ format!(" {}", label)
18
+ } else {
19
+ String::from("")
20
+ };
25
21
26
- return Some(format!(
27
- "{}{}{}",
28
- " ".repeat(start - start_index),
29
- "^".repeat(end - start),
30
- label
31
- ));
22
+ return Some(format!(
23
+ "{}{}{}",
24
+ " ".repeat(start - start_index),
25
+ "^".repeat(end - start),
26
+ label
27
+ ));
28
+ }
29
+ _ => continue,
32
30
}
33
- _ => continue,
31
+ }"# ;
32
+
33
+ fn source_snippet ( ) -> Snippet < ' static , WithLineNumber < & ' static str > > {
34
+ Snippet {
35
+ title : Some ( Title {
36
+ code : Some ( & "E0308" ) ,
37
+ message : Message {
38
+ text : & "mismatched types" ,
39
+ level : Level :: Error ,
40
+ } ,
41
+ } ) ,
42
+ slices : & [ Slice {
43
+ span : WithLineNumber {
44
+ line_num : 51 ,
45
+ data : SOURCE ,
46
+ } ,
47
+ origin : Some ( & "src/format.rs" ) ,
48
+ annotations : & [
49
+ Annotation {
50
+ span : 5 ..19 ,
51
+ message : Some ( Message {
52
+ text : & "expected `Option<String>` because of return type" ,
53
+ level : Level :: Warning ,
54
+ } ) ,
55
+ } ,
56
+ Annotation {
57
+ span : 26 ..725 ,
58
+ message : Some ( Message {
59
+ text : & "expected enum `std::option::Option`" ,
60
+ level : Level :: Error ,
61
+ } ) ,
62
+ } ,
63
+ ] ,
64
+ footer : & [ ] ,
65
+ } ] ,
34
66
}
35
- }"# ;
67
+ }
36
68
37
- fn create_snippet ( ) {
38
- let snippet = Snippet {
39
- title : Some ( Annotation {
40
- id : Some ( "E0308" ) ,
41
- label : Some ( "mismatched types" ) ,
42
- annotation_type : AnnotationType :: Error ,
69
+ fn range_snippet ( ) -> Snippet < ' static , Range < usize > > {
70
+ Snippet {
71
+ title : Some ( Title {
72
+ code : Some ( & "E0308" ) ,
73
+ message : Message {
74
+ text : & "mismatched types" ,
75
+ level : Level :: Error ,
76
+ } ,
43
77
} ) ,
44
- footer : & [ ] ,
45
78
slices : & [ Slice {
46
- source : SOURCE ,
47
- line_start : Some ( 51 ) ,
48
- origin : Some ( "src/format.rs" ) ,
79
+ span : 0 ..725 ,
80
+ origin : Some ( & "src/format.rs" ) ,
49
81
annotations : & [
50
- SourceAnnotation {
51
- label : "expected `Option<String>` because of return type" ,
52
- annotation_type : AnnotationType :: Warning ,
53
- range : 5 ..19 ,
82
+ Annotation {
83
+ span : 5 ..19 ,
84
+ message : Some ( Message {
85
+ text : & "expected `Option<String>` because of return type" ,
86
+ level : Level :: Warning ,
87
+ } ) ,
54
88
} ,
55
- SourceAnnotation {
56
- label : "expected enum `std::option::Option`" ,
57
- annotation_type : AnnotationType :: Error ,
58
- range : 23 ..725 ,
89
+ Annotation {
90
+ span : 26 ..725 ,
91
+ message : Some ( Message {
92
+ text : & "expected enum `std::option::Option`" ,
93
+ level : Level :: Error ,
94
+ } ) ,
59
95
} ,
60
96
] ,
97
+ footer : & [ ] ,
61
98
} ] ,
62
- } ;
63
- let r = AsciiRenderer :: < PlainStyle > :: new ( ) ;
64
- let dl: DisplayList = ( & snippet) . into ( ) ;
65
- let mut result: Vec < u8 > = Vec :: new ( ) ;
66
- r. fmt ( & mut result, & dl) . unwrap ( ) ;
99
+ }
67
100
}
68
101
69
102
pub fn criterion_benchmark ( c : & mut Criterion ) {
70
- c. bench_function ( "format" , |b| b. iter ( || black_box ( create_snippet ( ) ) ) ) ;
103
+ c. bench_function ( "format [&str]" , |b| {
104
+ b. iter ( || {
105
+ black_box ( {
106
+ let snippet = source_snippet ( ) ;
107
+ let formatted = format ( & snippet, & ( ) ) ;
108
+ let mut out: Vec < u8 > = Vec :: new ( ) ;
109
+ renderer:: Ascii :: plain ( ) . render ( & formatted, & ( ) , & mut out)
110
+ } )
111
+ } )
112
+ } ) ;
113
+ c. bench_function ( "format [Range]" , |b| {
114
+ b. iter ( || {
115
+ black_box ( {
116
+ let snippet = range_snippet ( ) ;
117
+ let formatted = format ( & snippet, & SOURCE ) ;
118
+ let mut out: Vec < u8 > = Vec :: new ( ) ;
119
+ renderer:: Ascii :: plain ( ) . render ( & formatted, & SOURCE , & mut out)
120
+ } )
121
+ } )
122
+ } ) ;
71
123
}
72
124
73
125
criterion_group ! ( benches, criterion_benchmark) ;
0 commit comments