[red-knot] Avoid creating UnionBuilders unnecessarily#16218
Closed
MichaReiser wants to merge 4 commits intomainfrom
Closed
[red-knot] Avoid creating UnionBuilders unnecessarily#16218MichaReiser wants to merge 4 commits intomainfrom
UnionBuilders unnecessarily#16218MichaReiser wants to merge 4 commits intomainfrom
Conversation
Member
|
This looks great to me, and codspeed's reporting a 1% speedup. Another optimisation idea could be to pre-size the diff --git a/crates/red_knot_python_semantic/src/types.rs b/crates/red_knot_python_semantic/src/types.rs
index 4601543d4..6df55b467 100644
--- a/crates/red_knot_python_semantic/src/types.rs
+++ b/crates/red_knot_python_semantic/src/types.rs
@@ -4695,7 +4695,13 @@ impl<'db> UnionType<'db> {
return first.into();
};
- let mut builder = UnionBuilder::new(db).add(first.into()).add(second.into());
+ let iterator_length = elements.size_hint().1.and_then(|size| size.checked_add(2));
+
+ let mut builder = iterator_length
+ .map(|num_elements| UnionBuilder::with_capacity(db, num_elements))
+ .unwrap_or_else(|| UnionBuilder::new(db))
+ .add(first.into())
+ .add(second.into());
for element in elements {
builder = builder.add(element.into());
diff --git a/crates/red_knot_python_semantic/src/types/builder.rs b/crates/red_knot_python_semantic/src/types/builder.rs
index 45be6ac48..c5cdf294d 100644
--- a/crates/red_knot_python_semantic/src/types/builder.rs
+++ b/crates/red_knot_python_semantic/src/types/builder.rs
@@ -43,6 +43,13 @@ impl<'db> UnionBuilder<'db> {
}
}
+ pub(crate) fn with_capacity(db: &'db dyn Db, capacity: usize) -> Self {
+ Self {
+ db,
+ elements: Vec::with_capacity(capacity),
+ }
+ }
+
/// Collapse the union to a single type: `object`.
fn collapse_to_object(mut self) -> Self {
self.elements.clear(); |
UnionBuilders unnecessaryUnionBuilders unnecessarily
5ad6a67 to
fe308d6
Compare
fe308d6 to
6ce0a3c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
UnionBuilder::from_elementsis called very frequently. Let's see if short-circuiting in the case whereelementsis zero or one elementslong helps improve perf.
Test Plan
This should not change behavior.