Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pattern analysis: Use contiguous indices for enum variants #16979

Merged
merged 1 commit into from Apr 1, 2024

Conversation

Nadrieril
Copy link
Member

@Nadrieril Nadrieril commented Mar 30, 2024

The main blocker to using the in-tree version of the pattern_analysis crate is that rustc requires enum indices to be contiguous because it uses IndexVec/BitSet for performance. Currently we swap these out for FxHashMap/FxHashSet when the rustc feature is off, but we can't do that if we use the in-tree crate.

This PR solves the problem by using contiguous indices on the r-a side too.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 30, 2024
&Variant(id) => Some(id.into()),
Variant(id) => {
let hir_def::AdtId::EnumId(eid) = adt else {
panic!("bad constructor {ctor:?} for adt {adt:?}")
Copy link
Member

Choose a reason for hiding this comment

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

Maybe stdx::never and return None here so we don't panic?

Copy link
Member Author

Choose a reason for hiding this comment

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

If we return None we panic only a tiny bit later because most of the calls of this function unwrap() the output

Copy link
Member Author

Choose a reason for hiding this comment

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

Want me to change that anyway?

Copy link
Member

Choose a reason for hiding this comment

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

panicking early seems better then

Comment on lines 50 to 57
// Find the index of this variant in the list of variants.
db.enum_data(eid)
.variants
.iter()
.map(|(evid, _name)| *evid)
.enumerate()
.find(|(_, evid)| *evid == target_evid)
.map(|(i, _)| EnumVariantContiguousIndex(i))
Copy link
Member

Choose a reason for hiding this comment

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

This can be simplified to

Suggested change
// Find the index of this variant in the list of variants.
db.enum_data(eid)
.variants
.iter()
.map(|(evid, _name)| *evid)
.enumerate()
.find(|(_, evid)| *evid == target_evid)
.map(|(i, _)| EnumVariantContiguousIndex(i))
target_evid.lookup(self.db.upcast()).index as usize

(we store the index in the item tree to prevent a linear lookup here)

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh nicee

@Veykril
Copy link
Member

Veykril commented Apr 1, 2024

Thanks!
@bors delegate+

@bors
Copy link
Collaborator

bors commented Apr 1, 2024

✌️ @Nadrieril, you can now approve this pull request!

If @Veykril told you to "r=me" after making some further change, please make that change, then do @bors r=@Veykril

@Nadrieril
Copy link
Member Author

@bors r=@Veykril

@bors
Copy link
Collaborator

bors commented Apr 1, 2024

📌 Commit 7e8f2d8 has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Apr 1, 2024

⌛ Testing commit 7e8f2d8 with merge c82d168...

@bors
Copy link
Collaborator

bors commented Apr 1, 2024

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing c82d168 to master...

@bors bors merged commit c82d168 into rust-lang:master Apr 1, 2024
11 checks passed
@Nadrieril Nadrieril deleted the contiguous-enum-id branch April 1, 2024 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants