Skip to content

Commit c28a62f

Browse files
committed
Add benchmarks for impl Debug for str
In order to inform future perf improvements and prevent regressions, lets add some benchmarks that stress `impl Debug for str`.
1 parent f9dca46 commit c28a62f

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

library/core/benches/str.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use test::{black_box, Bencher};
33

44
mod char_count;
55
mod corpora;
6+
mod debug;
67
mod iter;
78

89
#[bench]

library/core/benches/str/debug.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use test::{black_box, Bencher};
2+
3+
#[bench]
4+
fn ascii_only(b: &mut Bencher) {
5+
b.iter(|| {
6+
let s = "just a bit of ascii text that has no escapes. 64 bytes exactly!!";
7+
black_box(format!("{:?}", black_box(s)));
8+
});
9+
}
10+
11+
#[bench]
12+
fn ascii_escapes(b: &mut Bencher) {
13+
b.iter(|| {
14+
let s = "some\tmore\tascii\ttext\nthis time with some \"escapes\", also 64 byte";
15+
black_box(format!("{:?}", black_box(s)));
16+
});
17+
}
18+
19+
#[bench]
20+
fn some_unicode(b: &mut Bencher) {
21+
b.iter(|| {
22+
let s = "egy kis szöveg néhány unicode betűvel. legyen ez is 64 byte.";
23+
black_box(format!("{:?}", black_box(s)));
24+
});
25+
}
26+
27+
#[bench]
28+
fn mostly_unicode(b: &mut Bencher) {
29+
b.iter(|| {
30+
let s = "предложение из кириллических букв.";
31+
black_box(format!("{:?}", black_box(s)));
32+
});
33+
}
34+
35+
#[bench]
36+
fn mixed(b: &mut Bencher) {
37+
b.iter(|| {
38+
let s = "\"❤️\"\n\"hűha ez betű\"\n\"кириллических букв\".";
39+
black_box(format!("{:?}", black_box(s)));
40+
});
41+
}

library/core/tests/fmt/debug.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//! This primarily tests that `impl Debug for str` is correct for a number of inputs,
2+
//! but also that it minimizes calls to the underlying `Write`r.
3+
//! While that is an implementation detail and there are no guarantees about it,
4+
//! we should still try to minimize those calls over time rather than regress them.
5+
//!
6+
//! See also the benchmarks in `core/benches/str/debug.rs`.
7+
use std::fmt::{self, Write};
8+
9+
#[derive(Default)]
10+
struct CountingWriter {
11+
buf: String,
12+
write_calls: usize,
13+
}
14+
15+
impl Write for CountingWriter {
16+
fn write_str(&mut self, s: &str) -> fmt::Result {
17+
self.buf.push_str(s);
18+
self.write_calls += 1;
19+
Ok(())
20+
}
21+
}
22+
23+
fn run_fmt(s: &str, expected: &str, expected_write_calls: usize) {
24+
let mut w = CountingWriter::default();
25+
26+
write!(&mut w, "{s:?}").unwrap();
27+
assert_eq!(s.len(), 64);
28+
assert_eq!(w.buf, expected);
29+
assert_eq!(w.write_calls, expected_write_calls);
30+
}
31+
32+
#[test]
33+
fn ascii_only() {
34+
run_fmt(
35+
"just a bit of ascii text that has no escapes. 64 bytes exactly!!",
36+
r#""just a bit of ascii text that has no escapes. 64 bytes exactly!!""#,
37+
3,
38+
);
39+
}
40+
41+
#[test]
42+
fn ascii_escapes() {
43+
run_fmt(
44+
"some\tmore\tascii\ttext\nthis time with some \"escapes\", also 64 byte",
45+
r#""some\tmore\tascii\ttext\nthis time with some \"escapes\", also 64 byte""#,
46+
21,
47+
);
48+
}
49+
50+
#[test]
51+
fn some_unicode() {
52+
run_fmt(
53+
"egy kis szöveg néhány unicode betűvel. legyen ez is 64 byte.",
54+
r#""egy kis szöveg néhány unicode betűvel. legyen ez is 64 byte.""#,
55+
3,
56+
);
57+
}
58+
59+
#[test]
60+
fn mostly_unicode() {
61+
run_fmt("предложение из кириллических букв.", r#""предложение из кириллических букв.""#, 3);
62+
}
63+
64+
#[test]
65+
fn mixed() {
66+
run_fmt(
67+
"\"❤️\"\n\"hűha ez betű\"\n\"кириллических букв\".",
68+
r#""\"❤\u{fe0f}\"\n\"hűha ez betű\"\n\"кириллических букв\".""#,
69+
36,
70+
);
71+
}

library/core/tests/fmt/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod builders;
2+
mod debug;
23
mod float;
34
mod num;
45

0 commit comments

Comments
 (0)