Skip to content

Eliminate CrateMetadataRef.#155663

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
nnethercote:eliminate-CrateMetadataRef
Apr 24, 2026
Merged

Eliminate CrateMetadataRef.#155663
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
nnethercote:eliminate-CrateMetadataRef

Conversation

@nnethercote
Copy link
Copy Markdown
Contributor

@nnethercote nnethercote commented Apr 23, 2026

View all comments

There are a number of things I dislike about CrateMetadataRef.

  • It contains two fields cstore and cdata. The latter points to data within the former. It's like having an Elem type that has a reference to a vec element and also a reference to the vec itself. Weird.
  • The cdata field gets a lot of use, and the Deref impl just derefs that field. The cstore field is rarely used.
  • CrateMetadataRef is not a good name.
  • Variables named cdata sometimes refer to values of this type and sometimes to values of type CrateMetadata, which is confusing.

The good news is that CrateMetadataRef is not necessary and can be replaced with &CrateMetadata. Why? Everywhere that CrateMetadataRef is used, a TyCtxt is also present, and the CStore is accessible from the TyCtxt with CStore::from_tcx.

So this commit removes CrateMetadataRef and replaces all its uses with &CrateMetadata. Notes:

  • This requires adding only two uses of CStore::from_tcx, which shows how rarely the cstore field was used.
  • get_crate_data now matches get_crate_data_mut more closely.
  • A few variables are renamed for consistency, e.g. data/cmeta -> cdata.
  • An unnecessary local variable (local_cdata) in decode_expn_id is removed.
  • All the CrateMetadataRef methods become CrateMetadata methods, and their receiver changes from self to &self.
  • RawDefId::decode_from_cdata is inlined and removed, because it has a single call site.

r? @mejrs

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 23, 2026
@nnethercote
Copy link
Copy Markdown
Contributor Author

Locally this was a perf win, mostly on doc builds. Let's try on CI:

@bors try @rust-timer queue

cc @petrochenkov

@rust-timer

This comment has been minimized.

@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 Apr 23, 2026
rust-bors Bot pushed a commit that referenced this pull request Apr 23, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 23, 2026

☀️ Try build successful (CI)
Build commit: 073731e (073731e7481a7399dd3febf125eb5483b67d4642, parent: 913e4bea83424658d76712fee9c52452a3a9ef0e)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (073731e): 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.7% [0.3%, 1.2%] 2
Improvements ✅
(primary)
-0.6% [-1.3%, -0.3%] 14
Improvements ✅
(secondary)
-0.8% [-1.3%, -0.2%] 30
All ❌✅ (primary) -0.6% [-1.3%, -0.3%] 14

Max RSS (memory usage)

Results (primary 1.8%)

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

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

Cycles

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

Binary size

Results (secondary 0.0%)

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

Bootstrap: 492.161s -> 492.336s (0.04%)
Artifact size: 394.33 MiB -> 394.26 MiB (-0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 23, 2026
@nnethercote
Copy link
Copy Markdown
Contributor Author

Perf results are good, and match what I saw locally.

@petrochenkov petrochenkov self-assigned this Apr 23, 2026
@mejrs
Copy link
Copy Markdown
Contributor

mejrs commented Apr 23, 2026

This looks really nice, but I think petrochenkov has some thoughts :)

@mejrs mejrs removed their assignment Apr 23, 2026
@petrochenkov
Copy link
Copy Markdown
Contributor

Everywhere that CrateMetadataRef is used, a TyCtxt is also present, and the CStore is accessible from the TyCtxt with CStore::from_tcx

This wasn't the case until recently, only Session was passed around, not full tcx, because no other parts of tcx were used during decoding, that's why cstore had to be preserved in CrateMetadataRef.
Also if CStrore::from_tcx was used often, it could be a pessimization compared to passing cstore around, because it's going through a lock, but fortunately CStore::from_tcx is not used often.

@petrochenkov
Copy link
Copy Markdown
Contributor

@bors r=mejrs,petrochenkov

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 23, 2026

📌 Commit f1f655d has been approved by mejrs,petrochenkov

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. 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. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 23, 2026
@rust-bors

This comment has been minimized.

There are a number of things I dislike about `CrateMetadataRef`.
- It contains two fields `cstore` and `cdata`. The latter points to data
  within the former. It's like having an `Elem` type that has a
  reference to a vec element and also a reference to the vec itself.
  Weird.
- The `cdata` field gets a lot of use, and the `Deref` impl just derefs
  that field. The `cstore` field is rarely used.
- `CrateMetadataRef` is not a good name.
- Variables named `cdata` sometimes refer to values of this type and
  sometimes to values of type `CrateMetadata`, which is confusing.

The good news is that `CrateMetadataRef` is not necessary and can be
replaced with `&CrateMetadata`. Why? Everywhere that `CrateMetadataRef`
is used, a `TyCtxt` is also present, and the `CStore` is accessible from
the `TyCtxt` with `CStore::from_tcx`.

So this commit removes `CrateMetadataRef` and replaces all its uses with
`&CrateMetadata`. Notes:
- This requires adding only two uses of `CStore::from_tcx`, which shows
  how rarely the `cstore` field was used.
- `get_crate_data` now matches `get_crate_data_mut` more closely.
- A few variables are renamed for consistency, e.g. `data`/`cmeta` ->
  `cdata`.
- An unnecessary local variable (`local_cdata`) in `decode_expn_id` is
  removed.
- All the `CrateMetadataRef` methods become `CrateMetadata` methods, and
  their receiver changes from `self` to `&self`.
- `RawDefId::decode_from_cdata` is inlined and removed, because it has a
  single call site.
@nnethercote nnethercote force-pushed the eliminate-CrateMetadataRef branch from f1f655d to 804e419 Compare April 24, 2026 00:11
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 24, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@nnethercote
Copy link
Copy Markdown
Contributor Author

I think the perf benefits here are mostly/entirely due to eliminating a CStore::from_tcx call that was also eliminated in #155628, which has now merged. So I suspect this will now be perf-neutral. Let's check.

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@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 Apr 24, 2026
rust-bors Bot pushed a commit that referenced this pull request Apr 24, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 24, 2026

☀️ Try build successful (CI)
Build commit: 9237761 (92377611c4498a8049254f7034ab73d596516797, parent: 36ba2c7712052d731a7082d0eba5ed3d9d56c133)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (9237761): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

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

Max RSS (memory usage)

Results (secondary -1.1%)

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)
6.0% [6.0%, 6.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.6% [-6.5%, -2.7%] 2
All ❌✅ (primary) - - 0

Cycles

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

Binary size

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

Bootstrap: 490.512s -> 492.425s (0.39%)
Artifact size: 394.34 MiB -> 394.30 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 24, 2026
@nnethercote
Copy link
Copy Markdown
Contributor Author

This is now perf neutral and can be rolled up.

@bors r=mejrs,petrochenkov rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 24, 2026

📌 Commit 804e419 has been approved by mejrs,petrochenkov

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 24, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Apr 24, 2026
…aRef, r=mejrs,petrochenkov

Eliminate `CrateMetadataRef`.

There are a number of things I dislike about `CrateMetadataRef`.
- It contains two fields `cstore` and `cdata`. The latter points to data within the former. It's like having an `Elem` type that has a reference to a vec element and also a reference to the vec itself. Weird.
- The `cdata` field gets a lot of use, and the `Deref` impl just derefs that field. The `cstore` field is rarely used.
- `CrateMetadataRef` is not a good name.
- Variables named `cdata` sometimes refer to values of this type and sometimes to values of type `CrateMetadata`, which is confusing.

The good news is that `CrateMetadataRef` is not necessary and can be replaced with `&CrateMetadata`. Why? Everywhere that `CrateMetadataRef` is used, a `TyCtxt` is also present, and the `CStore` is accessible from the `TyCtxt` with `CStore::from_tcx`.

So this commit removes `CrateMetadataRef` and replaces all its uses with `&CrateMetadata`. Notes:
- This requires adding only two uses of `CStore::from_tcx`, which shows how rarely the `cstore` field was used.
- `get_crate_data` now matches `get_crate_data_mut` more closely.
- A few variables are renamed for consistency, e.g. `data`/`cmeta` -> `cdata`.
- An unnecessary local variable (`local_cdata`) in `decode_expn_id` is removed.
- All the `CrateMetadataRef` methods become `CrateMetadata` methods, and their receiver changes from `self` to `&self`.
- `RawDefId::decode_from_cdata` is inlined and removed, because it has a single call site.

r? @mejrs
rust-bors Bot pushed a commit that referenced this pull request Apr 24, 2026
Rollup of 9 pull requests

Successful merges:

 - #155684 (Generalize IO Traits for `Arc<T>` where `&T: IoTrait`)
 - #155081 (Move and clean up some ui test)
 - #155379 (Avoid query cycles in DataflowConstProp)
 - #155663 (Eliminate `CrateMetadataRef`.)
 - #155669 (Add `Sender` diagnostic item for `std::sync::mpsc::Sender`)
 - #155698 (Syntactically reject tuple index shorthands in struct patterns to fix a correctness regression)
 - #155703 (Remove myself as a maintainer of `wasm32-wasip1-threads`)
 - #155706 (Remove `AttributeLintKind` variants - part 7)
 - #155712 (Forbid `*-pass` and `*-fail` directives in tests/crashes)
@rust-bors rust-bors Bot merged commit 0c25381 into rust-lang:main Apr 24, 2026
12 checks passed
@rustbot rustbot added this to the 1.97.0 milestone Apr 24, 2026
rust-timer added a commit that referenced this pull request Apr 24, 2026
Rollup merge of #155663 - nnethercote:eliminate-CrateMetadataRef, r=mejrs,petrochenkov

Eliminate `CrateMetadataRef`.

There are a number of things I dislike about `CrateMetadataRef`.
- It contains two fields `cstore` and `cdata`. The latter points to data within the former. It's like having an `Elem` type that has a reference to a vec element and also a reference to the vec itself. Weird.
- The `cdata` field gets a lot of use, and the `Deref` impl just derefs that field. The `cstore` field is rarely used.
- `CrateMetadataRef` is not a good name.
- Variables named `cdata` sometimes refer to values of this type and sometimes to values of type `CrateMetadata`, which is confusing.

The good news is that `CrateMetadataRef` is not necessary and can be replaced with `&CrateMetadata`. Why? Everywhere that `CrateMetadataRef` is used, a `TyCtxt` is also present, and the `CStore` is accessible from the `TyCtxt` with `CStore::from_tcx`.

So this commit removes `CrateMetadataRef` and replaces all its uses with `&CrateMetadata`. Notes:
- This requires adding only two uses of `CStore::from_tcx`, which shows how rarely the `cstore` field was used.
- `get_crate_data` now matches `get_crate_data_mut` more closely.
- A few variables are renamed for consistency, e.g. `data`/`cmeta` -> `cdata`.
- An unnecessary local variable (`local_cdata`) in `decode_expn_id` is removed.
- All the `CrateMetadataRef` methods become `CrateMetadata` methods, and their receiver changes from `self` to `&self`.
- `RawDefId::decode_from_cdata` is inlined and removed, because it has a single call site.

r? @mejrs
@nnethercote nnethercote deleted the eliminate-CrateMetadataRef branch April 26, 2026 23:07
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Apr 27, 2026
Rollup of 9 pull requests

Successful merges:

 - rust-lang/rust#155684 (Generalize IO Traits for `Arc<T>` where `&T: IoTrait`)
 - rust-lang/rust#155081 (Move and clean up some ui test)
 - rust-lang/rust#155379 (Avoid query cycles in DataflowConstProp)
 - rust-lang/rust#155663 (Eliminate `CrateMetadataRef`.)
 - rust-lang/rust#155669 (Add `Sender` diagnostic item for `std::sync::mpsc::Sender`)
 - rust-lang/rust#155698 (Syntactically reject tuple index shorthands in struct patterns to fix a correctness regression)
 - rust-lang/rust#155703 (Remove myself as a maintainer of `wasm32-wasip1-threads`)
 - rust-lang/rust#155706 (Remove `AttributeLintKind` variants - part 7)
 - rust-lang/rust#155712 (Forbid `*-pass` and `*-fail` directives in tests/crashes)
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-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