Skip to content

Use LocalDefId as an index in metadata encoding - #163321

Open
aerooneqq wants to merge 8 commits into
rust-lang:mainfrom
aerooneqq:local-def-index
Open

aerooneqq wants to merge 8 commits into
rust-lang:mainfrom
aerooneqq:local-def-index

Conversation

@aerooneqq

@aerooneqq aerooneqq commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

The goal of this PR to eliminate encoding of a raw DefIndex, instead we should encode something (LocalDefId) that certainly indicates that index belongs to the local crate. The implementation of DefIndex encoding should always panic to prevent accidental remappings of non-local DefIndexes in #162809.

So the plan is as follows:

  • Use LocalDefId as a writing index in tables for metadata encoding, use LocalDefId instead of DefIndex everywhere, specialize encode/decode methods to correctly process it,
  • Replace implementation of encode_def_index in EncodeContext with panic, so we never remap and encode def index,
  • Make tables' keys two-sided: we can read and write using different index types, in this case we will write using LocalDefId and read using DefIndex (done in Allow using different index types when reading and writing to tables #163447).

Blocked by #162900.

r? @petrochenkov

@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 25, 2026
@petrochenkov

Copy link
Copy Markdown
Contributor

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

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 25, 2026
Create `LocalDefIndex` for metadata encoding
@rust-bors

rust-bors Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 2fa5a47 (2fa5a472bd28122b86b69706e673d2b3704e5c27)
Base parent: 4e701dc (4e701dc6ba63a40c908a173df269a55e1fd6297e)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (2fa5a47): comparison URL.

Overall result: ❌ regressions - 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

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.8% [0.8%, 0.9%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

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

Cycles

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

Binary size

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

Bootstrap: 488.984s -> 488.935s (-0.01%)
Artifact size: 406.31 MiB -> 406.32 MiB (0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 25, 2026
@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_hir_id/src/definitions.rs
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 28, 2026
…ata, r=petrochenkov

Allow using different index types when reading and writing to tables

Make tables of metadata two-sided: one can write with one index type and read with another, as long as both those types are indexes. That will be used in rust-lang#163321 when we will have `LocalDefIndex` or similar type.

r? @petrochenkov
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 28, 2026
…ata, r=petrochenkov

Allow using different index types when reading and writing to tables

Make tables of metadata two-sided: one can write with one index type and read with another, as long as both those types are indexes. That will be used in rust-lang#163321 when we will have `LocalDefIndex` or similar type.

r? @petrochenkov
tgross35 added a commit to tgross35/rust that referenced this pull request Sep 29, 2026
…ata, r=petrochenkov

Allow using different index types when reading and writing to tables

Make tables of metadata two-sided: one can write with one index type and read with another, as long as both those types are indexes. That will be used in rust-lang#163321 when we will have `LocalDefIndex` or similar type.

r? @petrochenkov
tgross35 added a commit to tgross35/rust that referenced this pull request Sep 29, 2026
…ata, r=petrochenkov

Allow using different index types when reading and writing to tables

Make tables of metadata two-sided: one can write with one index type and read with another, as long as both those types are indexes. That will be used in rust-lang#163321 when we will have `LocalDefIndex` or similar type.

r? @petrochenkov
@rust-bors

This comment has been minimized.

@aerooneqq
aerooneqq force-pushed the local-def-index branch 2 times, most recently from d3319be to 68c2396 Compare September 29, 2026 10:11
@rustbot rustbot added the A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) label Sep 29, 2026
@rust-log-analyzer

This comment has been minimized.

@aerooneqq

Copy link
Copy Markdown
Contributor Author

After some experiments with LocalDefIndex and LocalDefId it seems that LocalDefId is a better option, so now waiting for #162900 to merge and after that this will be ready.

@aerooneqq aerooneqq changed the title Create LocalDefIndex for metadata encoding Use LocalDefId as an index in metadata encoding Sep 29, 2026
@rust-bors

This comment has been minimized.

@petrochenkov

Copy link
Copy Markdown
Contributor

@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 Oct 2, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 2, 2026
Use `LocalDefId` as an index in metadata encoding
@rust-bors

rust-bors Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 9c851b5 (9c851b5dd100fb5b5547d9c0090ef7f1e97e902e)
Base parent: c165834 (c1658342b0ea7eab61db1960082668c77f553522)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (9c851b5): 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 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
Improvements ✅
(primary)
-0.5% [-0.5%, -0.5%] 2
Improvements ✅
(secondary)
-0.4% [-0.7%, -0.2%] 31
All ❌✅ (primary) -0.5% [-0.5%, -0.5%] 2

Max RSS (memory usage)

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

Cycles

Results (secondary 4.6%)

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

Binary size

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

Bootstrap: 491.121s -> 491.415s (0.06%)
Artifact size: 408.64 MiB -> 408.67 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 2, 2026
@aerooneqq
aerooneqq marked this pull request as ready for review October 2, 2026 16:40
@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 Oct 2, 2026
@aerooneqq

Copy link
Copy Markdown
Contributor Author

cc @oli-obk as it is important prerequisite for #162809.

Comment thread compiler/rustc_span/src/def_id.rs Outdated
Comment thread compiler/rustc_metadata/src/rmeta/mod.rs
Comment thread compiler/rustc_metadata/src/rmeta/mod.rs Outdated
Comment thread compiler/rustc_metadata/src/rmeta/table.rs
Comment thread compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs Outdated
Comment thread compiler/rustc_metadata/src/rmeta/encoder.rs
Comment thread compiler/rustc_metadata/src/rmeta/encoder.rs Outdated
}
}

impl<'a, 'tcx> Decodable<MetadataDecodeContext<'a, 'tcx>> for LocalDefId {

@petrochenkov petrochenkov Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hm, why do local def ids ever need to be decoded from metadata?

Or is this somehow used for decoding external DefIndexes, which are not actually local?
Then it's a hack, that needs at least a good comment explaining why we have to do this.

Ideally, we'd of course decode DefIndexes directly, even if they were decoded as LocalDefIds, but I agree that it may be annoying if they are e.g. use the same structure in compiler/rustc_metadata/src/rmeta/mod.rs.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

When LocalDefId is in the value of some table we need to decode it, we can access tables' keys with different types as we created infrastructure for this, but when the same situation happens in values there is more needed to be done to support it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Then it's a hack, that needs at least a good comment explaining why we have to do this.

Still needs a comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Or is this somehow used for decoding external DefIndexes, which are not actually local?

They are local to the crate being decoded, so those def indices are external for the current crate but local to external, it makes some sense why we decoding local def ids.

@petrochenkov petrochenkov 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 Oct 3, 2026
@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 Oct 5, 2026
@petrochenkov

Copy link
Copy Markdown
Contributor

(I'll finish the review later today, need to go now.)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants