Skip to content

rustdoc: optimize impl sorting#157179

Merged
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
nnethercote:compare_names
May 31, 2026
Merged

rustdoc: optimize impl sorting#157179
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
nnethercote:compare_names

Conversation

@nnethercote

Copy link
Copy Markdown
Contributor

Currently rustdoc sorts impls in a couple of places using long HTML string representations of the impls. Using plain text representations instead speeds things up. Details in individual commits.

Currently impls printed in alternate mode use plain text for the first
part and HTML for the where clause. This commit fixes the where clause
so it's also plain text.
These sorts are hot, so it's significantly faster to use concise plain
text rather than verbose HTML.
We don't need to because the char ordering works out naturally.
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels May 31, 2026
@nnethercote

Copy link
Copy Markdown
Contributor Author

LLM disclosure: I wrote the three commits by hand after viewing profiling data. I used an LLM to review the commits before submitting. The LLM identified that ImplString::is_negative was no longer used, so I removed that field by hand.

@nnethercote

Copy link
Copy Markdown
Contributor Author

@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 May 31, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request May 31, 2026
@rust-bors

rust-bors Bot commented May 31, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: e444e2a (e444e2a215db55a372a3012da35a7c1b15bc6b4d, parent: e7a21fa2562f67cf9cac407c29287a16862b4463)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (e444e2a): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

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

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.5% [-6.7%, -0.4%] 13
Improvements ✅
(secondary)
-1.1% [-1.9%, -0.3%] 2
All ❌✅ (primary) -1.5% [-6.7%, -0.4%] 13

Max RSS (memory usage)

Results (secondary -3.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

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

Cycles

Results (primary -2.2%, secondary -4.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

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

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 513.315s -> 511.874s (-0.28%)
Artifact size: 400.75 MiB -> 400.75 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label May 31, 2026
@Kobzol

Kobzol commented May 31, 2026

Copy link
Copy Markdown
Member

Nice wins! Have you tried replacing compare_names with just <, to see what is its effect on perf.?

Btw I noticed at https://doc.rust-lang.org/std/cell/struct.Cell.html#trait-implementations, then in the bar on the left, !Sync is sorted as the first item, but in the actual list of trait impls in the middle, it is shown as last. I wonder if that is an existing bug in the sorting logic, or if it is supposed to be like that.

@nnethercote

Copy link
Copy Markdown
Contributor Author

Nice wins! Have you tried replacing compare_names with just <, to see what is its effect on perf.?

Yes... this approach gets about 90-95% of the improvement of replacing compare_names with Ord::cmp.

Btw I noticed at https://doc.rust-lang.org/std/cell/struct.Cell.html#trait-implementations, then in the bar on the left, !Sync is sorted as the first item, but in the actual list of trait impls in the middle, it is shown as last. I wonder if that is an existing bug in the sorting logic, or if it is supposed to be like that.

Good question, I'm not sure. CoerceUnsize and DispatchFromDyn are also in different places; those two both have impl<T, U> instead of impl<T>, which might be a factor? And the AsRef impls are also in different orders in the two places. In general the order in the left sidebar seems better.

I just checked with my local builds. The orders are unchanged by this PR, so I think I've successfully preserved the existing behaviour. Which seems reasonable for an optimization PR. Any bugs should probably be fixed in a separate PR.

@nnethercote

Copy link
Copy Markdown
Contributor Author

r? @GuillaumeGomez

@nnethercote nnethercote marked this pull request as ready for review May 31, 2026 10:24
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 31, 2026
@GuillaumeGomez

Copy link
Copy Markdown
Member

Looks great, thanks!

@bors r+ rollup

@rust-bors

rust-bors Bot commented May 31, 2026

Copy link
Copy Markdown
Contributor

📌 Commit f7c8bc2 has been approved by GuillaumeGomez

It is now in the queue for this repository.

@rust-bors rust-bors Bot 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 May 31, 2026
@GuillaumeGomez

Copy link
Copy Markdown
Member

Woups, perf sensitive.

@bors rollup=never

@rust-bors

This comment has been minimized.

@Kobzol

Kobzol commented May 31, 2026

Copy link
Copy Markdown
Member

Nice wins! Have you tried replacing compare_names with just <, to see what is its effect on perf.?

Yes... this approach gets about 90-95% of the improvement of replacing compare_names with Ord::cmp.

I meant combining this PR with replacing compare_names with just cmp.

@rust-bors rust-bors Bot added merged-by-bors This PR was explicitly merged by bors. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels May 31, 2026
@rust-bors

rust-bors Bot commented May 31, 2026

Copy link
Copy Markdown
Contributor

☀️ Test successful - CI
Approved by: GuillaumeGomez
Duration: 3h 15m 1s
Pushing 1ce45a0 to main...

@rust-bors rust-bors Bot merged commit 1ce45a0 into rust-lang:main May 31, 2026
14 checks passed
@rustbot rustbot added this to the 1.98.0 milestone May 31, 2026
@github-actions

Copy link
Copy Markdown
Contributor
What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing 9e293ae (parent) -> 1ce45a0 (this PR)

Test differences

Show 4 test diffs

4 doctest diffs were found. These are ignored, as they are noisy.

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard 1ce45a011a3fe99ab08643d8eb7229bfc3b40bda --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. x86_64-msvc-ext2: 2h 2m -> 1h 28m (-27.8%)
  2. dist-powerpc-linux: 1h 15m -> 1h 31m (+21.6%)
  3. x86_64-msvc-ext3: 1h 44m -> 1h 24m (-19.5%)
  4. dist-s390x-linux: 1h 30m -> 1h 12m (-19.4%)
  5. dist-x86_64-solaris: 1h 21m -> 1h 35m (+17.9%)
  6. x86_64-gnu-tools: 55m 8s -> 45m 41s (-17.2%)
  7. x86_64-gnu-llvm-21-2: 1h 26m -> 1h 39m (+15.2%)
  8. armhf-gnu: 1h 18m -> 1h 27m (+11.3%)
  9. dist-apple-various: 1h 39m -> 1h 49m (+11.0%)
  10. pr-check-1: 25m 14s -> 27m 59s (+10.9%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (1ce45a0): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.5% [-6.7%, -0.3%] 13
Improvements ✅
(secondary)
-1.1% [-1.9%, -0.4%] 2
All ❌✅ (primary) -1.5% [-6.7%, -0.3%] 13

Max RSS (memory usage)

Results (primary -3.2%, secondary -5.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-3.2% [-4.7%, -1.3%] 3
Improvements ✅
(secondary)
-5.8% [-5.8%, -5.8%] 1
All ❌✅ (primary) -3.2% [-4.7%, -1.3%] 3

Cycles

This perf run didn't have relevant results for this metric.

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 508.947s -> 513.507s (0.90%)
Artifact size: 400.66 MiB -> 400.69 MiB (0.01%)

@nnethercote

Copy link
Copy Markdown
Contributor Author

Btw I noticed at https://doc.rust-lang.org/std/cell/struct.Cell.html#trait-implementations, then in the bar on the left, !Sync is sorted as the first item, but in the actual list of trait impls in the middle, it is shown as last.

I have a fix for this in #157233.

@nnethercote nnethercote deleted the compare_names branch June 3, 2026 00:27
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 7, 2026
…pls-cleanup, r=GuillaumeGomez

Cleanup and optimize `render_impls`

- take ownership of the `Vec<&Impl>` instead of copying into another alloc
- reuse `ImplString` to do natural sort ordering
- lazy formatting

Somewhat of a follow-up to rust-lang#157233 and rust-lang#157179 (cc @nnethercote - thanks!)
This kinda undoes rust-lang@f7c8bc2 but IMHO it makes more sense to be explicit about negative impl ordering, and also seems kinda wasteful to "render" the negativity into a string and rely on however ASCII decided to order characters. I can also undo this part, I think this PR is still a positive change even without it.

r? @GuillaumeGomez

LLM disclosure:
I used LLM for reviewing my changes and making sure some assumptions I was making were correct (i.e. that `render_impls` will not render anything IFF the list of traits passed to it is empty)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 7, 2026
…pls-cleanup, r=GuillaumeGomez

Cleanup and optimize `render_impls`

- take ownership of the `Vec<&Impl>` instead of copying into another alloc
- reuse `ImplString` to do natural sort ordering
- lazy formatting

Somewhat of a follow-up to rust-lang#157233 and rust-lang#157179 (cc @nnethercote - thanks!)
This kinda undoes rust-lang@f7c8bc2 but IMHO it makes more sense to be explicit about negative impl ordering, and also seems kinda wasteful to "render" the negativity into a string and rely on however ASCII decided to order characters. I can also undo this part, I think this PR is still a positive change even without it.

r? @GuillaumeGomez

LLM disclosure:
I used LLM for reviewing my changes and making sure some assumptions I was making were correct (i.e. that `render_impls` will not render anything IFF the list of traits passed to it is empty)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 7, 2026
…pls-cleanup, r=GuillaumeGomez

Cleanup and optimize `render_impls`

- take ownership of the `Vec<&Impl>` instead of copying into another alloc
- reuse `ImplString` to do natural sort ordering
- lazy formatting

Somewhat of a follow-up to rust-lang#157233 and rust-lang#157179 (cc @nnethercote - thanks!)
This kinda undoes rust-lang@f7c8bc2 but IMHO it makes more sense to be explicit about negative impl ordering, and also seems kinda wasteful to "render" the negativity into a string and rely on however ASCII decided to order characters. I can also undo this part, I think this PR is still a positive change even without it.

r? @GuillaumeGomez

LLM disclosure:
I used LLM for reviewing my changes and making sure some assumptions I was making were correct (i.e. that `render_impls` will not render anything IFF the list of traits passed to it is empty)
jhpratt added a commit to jhpratt/rust that referenced this pull request Jun 7, 2026
…pls-cleanup, r=GuillaumeGomez

Cleanup and optimize `render_impls`

- take ownership of the `Vec<&Impl>` instead of copying into another alloc
- reuse `ImplString` to do natural sort ordering
- lazy formatting

Somewhat of a follow-up to rust-lang#157233 and rust-lang#157179 (cc @nnethercote - thanks!)
This kinda undoes rust-lang@f7c8bc2 but IMHO it makes more sense to be explicit about negative impl ordering, and also seems kinda wasteful to "render" the negativity into a string and rely on however ASCII decided to order characters. I can also undo this part, I think this PR is still a positive change even without it.

r? @GuillaumeGomez

LLM disclosure:
I used LLM for reviewing my changes and making sure some assumptions I was making were correct (i.e. that `render_impls` will not render anything IFF the list of traits passed to it is empty)
jhpratt added a commit to jhpratt/rust that referenced this pull request Jun 7, 2026
…pls-cleanup, r=GuillaumeGomez

Cleanup and optimize `render_impls`

- take ownership of the `Vec<&Impl>` instead of copying into another alloc
- reuse `ImplString` to do natural sort ordering
- lazy formatting

Somewhat of a follow-up to rust-lang#157233 and rust-lang#157179 (cc @nnethercote - thanks!)
This kinda undoes rust-lang@f7c8bc2 but IMHO it makes more sense to be explicit about negative impl ordering, and also seems kinda wasteful to "render" the negativity into a string and rely on however ASCII decided to order characters. I can also undo this part, I think this PR is still a positive change even without it.

r? @GuillaumeGomez

LLM disclosure:
I used LLM for reviewing my changes and making sure some assumptions I was making were correct (i.e. that `render_impls` will not render anything IFF the list of traits passed to it is empty)
rust-timer added a commit that referenced this pull request Jun 8, 2026
Rollup merge of #157540 - yotamofek:pr/rustdoc/render_all_impls-cleanup, r=GuillaumeGomez

Cleanup and optimize `render_impls`

- take ownership of the `Vec<&Impl>` instead of copying into another alloc
- reuse `ImplString` to do natural sort ordering
- lazy formatting

Somewhat of a follow-up to #157233 and #157179 (cc @nnethercote - thanks!)
This kinda undoes f7c8bc2 but IMHO it makes more sense to be explicit about negative impl ordering, and also seems kinda wasteful to "render" the negativity into a string and rely on however ASCII decided to order characters. I can also undo this part, I think this PR is still a positive change even without it.

r? @GuillaumeGomez

LLM disclosure:
I used LLM for reviewing my changes and making sure some assumptions I was making were correct (i.e. that `render_impls` will not render anything IFF the list of traits passed to it is empty)
github-actions Bot pushed a commit to rust-lang/stdarch that referenced this pull request Jun 8, 2026
…up, r=GuillaumeGomez

Cleanup and optimize `render_impls`

- take ownership of the `Vec<&Impl>` instead of copying into another alloc
- reuse `ImplString` to do natural sort ordering
- lazy formatting

Somewhat of a follow-up to rust-lang/rust#157233 and rust-lang/rust#157179 (cc @nnethercote - thanks!)
This kinda undoes rust-lang/rust@f7c8bc2 but IMHO it makes more sense to be explicit about negative impl ordering, and also seems kinda wasteful to "render" the negativity into a string and rely on however ASCII decided to order characters. I can also undo this part, I think this PR is still a positive change even without it.

r? @GuillaumeGomez

LLM disclosure:
I used LLM for reviewing my changes and making sure some assumptions I was making were correct (i.e. that `render_impls` will not render anything IFF the list of traits passed to it is empty)
asukaminato0721 pushed a commit to asukaminato0721/rust-analyzer that referenced this pull request Jun 8, 2026
…up, r=GuillaumeGomez

Cleanup and optimize `render_impls`

- take ownership of the `Vec<&Impl>` instead of copying into another alloc
- reuse `ImplString` to do natural sort ordering
- lazy formatting

Somewhat of a follow-up to rust-lang/rust#157233 and rust-lang/rust#157179 (cc @nnethercote - thanks!)
This kinda undoes rust-lang/rust@f7c8bc2 but IMHO it makes more sense to be explicit about negative impl ordering, and also seems kinda wasteful to "render" the negativity into a string and rely on however ASCII decided to order characters. I can also undo this part, I think this PR is still a positive change even without it.

r? @GuillaumeGomez

LLM disclosure:
I used LLM for reviewing my changes and making sure some assumptions I was making were correct (i.e. that `render_impls` will not render anything IFF the list of traits passed to it is empty)
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. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants