Optimize requirements_for_extra (#10348)

This commit is contained in:
konsti 2025-01-07 15:12:12 +01:00 committed by GitHub
parent c6ac121ed0
commit 3cbf8ab7b1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 55 additions and 33 deletions

View file

@ -42,7 +42,12 @@ impl Overrides {
&'a self,
requirements: impl IntoIterator<Item = &'a Requirement>,
) -> impl Iterator<Item = Cow<'a, Requirement>> {
requirements.into_iter().flat_map(|requirement| {
if self.0.is_empty() {
// Fast path: There are no overrides.
return Either::Left(requirements.into_iter().map(Cow::Borrowed));
}
Either::Right(requirements.into_iter().flat_map(|requirement| {
let Some(overrides) = self.get(&requirement.name) else {
// Case 1: No override(s).
return Either::Left(std::iter::once(Cow::Borrowed(requirement)));
@ -70,6 +75,6 @@ impl Overrides {
})
},
)))
})
}))
}
}