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

Refactor fmt::Display impls in rustdoc #135494

Merged
merged 3 commits into from
Jan 23, 2025

Conversation

yotamofek
Copy link
Contributor

@yotamofek yotamofek commented Jan 14, 2025

This PR does a couple of things, with the intention of cleaning up and streamlining some of the fmt::Display impls in rustdoc:

  1. Use the unstable fmt::from_fn instead of open-coding it.
  2. Replace bespoke implementations of Itertools::format with the method itself.
  3. Some more minor cleanups - DRY, remove unnecessary calls to Symbol::as_str(), replace some format!() calls with lazier options

The changes are mostly cosmetic but some of them might have a slight positive effect on performance.

@rustbot
Copy link
Collaborator

rustbot commented Jan 14, 2025

r? @fmease

rustbot has assigned @fmease.
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-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Jan 14, 2025
@yotamofek yotamofek changed the title Rustdoc fmt from fn Refactor fmt::Display impls in rustdoc Jan 14, 2025
@@ -389,98 +390,84 @@ fn write_with_opt_paren<T: fmt::Display>(
Ok(())
}

impl Display<'_> {
fn display_quantified_sub_cfgs(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I really don't think this is a great name for this function, but couldn't come up with a better one 😅

Copy link
Member

@fmease fmease Jan 22, 2025

Choose a reason for hiding this comment

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

I would just go for display_sub_cfgs since the only constructs (of the cfg sublanguage) that may have sub-cfgs are all and any.

I guess “quantified” is not wrong since it's true that the sub cfgs are quantified by either $\forall$ or $\exists$ over a finite domain $\mathbb{F}_n$ — aka n-ary conjunction ($\bigwedge_{i=0}^n$) and n-ary disjunction ($\bigvee_{i=0}^n$), respectively. Anyway ^^

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed. I just looked up "common term for ALL and ANY" or something haha. Math is not my strong suit, so thanks for the thorough explanation, I appreciate it 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was considering making a Quantifier enum with Any or All variants to make this function a bit easier to understand, but not sure if it's not too verbose. Leaving at is as is for now, unless you think it's a good idea.

@aDotInTheVoid
Copy link
Member

@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 Jan 20, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 20, 2025
Refactor `fmt::Display` impls in rustdoc

This PR does a couple of things, with the intention of cleaning up and streamlining some of the `fmt::Display` impls in rustdoc:
1. Use the unstable [`fmt::from_fn`](rust-lang#117729) instead of open-coding it.
2. Replace bespoke implementations of `Itertools::format` with the method itself.
3. Some more minor cleanups - DRY, remove unnecessary calls to `Symbol::as_str()`, replace some `format!()` calls with lazier options

The changes are mostly cosmetic but some of them might have a slight positive effect on performance.
@bors
Copy link
Contributor

bors commented Jan 20, 2025

⌛ Trying commit 16e9fe6 with merge 6b19da6...

@bors
Copy link
Contributor

bors commented Jan 21, 2025

☀️ Try build successful - checks-actions
Build commit: 6b19da6 (6b19da6ca4f7739aa3366e3d606c076de97524c3)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (6b19da6): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

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.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

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

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
0.9% [0.4%, 1.5%] 4
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.0% [-1.0%, -1.0%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.5% [-1.0%, 1.5%] 5

Max RSS (memory usage)

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

Cycles

Results (secondary 9.9%)

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)
9.9% [9.4%, 10.5%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 765.975s -> 765.445s (-0.07%)
Artifact size: 326.02 MiB -> 325.98 MiB (-0.01%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jan 21, 2025
@yotamofek
Copy link
Contributor Author

@aDotInTheVoid should I do something about the perf results? AFAIR rustc-perf doesn't actually benchmark rustdoc, so I would guess these changes must be noise?

@fmease
Copy link
Member

fmease commented Jan 21, 2025

@yotamofek It does benchmark rustdoc. There are 4 profile=doc (aka rustdoc) regressions, the check one is spurious.

@yotamofek
Copy link
Contributor Author

Oh, ok, I see it now. So it's definitely a real regression.
That sucks, I think using Itertools::format is much nicer than re-implementing it so many times, but it uses Cell under the hood (because Display takes an immutable ref to self) so that might be the source of the regression.
Will look into it.
Thanks!

@yotamofek yotamofek force-pushed the rustdoc-fmt-from_fn branch from 16e9fe6 to 37c30a0 Compare January 21, 2025 20:09
@yotamofek
Copy link
Contributor Author

I removed the commit with the usages of Itertools::format, would love another perf run to eliminate the other commits from being the culprits for the regression. @aDotInTheVoid or @fmease

@aDotInTheVoid
Copy link
Member

@bors try @rust-timer queue

bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 21, 2025
Refactor `fmt::Display` impls in rustdoc

This PR does a couple of things, with the intention of cleaning up and streamlining some of the `fmt::Display` impls in rustdoc:
1. Use the unstable [`fmt::from_fn`](rust-lang#117729) instead of open-coding it.
2. Replace bespoke implementations of `Itertools::format` with the method itself.
3. Some more minor cleanups - DRY, remove unnecessary calls to `Symbol::as_str()`, replace some `format!()` calls with lazier options

The changes are mostly cosmetic but some of them might have a slight positive effect on performance.
@bors
Copy link
Contributor

bors commented Jan 21, 2025

⌛ Trying commit 37c30a0 with merge 109d05c...

@bors
Copy link
Contributor

bors commented Jan 21, 2025

☀️ Try build successful - checks-actions
Build commit: 109d05c (109d05ce649ea6dde19695dbd197cbf63bb11520)

@fmease
Copy link
Member

fmease commented Jan 22, 2025

@rust-timer ping

@fmease
Copy link
Member

fmease commented Jan 22, 2025

@rust-timer build 109d05c

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (109d05c): 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 the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

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

Max RSS (memory usage)

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

Cycles

Results (secondary 2.0%)

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)
2.0% [2.0%, 2.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 764.328s -> 765.066s (0.10%)
Artifact size: 326.09 MiB -> 326.05 MiB (-0.01%)

@rustbot rustbot removed the perf-regression Performance regression. label Jan 22, 2025
@yotamofek yotamofek force-pushed the rustdoc-fmt-from_fn branch from 37c30a0 to 57de8b7 Compare January 22, 2025 05:16
@yotamofek
Copy link
Contributor Author

yotamofek commented Jan 22, 2025

The first commit likely doesn't have an effect on perf / runtime behavior because the compiler should be smart enough to recognize that it's better to pass the large parameter by reference. However, it's still nicer to make it explicit.

Actually, that was needed to make it easier to adopt fmt::from_fn (which takes a Fn and not an FnOnce, like display_fn did, so requires Copy-able captures for its closures)

@rust-log-analyzer

This comment has been minimized.

@yotamofek yotamofek force-pushed the rustdoc-fmt-from_fn branch from 57de8b7 to 48b5481 Compare January 22, 2025 05:31
@fmease
Copy link
Member

fmease commented Jan 22, 2025

The first commit likely doesn't have an effect on perf / runtime behavior because the compiler should be smart enough to recognize that it's better to pass the large parameter by reference. However, it's still nicer to make it explicit.

Actually, that was needed to make it easier to adopt fmt::from_fn (which takes a Fn and not an FnOnce, like display_fn did, so requires Copy-able captures for its closures)

Ah, I see. Thanks for the correction, that makes perfect sense!

@bors r+

@bors
Copy link
Contributor

bors commented Jan 22, 2025

📌 Commit 48b5481 has been approved by fmease

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 22, 2025
@bors
Copy link
Contributor

bors commented Jan 23, 2025

⌛ Testing commit 48b5481 with merge fc0094f...

@bors
Copy link
Contributor

bors commented Jan 23, 2025

☀️ Test successful - checks-actions
Approved by: fmease
Pushing fc0094f to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jan 23, 2025
@bors bors merged commit fc0094f into rust-lang:master Jan 23, 2025
7 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 23, 2025
@yotamofek yotamofek deleted the rustdoc-fmt-from_fn branch January 23, 2025 12:38
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (fc0094f): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

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

Max RSS (memory usage)

Results (secondary 1.6%)

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)
1.6% [0.5%, 3.6%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Cycles

Results (primary -2.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)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.2% [-2.2%, -2.2%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.2% [-2.2%, -2.2%] 1

Binary size

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

Bootstrap: 776.757s -> 778.767s (0.26%)
Artifact size: 325.99 MiB -> 325.93 MiB (-0.02%)

jhpratt added a commit to jhpratt/rust that referenced this pull request Jan 26, 2025
…rrowLii

Use `fmt::from_fn` in more places in the compiler

Use the unstable functions from rust-lang#117729 in more places in the compiler, follow up to rust-lang#135494
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jan 26, 2025
Rollup merge of rust-lang#135951 - yotamofek:use-debug-helpers, r=SparrowLii

Use `fmt::from_fn` in more places in the compiler

Use the unstable functions from rust-lang#117729 in more places in the compiler, follow up to rust-lang#135494
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 29, 2025
[WIP: perf] attempt to prevent regressions in that originally happened in rust-lang#135494

This implements something similar to [`Itertools::format`](https://docs.rs/itertools/latest/itertools/trait.Itertools.html#method.format), but on `Fn`s returning iterators instead of directly on iterators, to allow implementing `Display` without the use of a `Cell` (to handle the possibility of `fmt` being called multiple times while receiving `&self`).

This is WIP, I just want to get a perf run first to see if the regression I saw in rust-lang#135494 is fixed
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 29, 2025
[WIP: perf] attempt to prevent regressions in that originally happened in rust-lang#135494

This implements something similar to [`Itertools::format`](https://docs.rs/itertools/latest/itertools/trait.Itertools.html#method.format), but on `Fn`s returning iterators instead of directly on iterators, to allow implementing `Display` without the use of a `Cell` (to handle the possibility of `fmt` being called multiple times while receiving `&self`).

This is WIP, I just want to get a perf run first to see if the regression I saw in rust-lang#135494 is fixed
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 1, 2025
[WIP: perf] attempt to prevent regressions in that originally happened in rust-lang#135494

This implements something similar to [`Itertools::format`](https://docs.rs/itertools/latest/itertools/trait.Itertools.html#method.format), but on `Fn`s returning iterators instead of directly on iterators, to allow implementing `Display` without the use of a `Cell` (to handle the possibility of `fmt` being called multiple times while receiving `&self`).

This is WIP, I just want to get a perf run first to see if the regression I saw in rust-lang#135494 is fixed
jhpratt added a commit to jhpratt/rust that referenced this pull request Feb 4, 2025
… r=dtolnay

Mark `std::fmt::from_fn` as `#[must_use]`

While working on rust-lang#135494 I managed to shoot my own foot a few times by forgetting to actually use the result of `fmt::from_fn`, so I think a `#[must_use]` could be appropriate!

Didn't have a good message to put in the attr so left it blank, still unstable so we can come back to it I guess?

cc rust-lang#117729 (and a huge +1 for getting it stabilized, it's very useful IMHO)
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 5, 2025
…illaumeGomez

librustdoc: create a helper for separating elements of an iterator instead of implementing it multiple times

This implements something similar to [`Itertools::format`](https://docs.rs/itertools/latest/itertools/trait.Itertools.html#method.format), but on `Fn`s returning iterators instead of directly on iterators, to allow implementing `Display` without the use of a `Cell` (to handle the possibility of `fmt` being called multiple times while receiving `&self`).

~This is WIP, I just want to get a perf run first to see if the regression I saw in rust-lang#135494 is fixed~

This was originally part of rust-lang#135494 , but originally caused a perf regression that was since fixed:
https://github.com/rust-lang/rust/blob/7d5ae1863aa66847a4edf8d2ef9420717df65c5d/src/librustdoc/html/format.rs#L507
fmease added a commit to fmease/rust that referenced this pull request Feb 5, 2025
… r=dtolnay

Mark `std::fmt::from_fn` as `#[must_use]`

While working on rust-lang#135494 I managed to shoot my own foot a few times by forgetting to actually use the result of `fmt::from_fn`, so I think a `#[must_use]` could be appropriate!

Didn't have a good message to put in the attr so left it blank, still unstable so we can come back to it I guess?

cc rust-lang#117729 (and a huge +1 for getting it stabilized, it's very useful IMHO)
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 5, 2025
Rollup merge of rust-lang#136502 - yotamofek:pr/fmt-from-fn-must-use, r=dtolnay

Mark `std::fmt::from_fn` as `#[must_use]`

While working on rust-lang#135494 I managed to shoot my own foot a few times by forgetting to actually use the result of `fmt::from_fn`, so I think a `#[must_use]` could be appropriate!

Didn't have a good message to put in the attr so left it blank, still unstable so we can come back to it I guess?

cc rust-lang#117729 (and a huge +1 for getting it stabilized, it's very useful IMHO)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

7 participants