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

Optimize io::Write::write_fmt for constant strings #138650

Merged
merged 1 commit into from
Mar 21, 2025

Conversation

thaliaarchi
Copy link
Contributor

When the formatting args to fmt::Write::write_fmt are a statically known string, it simplifies to only calling write_str without a runtime branch. Do the same in io::Write::write_fmt with write_all.

Also, match the convention of fmt::Write for the name of args.

When the formatting args to `fmt::Write::write_fmt` are a statically
known string, it simplifies to only calling `write_str` without a
runtime branch. Do the same in `io::Write::write_fmt` with `write_all`.

Also, match the convention of `fmt::Write` for the name of `args`.
@rustbot
Copy link
Collaborator

rustbot commented Mar 18, 2025

r? @ibraheemdev

rustbot has assigned @ibraheemdev.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 18, 2025
@thaliaarchi
Copy link
Contributor Author

thaliaarchi commented Mar 18, 2025

This change is extracted from #137762 and does not use fmt::Arguments::estimated_capacity, unlike the rest of that PR, so is not affected by the heuristics which likely overallocate the capacity. I think this is likely beneficial on its own.

Can we do a perf run?

cc @workingjubilee

@Kobzol
Copy link
Contributor

Kobzol commented Mar 18, 2025

@bors try @rust-timer queue

@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 18, 2025
@bors
Copy link
Contributor

bors commented Mar 18, 2025

⌛ Trying commit a0c3dd4 with merge 8b7fc8c...

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 18, 2025
Optimize `io::Write::write_fmt` for constant strings

When the formatting args to `fmt::Write::write_fmt` are a statically known string, it simplifies to only calling `write_str` without a runtime branch. Do the same in `io::Write::write_fmt` with `write_all`.

Also, match the convention of `fmt::Write` for the name of `args`.
@bors
Copy link
Contributor

bors commented Mar 18, 2025

☀️ Try build successful - checks-actions
Build commit: 8b7fc8c (8b7fc8cbf54ba3210a57b028a2cc040dba8e8150)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (8b7fc8c): comparison URL.

Overall result: no relevant changes - 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 benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results (primary -2.0%, secondary 0.2%)

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)
5.5% [5.5%, 5.5%] 1
Regressions ❌
(secondary)
2.4% [2.4%, 2.4%] 1
Improvements ✅
(primary)
-5.8% [-9.1%, -2.5%] 2
Improvements ✅
(secondary)
-2.0% [-2.0%, -2.0%] 1
All ❌✅ (primary) -2.0% [-9.1%, 5.5%] 3

Cycles

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

Binary size

Results (primary 0.1%)

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.1% [0.0%, 0.2%] 10
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.1%, -0.0%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.1% [-0.1%, 0.2%] 12

Bootstrap: 776.409s -> 774.964s (-0.19%)
Artifact size: 365.11 MiB -> 365.16 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 18, 2025
@thaliaarchi
Copy link
Contributor Author

This is a runtime thing, so I'm not surprised we're not seeing any change in the compile time benchmarks. I think that means we're good.

@ibraheemdev
Copy link
Member

The benchmarks measure the run time of the compiler. It seems unlikely to use io::Write with a constant string, but given that we already do this in fmt::Write, it seems reasonable. @bors r+ rollup

@bors
Copy link
Contributor

bors commented Mar 19, 2025

📌 Commit a0c3dd4 has been approved by ibraheemdev

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 19, 2025
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Mar 20, 2025
…ibraheemdev

Optimize `io::Write::write_fmt` for constant strings

When the formatting args to `fmt::Write::write_fmt` are a statically known string, it simplifies to only calling `write_str` without a runtime branch. Do the same in `io::Write::write_fmt` with `write_all`.

Also, match the convention of `fmt::Write` for the name of `args`.
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 20, 2025
Rollup of 15 pull requests

Successful merges:

 - rust-lang#138321 ([bootstrap] Distribute split debuginfo if present)
 - rust-lang#138364 (ports the compiler test cases to new rust_intrinsic format)
 - rust-lang#138410 (Couple mir building cleanups)
 - rust-lang#138435 (Add support for postfix yield expressions)
 - rust-lang#138536 (stable_mir: Add `MutMirVisitor`)
 - rust-lang#138623 ([bootstrap] Use llvm_runtimes for compiler-rt)
 - rust-lang#138650 (Optimize `io::Write::write_fmt` for constant strings)
 - rust-lang#138652 (Reintroduce remote-test support in run-make tests)
 - rust-lang#138685 (Use `Option<Ident>` for lowered param names.)
 - rust-lang#138694 (Fix: add ohos target notes)
 - rust-lang#138700 (Suggest `-Whelp` when pass `--print lints` to rustc)
 - rust-lang#138709 (Update GCC submodule)
 - rust-lang#138724 (Check attrs: Don't try to retrieve the name of list stems)
 - rust-lang#138731 (coverage: Add LLVM plumbing for expansion regions)
 - rust-lang#138732 (Use `def_path_str` for def id arg in `UnsupportedOpInfo`)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 21, 2025
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#137357 (Document results of non-positive logarithms)
 - rust-lang#138650 (Optimize `io::Write::write_fmt` for constant strings)
 - rust-lang#138694 (Fix: add ohos target notes)
 - rust-lang#138713 (interpret memory access hooks: also pass through the Pointer used for the access)
 - rust-lang#138724 (Check attrs: Don't try to retrieve the name of list stems)
 - rust-lang#138743 (bootstrap: add `--ci` flag)
 - rust-lang#138751 (Fix the "used_with_archive" test on Fuchsia)
 - rust-lang#138754 (Handle spans of `~const`, `const`  and `async` trait bounds in macro expansion)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 809378b into rust-lang:master Mar 21, 2025
7 checks passed
@rustbot rustbot added this to the 1.87.0 milestone Mar 21, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 21, 2025
Rollup merge of rust-lang#138650 - thaliaarchi:io-write-fmt-known, r=ibraheemdev

Optimize `io::Write::write_fmt` for constant strings

When the formatting args to `fmt::Write::write_fmt` are a statically known string, it simplifies to only calling `write_str` without a runtime branch. Do the same in `io::Write::write_fmt` with `write_all`.

Also, match the convention of `fmt::Write` for the name of `args`.
@thaliaarchi thaliaarchi deleted the io-write-fmt-known branch March 21, 2025 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants