Skip to content

Conversation

@osiewicz
Copy link
Contributor

@osiewicz osiewicz commented Nov 25, 2025

Upstream monomorphisations are a blessing and a curse of Zed's build performance.

On one hand, we do benefit from it, as builds with share-generics disabled are slower for us (plus we don't really want to use nightly anyways).
On the other, deserializing query results takes a lot of time. For some crates close to the end of our compilation pipeline, it's over 400ms per crate.

To make matters worse, I've measured a hit ratio of upstream generics. A sample of such measurement goes as follows:

upstream_monomorphization returned None for 28501 distinct monomorphizations.
upstream_monomorphization returned Some 2518 times. Results came from 163 distinct CrateNums.
In total, there were 619731 instantiations to chose from (in upstream_monomorphizations() map))

This is horrid for us, as we're using a very small percentage of the map that we spend so much time deserializing from.

This commit tries to (rather clumsily) move us towards a sparse representation of upstream_monomorphizations. Instead of storing <DefId, (GenericArgsRef<'_>)> which is rather heavy to deserialize, we'll resort to storing Hashes of Instances. I plan to make this more foolproof, hence this commit is marked as WIP.
For one, we should probably keep the projection queries. Also, it might be worthwhile to store index pointing at entry within exported_generics of target crate in order to remedy a potential for collisions.

This commit reduces a touch crates/editor/src/editor.rs scenario in Zed for me from 14.5s to 11s.

@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 Nov 25, 2025
@bjorn3
Copy link
Member

bjorn3 commented Nov 25, 2025

On the other, deserializing query results takes a lot of time. For some crates close to the end of our compilation pipeline, it's over 400ms per crate.

That is a lot. I wonder if odht could help for lazy deserialization of the DefIdMap. I don't know how easy it would be to handle with respect to deserialization and borrowing from the raw crate metadata bytes from a query though.

@osiewicz
Copy link
Contributor Author

That's worth a try I guess.
It seems to only support encoding data with fixed size, which would not work with GenericArgsRef I think?

@bjorn3
Copy link
Member

bjorn3 commented Nov 25, 2025

Looks like it indeed. And LazyTable doesn't allow variable sized values either..

@Kobzol
Copy link
Member

Kobzol commented Nov 25, 2025

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Nov 25, 2025
…mean, r=<try>

rustc_codegen_ssa: Make upstream monomorphizations representation sparse
@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 25, 2025
@rust-bors
Copy link
Contributor

rust-bors bot commented Nov 25, 2025

☀️ Try build successful (CI)
Build commit: 4fcb6e6 (4fcb6e64fedb642e69a609c131cb6bd21443a776, parent: 7934bbdf84a6b9a30297caf4f4f38286dedf876a)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (4fcb6e6): comparison URL.

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

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@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.2% [0.1%, 0.4%] 3
Regressions ❌
(secondary)
0.4% [0.3%, 0.5%] 4
Improvements ✅
(primary)
-0.6% [-1.8%, -0.2%] 11
Improvements ✅
(secondary)
-0.9% [-1.7%, -0.6%] 5
All ❌✅ (primary) -0.5% [-1.8%, 0.4%] 14

Max RSS (memory usage)

Results (primary -1.1%)

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

mean range count
Regressions ❌
(primary)
3.0% [3.0%, 3.0%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.6% [-2.4%, -0.8%] 7
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.1% [-2.4%, 3.0%] 8

Cycles

Results (primary 0.9%, 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)
4.0% [4.0%, 4.0%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.2% [-2.2%, -2.2%] 1
Improvements ✅
(secondary)
-3.8% [-7.5%, -2.4%] 4
All ❌✅ (primary) 0.9% [-2.2%, 4.0%] 2

Binary size

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

Bootstrap: 472.579s -> 470.697s (-0.40%)
Artifact size: 386.92 MiB -> 386.90 MiB (-0.00%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 25, 2025
@bors
Copy link
Collaborator

bors commented Dec 14, 2025

☔ The latest upstream changes (presumably #146348) made this pull request unmergeable. Please resolve the merge conflicts.

@rust-log-analyzer

This comment has been minimized.

@osiewicz osiewicz force-pushed the upstream-monomorphizations-lean-and-mean branch from 9e1740f to 4d2d113 Compare January 21, 2026 19:13
@rust-bors

This comment has been minimized.

@osiewicz osiewicz force-pushed the upstream-monomorphizations-lean-and-mean branch from 4d2d113 to 1b2e745 Compare February 10, 2026 01:13
@osiewicz osiewicz marked this pull request as ready for review February 10, 2026 01:14
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 10, 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 Feb 10, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 10, 2026

r? @JonathanBrouwer

rustbot has assigned @JonathanBrouwer.
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 21 candidates
  • Random selection from 13 candidates

@osiewicz
Copy link
Contributor Author

Fair disclosure: I have used Claude Opus to generate code submitted in this PR.

@rust-log-analyzer

This comment has been minimized.

Upstream monomorphisations are a blessing and a curse of Zed's build performance.

On one hand, we do benefit from it, as builds with share-generics disabled are slower for us (plus we don't really want to use nightly anyways).
On the other, deserializing query results takes *a lot* of time. For some crates close to the end of our compilation pipeline, it's over 400ms per crate.

To make matters worse, I've measured a hit ratio of upstream generics. A sample of such measurement goes as follows:
```
upstream_monomorphization returned None for 28501 distinct monomorphizations.
upstream_monomorphization returned Some 2518 times. Results came from 163 distinct CrateNums.
In total, there are 619731 instantiations
```
This is horrid for us, as we're using a very small percentage of the map that we spend so much time deserializing from.

This commit tries to (rather clumsily) move us towards a sparse representation of upstream_monomorphizations. Instead of storing <DefId, (GenericArgsRef<'_>)> which is rather heavy to deserialize, we'll resort to storing Hashes of Instances.

This commit reduces a `touch crates/editor/src/editor.rs` scenario in Zed for me from 14.5s to 11s.
@osiewicz osiewicz force-pushed the upstream-monomorphizations-lean-and-mean branch from 1b2e745 to 9c3a781 Compare February 10, 2026 01:20
@rust-log-analyzer
Copy link
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
fmt: checked 6697 files
tidy check
tidy [rustdoc_json (src)]: `rustdoc-json-types` modified, checking format version
tidy: Skipping binary file check, read-only filesystem
tidy [alphabetical (compiler)]: /checkout/compiler/rustc_metadata/src/rmeta/parameterized.rs:71: line not in alphabetical order (tip: use --bless to sort this list)
tidy [alphabetical (compiler)]: FAIL
removing old virtual environment
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10' and 'venv'
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10' and 'virtualenv'
Requirement already satisfied: pip in ./build/venv/lib/python3.10/site-packages (25.3)
Collecting pip
---
info: ES-Check: there were no ES version matching errors!  🎉
typechecking javascript files
tidy: The following check failed: alphabetical (compiler)
Bootstrap failed while executing `test src/tools/tidy tidyselftest --extra-checks=py,cpp,js,spellcheck`
Command `/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools-bin/rust-tidy --root-path=/checkout --cargo-path=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo --output-dir=/checkout/obj/build --concurrency=4 --npm-path=/node/bin/yarn --extra-checks=py,cpp,js,spellcheck` failed with exit code 1
Created at: src/bootstrap/src/core/build_steps/tool.rs:1612:23
Executed at: src/bootstrap/src/core/build_steps/test.rs:1365:29

Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:02:55
  local time: Tue Feb 10 01:27:09 UTC 2026
  network time: Tue, 10 Feb 2026 01:27:09 GMT
##[error]Process completed with exit code 1.
##[group]Run echo "disk usage:"

@osiewicz osiewicz marked this pull request as draft February 10, 2026 01:32
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf-regression Performance regression. 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants