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

Avoid allocating OrderedSet in UnionBuilder::simplify #13206

Merged
merged 2 commits into from
Sep 2, 2024

Conversation

MichaReiser
Copy link
Member

Summary

Calling OrderedSet::is_superset with a [...].into() allocates a new OrderedSet. The compiler may be able to
figure out that the set isn't strictly necessary, although I doubt that it is (requires reasoning across is_superset and Into).

This PR removes the is_superset call with two simple contains calls. It also tries to preserve the type orders
(which may also help with performance because remove in an OrderedSet is O(n).

Test Plan

cargo test

@MichaReiser MichaReiser added the red-knot Multi-file analysis & type inference label Sep 2, 2024
Copy link
Contributor

github-actions bot commented Sep 2, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Comment on lines 85 to 89
match self.elements.len() {
0 => Type::Never,
1 => self.elements[0],
_ => Type::Union(UnionType::new(self.db, self.elements)),
}
Copy link
Member

Choose a reason for hiding this comment

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

nit: we could match against a slice of elements (as_slice) to avoid indexing

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately not. The OrderedSet::as_slice method doesn't return a regular slice. It's a newtype wrapper around Slice that doesn't support matching.

@MichaReiser MichaReiser enabled auto-merge (squash) September 2, 2024 09:04
@MichaReiser MichaReiser merged commit 9986397 into main Sep 2, 2024
19 checks passed
@MichaReiser MichaReiser deleted the micha/union-builder-avoid-set branch September 2, 2024 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red-knot Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants