-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add benchmarks for impl Debug for str
#124551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ use test::{black_box, Bencher}; | |
|
||
mod char_count; | ||
mod corpora; | ||
mod debug; | ||
mod iter; | ||
|
||
#[bench] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//! This primarily benchmarks `impl Debug for str`, | ||
//! and it also explicitly tests that we minimizes calls to the underlying `Write`r. | ||
//! While that is an implementation detail and there are no guarantees about it, | ||
//! we should still try to minimize those calls over time rather than regress them. | ||
|
||
use std::fmt::{self, Write}; | ||
use test::{black_box, Bencher}; | ||
|
||
#[derive(Default)] | ||
struct CountingWriter { | ||
buf: String, | ||
write_calls: usize, | ||
} | ||
|
||
impl Write for CountingWriter { | ||
fn write_str(&mut self, s: &str) -> fmt::Result { | ||
self.buf.push_str(s); | ||
self.write_calls += 1; | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn assert_fmt(s: &str, expected: &str, expected_write_calls: usize) { | ||
let mut w = CountingWriter::default(); | ||
|
||
write!(&mut w, "{s:?}").unwrap(); | ||
assert_eq!(s.len(), 64); | ||
assert_eq!(w.buf, expected); | ||
assert_eq!(w.write_calls, expected_write_calls); | ||
} | ||
|
||
#[bench] | ||
fn ascii_only(b: &mut Bencher) { | ||
let s = "just a bit of ascii text that has no escapes. 64 bytes exactly!!"; | ||
assert_fmt(s, r#""just a bit of ascii text that has no escapes. 64 bytes exactly!!""#, 3); | ||
b.iter(|| { | ||
black_box(format!("{:?}", black_box(s))); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn ascii_escapes(b: &mut Bencher) { | ||
let s = "some\tmore\tascii\ttext\nthis time with some \"escapes\", also 64 byte"; | ||
assert_fmt( | ||
s, | ||
r#""some\tmore\tascii\ttext\nthis time with some \"escapes\", also 64 byte""#, | ||
21, | ||
); | ||
b.iter(|| { | ||
black_box(format!("{:?}", black_box(s))); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn some_unicode(b: &mut Bencher) { | ||
let s = "egy kis szöveg néhány unicode betűvel. legyen ez is 64 byte."; | ||
assert_fmt(s, r#""egy kis szöveg néhány unicode betűvel. legyen ez is 64 byte.""#, 3); | ||
b.iter(|| { | ||
black_box(format!("{:?}", black_box(s))); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn mostly_unicode(b: &mut Bencher) { | ||
let s = "предложение из кириллических букв."; | ||
assert_fmt(s, r#""предложение из кириллических букв.""#, 3); | ||
b.iter(|| { | ||
black_box(format!("{:?}", black_box(s))); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn mixed(b: &mut Bencher) { | ||
let s = "\"❤️\"\n\"hűha ez betű\"\n\"кириллических букв\"."; | ||
assert_fmt(s, r#""\"❤\u{fe0f}\"\n\"hűha ez betű\"\n\"кириллических букв\".""#, 36); | ||
b.iter(|| { | ||
black_box(format!("{:?}", black_box(s))); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally it's better to separate tests from benchmarks. That makes it easier to rewrite the benchmarks in case the optimizer gets too clever and optimizes things away. And it measures fewer things which should increase the SNR.
Right now this would also be benchmarking string comparison.
I know we have a few benchmarks which mix things, but they're not a good example to follow.
You can still write them in a single
#[bench]
function by running the asserts outside the benchmark loop.Or you can declare the test strings as consts so they can be reused between tests and benches.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I first tried to extract those to the
tests
directory, but then ended up leaving them together inside the#[bench]
fn outside of the benchmark loop as you suggested.IMO it makes sense to co-locate these, as especially counting / optimizing the
write
calls is done purely for performance reasons.