Skip to content
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

Prefer to_string to format!("{}") #7581

Merged
merged 2 commits into from
Sep 7, 2022
Merged

Prefer to_string to format!("{}") #7581

merged 2 commits into from
Sep 7, 2022

Conversation

mina86
Copy link
Contributor

@mina86 mina86 commented Sep 7, 2022

As per benchmarks in #7568, using
format! as opposed to calling to_string directly has ~100ns
overhead. There’s no reason not to get rid of that overhead especially
since using to_string is actually shorter to type.

Note that it may be beneficial to further optimise integer formatting
by using fixed-size buffer and custom conversion which doesn’t use
std. This optimisation is outside the scope of this commit.

As per benchmarks in near#7568, using
format! as opposed to calling to_string directly has ~100ns overhead.
There’s no reason not to get rid of that overhead especially since
using to_string is actually shorter to type.

Note that it may be beneficial to further optimise integer formatting
by using fixed-size buffer and custom conversion which doesn’t use
std.  This optimisation is outside the scope of this commit.
@mina86 mina86 requested a review from a team as a code owner September 7, 2022 14:24
@nikurt
Copy link
Contributor

nikurt commented Sep 7, 2022

Let's wait for the end of the investigation.
increment_counter! macro from metrics-rs/metrics seems promising.

Copy link
Contributor

@matklad matklad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't worry about performance here: if format! vs to_string matters you probably should not be using strnigs at all.

But this is a good change on the grounds of code style alone, format!("{}", foo) is highly unidiomatic, foo.to_string() is the way to spell that.

@mina86
Copy link
Contributor Author

mina86 commented Sep 7, 2022

Let's wait for the end of the investigation. increment_counter! macro from metrics-rs/metrics seems promising.

This changes more than just instances with metrics so even if metrics are changed we’ll still want to change other places to use to_string.

@near-bulldozer near-bulldozer bot merged commit 4b05571 into near:master Sep 7, 2022
@mina86 mina86 deleted the b branch September 7, 2022 18:44
nikurt pushed a commit that referenced this pull request Nov 9, 2022
As per benchmarks in #7568, using
format! as opposed to calling to_string directly has ~100ns
overhead. There’s no reason not to get rid of that overhead especially
since using to_string is actually shorter to type.

Note that it may be beneficial to further optimise integer formatting
by using fixed-size buffer and custom conversion which doesn’t use
std.  This optimisation is outside the scope of this commit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants