Skip to content

Mono item collection microoptimizations - #162480

Closed
Kobzol wants to merge 2 commits into
rust-lang:mainfrom
Kobzol:mono-collect-3
Closed

Mono item collection microoptimizations#162480
Kobzol wants to merge 2 commits into
rust-lang:mainfrom
Kobzol:mono-collect-3

Conversation

@Kobzol

@Kobzol Kobzol commented Sep 8, 2026

Copy link
Copy Markdown
Member

Found this while staring at the mono item collection code. I kinda doubt it will even show up on our benchmarks, but it seems like a reasonable improvement nonetheless.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 8, 2026
@Kobzol

Kobzol commented Sep 8, 2026

Copy link
Copy Markdown
Member 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 Sep 8, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 8, 2026
Mono item collection microoptimizations
@rust-bors

rust-bors Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: bfad934 (bfad9347ddd9cacf4405e7aeb492433d97128891)
Base parent: 745de6e (745de6eca673de5329ec68f2689629a5ca45ab35)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (bfad934): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

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.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@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.3% [0.2%, 0.4%] 6
Improvements ✅
(primary)
-0.3% [-0.4%, -0.2%] 3
Improvements ✅
(secondary)
-0.3% [-0.3%, -0.3%] 1
All ❌✅ (primary) -0.3% [-0.4%, -0.2%] 3

Max RSS (memory usage)

Results (primary 0.5%)

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

mean range count
Regressions ❌
(primary)
0.5% [0.4%, 0.7%] 7
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.5% [0.4%, 0.7%] 7

Cycles

Results (primary 0.2%, secondary -3.4%)

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

mean range count
Regressions ❌
(primary)
1.2% [0.4%, 2.6%] 6
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.8% [-1.3%, -0.5%] 7
Improvements ✅
(secondary)
-3.4% [-3.5%, -3.2%] 3
All ❌✅ (primary) 0.2% [-1.3%, 2.6%] 13

Binary size

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

Bootstrap: 478.441s -> 477.072s (-0.29%)
Artifact size: 403.53 MiB -> 403.64 MiB (0.03%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Sep 8, 2026
@Kobzol

Kobzol commented Sep 8, 2026

Copy link
Copy Markdown
Member Author

If you open the non-relevant results, it is very slightly green across the board, but the effect is very small. Anyway, I'll let the reviewer judge whether this is worth it or not :)

@Kobzol
Kobzol marked this pull request as ready for review September 8, 2026 17:28
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 8, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Sep 8, 2026
@rustbot

rustbot commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

r? @petrochenkov

rustbot has assigned @petrochenkov.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 76 candidates
  • Random selection from 20 candidates

def_path_str,
});
};
used_items.reserve(used.len());

@JonathanBrouwer JonathanBrouwer Sep 8, 2026

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.

Instead of reserving outside of the extend call which is easily forgotten, how about this implementation:
#162495

View changes since the review

// from multiple root items within a CGU, which is fine, it just means
// the `insert` will be a no-op.
for inlined_item in reachable_inlined_items {
for inlined_item in &reachable_inlined_items {

@JonathanBrouwer JonathanBrouwer Sep 8, 2026

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.

An alternative which might be slightly nicer as it doesn't need a copy:
reachable_inlined_items.drain(..)
and remove the .clear() above

The current implementation is also fine by me tho, if you prefer it. I think both of our options should be identical for performance

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think that iter + clear is generally faster than drain, because the implementation of drain tends to be complicated.

@Kobzol

Kobzol commented Sep 8, 2026

Copy link
Copy Markdown
Member Author

Closing in favor of #162495.

@Kobzol Kobzol closed this Sep 8, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 8, 2026
@Kobzol
Kobzol deleted the mono-collect-3 branch September 8, 2026 21:19
@JonathanBrouwer

JonathanBrouwer commented Sep 8, 2026

Copy link
Copy Markdown
Member

That pr only replaces one of the two commits from this PR tho

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 8, 2026
…d, r=Kobzol

Reserve items in `Extend` implementations

This might be a perf win, inspired by @Kobzol's approach in this PR: rust-lang#162480
@Kobzol

Kobzol commented Sep 8, 2026

Copy link
Copy Markdown
Member Author

Yeah, but the perf. effect was essentially zero, so I didn't consider it to be worth it.

rust-bors Bot pushed a commit that referenced this pull request Sep 9, 2026
Rollup merge of #162495 - JonathanBrouwer:reserve-mono-extend, r=Kobzol

Reserve items in `Extend` implementations

This might be a perf win, inspired by @Kobzol's approach in this PR: #162480
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf-regression Performance regression. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants