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

[Merged by Bors] - Avoid some format! into immediate format! #2913

Closed
wants to merge 2 commits into from

Conversation

CAD97
Copy link
Contributor

@CAD97 CAD97 commented Oct 3, 2021

Objective

  • Avoid usages of format! that ~immediately get passed to another format!. This avoids a temporary allocation and is just generally cleaner.

Solution

  • bevy_derive::shader_defs does a format!("{}", val.to_string()), which is better written as just format!("{}", val)
  • bevy_diagnostic::log_diagnostics_plugin does a format!("{:>}", format!(...)), which is better written as format!("{:>}", format_args!(...))
  • bevy_ecs::schedule does tracing::info!(..., name = &*format!("{:?}", val)), which is better written with the tracing shorthand tracing::info!(..., name = ?val)
  • bevy_reflect::reflect does f.write_str(&format!(...)), which is better written as write!(f, ...) (this could also be written using f.debug_tuple, but I opted to maintain alt debug behavior)
  • bevy_reflect::serde::{ser, de} do serde::Error::custom(format!(...)), which is better written as Error::custom(format_args!(...)), as Error::custom takes impl Display and just immediately calls format! again

@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Oct 3, 2021
@alice-i-cecile alice-i-cecile added C-Code-Quality A section of code that is hard to understand or change S-Needs-Review and removed S-Needs-Triage This issue needs to be labelled labels Oct 3, 2021
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

It all checks out. Thanks!

@CAD97
Copy link
Contributor Author

CAD97 commented Oct 3, 2021

@bjorn3

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=dde575f6343b3818645f5bb7811bca54

Testing alignment for log_diagnostic
println!("diagnostic with average");
println!("=======================");
println!(
    "{:<name_width$}: {:>12} (avg {:>})",
    diagnostic.name,
    format!("{:.6}{:1}", diagnostic.value, diagnostic.suffix),
    format!("{:.6}{:}", diagnostic.average, diagnostic.suffix),
    name_width = NAME_MAX_WIDTH,
);
println!(
    "{:<name_width$}: {:>12} (avg {:>})",
    diagnostic.name,
    format_args!("{:.6}{:1}", diagnostic.value, diagnostic.suffix),
    format_args!("{:.6}{:}", diagnostic.average, diagnostic.suffix),
    name_width = NAME_MAX_WIDTH,
);
println!(
    "{name:<name_width$}: {value:>11.6}{suffix:1} (avg {average:>.6}{suffix:})",
    name = diagnostic.name,
    value = diagnostic.value,
    suffix = diagnostic.suffix,
    average = diagnostic.average,
    name_width = NAME_MAX_WIDTH,
);

println!();
println!("diagnostic without average");
println!("==========================");
println!(
    "{:<name_width$}: {:>}",
    diagnostic.name,
    format!("{:.6}{:1}", diagnostic.value, diagnostic.suffix),
    name_width = NAME_MAX_WIDTH,
);
println!(
    "{:<name_width$}: {:>}",
    diagnostic.name,
    format_args!("{:.6}{:1}", diagnostic.value, diagnostic.suffix),
    name_width = NAME_MAX_WIDTH,
);
println!(
    "{name:<name_width$}: {value:.6}{suffix:}",
    name = diagnostic.name,
    value = diagnostic.value,
    suffix = diagnostic.suffix,
    name_width = NAME_MAX_WIDTH,
);
diagnostic with average
=======================
name      :   64.000000s (avg 64.000000s)
name      : 64.000000s (avg 64.000000s)
name      :   64.000000s (avg 64.000000s)

diagnostic without average
==========================
name      : 64.000000s
name      : 64.000000s
name      : 64.000000s

I've updated both to just not use format_args! and use named format arguments for clarity. This does not add any structured fields; it might be valuable to add value and average as structured data in the future.

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review labels Oct 4, 2021
@mockersf
Copy link
Member

mockersf commented Oct 6, 2021

bors r+

bors bot pushed a commit that referenced this pull request Oct 6, 2021
# Objective

- Avoid usages of `format!` that ~immediately get passed to another `format!`. This avoids a temporary allocation and is just generally cleaner.

## Solution

- `bevy_derive::shader_defs` does a `format!("{}", val.to_string())`, which is better written as just `format!("{}", val)`
- `bevy_diagnostic::log_diagnostics_plugin` does a `format!("{:>}", format!(...))`, which is better written as `format!("{:>}", format_args!(...))`
- `bevy_ecs::schedule` does `tracing::info!(..., name = &*format!("{:?}", val))`, which is better written with the tracing shorthand `tracing::info!(..., name = ?val)`
- `bevy_reflect::reflect` does `f.write_str(&format!(...))`, which is better written as `write!(f, ...)` (this could also be written using `f.debug_tuple`, but I opted to maintain alt debug behavior)
- `bevy_reflect::serde::{ser, de}` do `serde::Error::custom(format!(...))`, which is better written as `Error::custom(format_args!(...))`, as `Error::custom` takes `impl Display` and just immediately calls `format!` again
@bors bors bot changed the title Avoid some format! into immediate format! [Merged by Bors] - Avoid some format! into immediate format! Oct 6, 2021
@bors bors bot closed this Oct 6, 2021
@CAD97 CAD97 deleted the less-format branch October 6, 2021 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Code-Quality A section of code that is hard to understand or change S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants