-
Notifications
You must be signed in to change notification settings - Fork 10
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
Rewrite FormatArg::Display
#26
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
This file contains 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,38 @@ | ||
use criterion::{black_box, criterion_group, criterion_main, Criterion}; | ||
use tr::tr; | ||
|
||
pub fn short_literal(c: &mut Criterion) { | ||
c.bench_function("short_literal", |b| b.iter(|| { | ||
tr!("Hello"); | ||
})); | ||
} | ||
|
||
pub fn long_literal(c: &mut Criterion) { | ||
c.bench_function("long_literal", |b| b.iter(|| { | ||
tr!("Hello, world! This is a longer sentence but without argument markers. That is all for now, thank you for reading."); | ||
})); | ||
} | ||
|
||
pub fn short_argument(c: &mut Criterion) { | ||
c.bench_function("short_argument", |b| b.iter(|| { | ||
tr!("Hello {}!", black_box("world")); | ||
})); | ||
} | ||
|
||
pub fn long_argument(c: &mut Criterion) { | ||
c.bench_function("long_argument", |b| b.iter(|| { | ||
tr!("Hello {} and {} and {} and {} and {} and {} and {} and finally {}!", | ||
black_box("Mercury"), | ||
black_box("Venus"), | ||
black_box("Earth"), | ||
black_box("Mars"), | ||
black_box("Jupiter"), | ||
black_box("Saturn"), | ||
black_box("Uranus"), | ||
black_box("Neptune"), | ||
); | ||
})); | ||
} | ||
|
||
criterion_group!(benches, short_literal, long_literal, short_argument, long_argument); | ||
criterion_main!(benches); |
This file contains 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
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.
Not sure about the capacity here. It is hard to know what's the require capacity since we don't know the size of the arguments. I would have let the default string growing algorithm do its job. But it's true that 2 times the format length is probably fine.
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.
Yeah, there is a trade-off here. I would set at least
fmt_len
, because the final string will almost never be shorter than the format one. If anything{}
could get an empty string, but that is just two bytes less.The thing is that if you write
tr!("a {} b {} c {} d")
, you will do 7 write operations, and I guess at least 3 allocations, depending on the length of the arguments.I've run my benchmarks many times and
2 * fmt_len
seems to be the sweet spot.And since then I've been reading the
std
library; it uses this handy but undocumentedArguments::estimated_capacity
: it does some smart heuristics, and... hey! it doubles some length 🤓!If you agree, I'd leave it as is.