Skip to content

Use write_str() instead of write!() when possible#109175

Closed
kornelski wants to merge 1 commit into
rust-lang:masterfrom
kornelski:write_str
Closed

Use write_str() instead of write!() when possible#109175
kornelski wants to merge 1 commit into
rust-lang:masterfrom
kornelski:write_str

Conversation

@kornelski

Copy link
Copy Markdown
Contributor

write!(formatter, "literal") needlessly constructs fmt::Arguments::new_v1 that doesn't fully optimize out. OTOH formatter.write_str("literal") compiles to just a function call, so it's quicker to compile and results in smaller binaries.

I took care to correct uses of {{ and }} in the formatting string.

@rustbot

rustbot commented Mar 15, 2023

Copy link
Copy Markdown
Collaborator

r? @lcnr

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added A-testsuite Area: The testsuite used to check the correctness of rustc A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Mar 15, 2023
Comment thread src/tools/rust-analyzer/crates/cfg/src/dnf.rs Outdated
@rustbot

rustbot commented Mar 15, 2023

Copy link
Copy Markdown
Collaborator

rustc_macros::diagnostics was changed

cc @davidtwco, @compiler-errors, @JohnTitor, @TaKO8Ki

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

rustc_error_messages was changed

cc @davidtwco, @compiler-errors, @JohnTitor, @TaKO8Ki

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@saethlin

Copy link
Copy Markdown
Member

Does the optimization difference disappear with #106824 ?

It seems like the existing style is the opposite of this PR. Can/should we have a tidy check to enforce this style?

LocalValue::Dead => fmt.write_str(" is dead")?,
LocalValue::Live(Operand::Immediate(Immediate::Uninit)) => {
write!(fmt, " is uninitialized")?
fmt.write_str(" is uninitialized")?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unless benchmarks show that this is actually perf-critical, I'd rather stick to the old code and prioritize code readability and uniformity. It's better (IMO) to have the same uniform code for format strings with and without any interpolation.

@Kobzol

Kobzol commented Mar 15, 2023

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

Let's see the perf. effect of this. I also wonder whether this is required after Mara's inlining PR.

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 15, 2023
@bors

bors commented Mar 15, 2023

Copy link
Copy Markdown
Collaborator

⌛ Trying commit e867567 with merge f48221e382b2eb037dcb7d7de468168f7069c810...

@kornelski

kornelski commented Mar 15, 2023

Copy link
Copy Markdown
Contributor Author

#106824 is nice, but does not address the same thing. It simplifies Arguments passed to the formatter. This PR goes further skips the dynamic part of the formatting and prevents Arguments from being created in the first place. Perhaps on the scale of the whole compiler that's a tiny difference, but on the scale of string-formatting code it halves number of instructions.

The closure-based rewrite of formatting #101568 can achieve the same result in optimized code.

@bors

bors commented Mar 16, 2023

Copy link
Copy Markdown
Collaborator

☀️ Try build successful - checks-actions
Build commit: f48221e382b2eb037dcb7d7de468168f7069c810 (f48221e382b2eb037dcb7d7de468168f7069c810)

@rust-timer

This comment has been minimized.

@m-ou-se

m-ou-se commented Mar 16, 2023

Copy link
Copy Markdown
Member

write!(formatter, "literal") needlessly constructs fmt::Arguments::new_v1 that doesn't fully optimize out.

Hm, it shouldn't be too hard to fix that.

@m-ou-se

m-ou-se commented Mar 16, 2023

Copy link
Copy Markdown
Member

I've added it to the list in #99012:

  • Make sure write!(f, "literal") is just as efficient as f.write_str("literal")

@oli-obk

oli-obk commented Mar 16, 2023

Copy link
Copy Markdown
Contributor

@rust-timer build f48221e382b2eb037dcb7d7de468168f7069c810

let's still have a look at perf so we can add the data to the tracking issue, but close this PR once perf is done.

@oli-obk

oli-obk commented Mar 16, 2023

Copy link
Copy Markdown
Contributor

oh perf is just swamped. will take a bit to get a result here

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (f48221e382b2eb037dcb7d7de468168f7069c810): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.9% [-1.3%, -0.5%] 8
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.2% [-2.2%, -2.2%] 1
Improvements ✅
(secondary)
-2.2% [-2.2%, -2.2%] 1
All ❌✅ (primary) -2.2% [-2.2%, -2.2%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 17, 2023
@oli-obk

oli-obk commented Mar 17, 2023

Copy link
Copy Markdown
Contributor

Looks neutral with some smaller wins that may be noise.

Changing this isn't worth it in the compiler, but cleaning it up in general is still something to explore

@oli-obk oli-obk closed this Mar 17, 2023
@Kobzol

Kobzol commented Mar 17, 2023

Copy link
Copy Markdown
Member

I think that the wins look like more than noise. But I agree that it's probably not worth to merge this, as the wins are not big enough to justify the "worse/inconsistent" code style. Especially if there's hope that Mara's changes can win the perf back here soon-ish.

@lqd

lqd commented Mar 17, 2023

Copy link
Copy Markdown
Member

I also believe the tt-muncher wins are noise: here's how it currently looks

image

@Kobzol

Kobzol commented Mar 17, 2023

Copy link
Copy Markdown
Member

I was looking at the non-relevant results and it seemed to me that there are a lot of green results. In any case, the wins are definitely small, that's true :)

@kornelski kornelski deleted the write_str branch March 17, 2023 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-testsuite Area: The testsuite used to check the correctness of rustc A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.