Skip to content

Commit 74d77c5

Browse files
committed
Add benchmark for source file analysis
x86 generic: ``` test analyze_source_file::tests::bench_empty_text ... bench: 12.88 ns/iter (+/- 0.68) test analyze_source_file::tests::bench_newlines_long ... bench: 626.39 ns/iter (+/- 19.02) test analyze_source_file::tests::bench_newlines_short ... bench: 155.36 ns/iter (+/- 8.00) ``` x86 SSE2: ``` test analyze_source_file::tests::bench_empty_text ... bench: 14.48 ns/iter (+/- 0.34) test analyze_source_file::tests::bench_newlines_long ... bench: 236.33 ns/iter (+/- 5.35) test analyze_source_file::tests::bench_newlines_short ... bench: 148.13 ns/iter (+/- 3.60) ```
1 parent f2824da commit 74d77c5

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

compiler/rustc_span/src/analyze_source_file/tests.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
extern crate test;
2+
use test::Bencher;
3+
14
use super::*;
25

36
macro_rules! test {
4-
(case: $test_name:ident,
7+
(case: $name:ident,
58
text: $text:expr,
69
lines: $lines:expr,
710
multi_byte_chars: $multi_byte_chars:expr,) => {
811
#[test]
9-
fn $test_name() {
12+
fn ${concat(test_, $name)}() {
1013
let (lines, multi_byte_chars) = analyze_source_file($text);
1114

1215
let expected_lines: Vec<RelativeBytePos> =
@@ -107,3 +110,34 @@ test!(
107110
lines: vec![0, 7, 27],
108111
multi_byte_chars: vec![(13, 2), (29, 2)],
109112
);
113+
114+
macro_rules! bench {
115+
(case: $name:ident,
116+
text: $text:expr,
117+
repeat: $repeat:expr,) => {
118+
#[bench]
119+
fn ${concat(bench_, $name)}(b: &mut Bencher) {
120+
use std::hint::black_box;
121+
let text = $text.repeat($repeat);
122+
b.iter(|| black_box(analyze_source_file(&text)));
123+
}
124+
};
125+
}
126+
127+
bench!(
128+
case: empty_text,
129+
text: "",
130+
repeat: 1,
131+
);
132+
133+
bench!(
134+
case: newlines_short,
135+
text: "a\nc",
136+
repeat: 50,
137+
);
138+
139+
bench!(
140+
case: newlines_long,
141+
text: "012345678\nabcdef012345678\na",
142+
repeat: 50,
143+
);

compiler/rustc_span/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
#![feature(cfg_select)]
2424
#![feature(core_io_borrowed_buf)]
2525
#![feature(if_let_guard)]
26+
#![feature(macro_metavar_expr_concat)]
2627
#![feature(map_try_insert)]
2728
#![feature(negative_impls)]
2829
#![feature(read_buf)]
2930
#![feature(round_char_boundary)]
3031
#![feature(rustc_attrs)]
3132
#![feature(rustdoc_internals)]
33+
#![feature(test)]
3234
// tidy-alphabetical-end
3335

3436
// The code produced by the `Encodable`/`Decodable` derive macros refer to

0 commit comments

Comments
 (0)