Skip to content

Optimize flattening in apache airflow workspace #11313

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

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/uv-distribution/src/metadata/build_requires.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl BuildRequires {
project_workspace.project_root(),
project_sources,
project_indexes,
extra.as_ref(),
extra.as_deref(),
group,
locations,
project_workspace.workspace(),
Expand Down Expand Up @@ -181,7 +181,7 @@ impl BuildRequires {
workspace.install_path(),
project_sources,
project_indexes,
extra.as_ref(),
extra.as_deref(),
group,
locations,
workspace,
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-distribution/src/metadata/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ impl LoweredRequirement {
requirement
.marker
.top_level_extra_name()
.is_some_and(|extra| extra == *target)
.is_some_and(|extra| &*extra == target)
})
})
.cloned()
Expand Down
63 changes: 35 additions & 28 deletions crates/uv-distribution/src/metadata/requires_dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl RequiresDist {
project_workspace.project_root(),
project_sources,
project_indexes,
extra.as_ref(),
extra.as_deref(),
group,
locations,
project_workspace.workspace(),
Expand Down Expand Up @@ -262,7 +262,7 @@ impl RequiresDist {
// If there is no such requirement with the extra, error.
if !metadata.requires_dist.iter().any(|requirement| {
requirement.name == *name
&& requirement.marker.top_level_extra_name().as_ref() == Some(extra)
&& requirement.marker.top_level_extra_name().as_deref() == Some(extra)
}) {
return Err(MetadataError::IncompleteSourceExtra(
name.clone(),
Expand Down Expand Up @@ -370,6 +370,12 @@ impl FlatRequiresDist {
return Self(requirements);
}

// Memoize the top level extras, in the same order as `requirements`
let top_level_extras: Vec<_> = requirements
.iter()
.map(|req| req.marker.top_level_extra_name())
.collect();

// Transitively process all extras that are recursively included.
let mut flattened = requirements.clone();
let mut seen = FxHashSet::<(ExtraName, MarkerTree)>::default();
Expand All @@ -384,33 +390,34 @@ impl FlatRequiresDist {
}

// Find the requirements for the extra.
for requirement in &requirements {
if requirement.marker.top_level_extra_name().as_ref() == Some(&extra) {
let requirement = {
let mut marker = marker;
marker.and(requirement.marker);
uv_pypi_types::Requirement {
name: requirement.name.clone(),
extras: requirement.extras.clone(),
groups: requirement.groups.clone(),
source: requirement.source.clone(),
origin: requirement.origin.clone(),
marker: marker.simplify_extras(slice::from_ref(&extra)),
}
};
if requirement.name == *name {
// Add each transitively included extra.
queue.extend(
requirement
.extras
.iter()
.cloned()
.map(|extra| (extra, requirement.marker)),
);
} else {
// Add the requirements for that extra.
flattened.push(requirement);
for (requirement, top_level_extra) in requirements.iter().zip(top_level_extras.iter()) {
if top_level_extra.as_deref() != Some(&extra) {
continue;
}
let requirement = {
let mut marker = marker;
marker.and(requirement.marker);
uv_pypi_types::Requirement {
name: requirement.name.clone(),
extras: requirement.extras.clone(),
groups: requirement.groups.clone(),
source: requirement.source.clone(),
origin: requirement.origin.clone(),
marker: marker.simplify_extras(slice::from_ref(&extra)),
}
};
if requirement.name == *name {
// Add each transitively included extra.
queue.extend(
requirement
.extras
.iter()
.cloned()
.map(|extra| (extra, requirement.marker)),
);
} else {
// Add the requirements for that extra.
flattened.push(requirement);
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions crates/uv-pep508/src/marker/tree.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::cmp::Ordering;
use std::fmt::{self, Display, Formatter};
use std::ops::{Bound, Deref};
Expand Down Expand Up @@ -772,7 +773,7 @@ impl MarkerTree {
}

/// Returns the underlying [`MarkerTreeKind`] of the root node.
pub fn kind(&self) -> MarkerTreeKind<'_> {
pub fn kind(self) -> MarkerTreeKind<'static> {
if self.is_true() {
return MarkerTreeKind::True;
}
Expand Down Expand Up @@ -1002,11 +1003,19 @@ impl MarkerTree {
///
/// ASSUMPTION: There is one `extra = "..."`, and it's either the only marker or part of the
/// main conjunction.
pub fn top_level_extra_name(self) -> Option<ExtraName> {
pub fn top_level_extra_name(self) -> Option<Cow<'static, ExtraName>> {
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 didn't go through the DNF for the general top level marker case this could be a &'static ExtraName

// Fast path: The marker is only a `extra == "..."`.
if let MarkerTreeKind::Extra(marker) = self.kind() {
if marker.edge(true).is_true() {
let CanonicalMarkerValueExtra::Extra(extra) = marker.name;
return Some(Cow::Borrowed(extra));
}
}

let extra_expression = self.top_level_extra()?;

match extra_expression {
MarkerExpression::Extra { name, .. } => name.into_extra(),
MarkerExpression::Extra { name, .. } => name.into_extra().map(Cow::Owned),
_ => unreachable!(),
}
}
Expand Down
Loading