From 0a2271f5010e83c9cb4fa472414afd04cfe1f5c1 Mon Sep 17 00:00:00 2001 From: konstin Date: Fri, 7 Feb 2025 12:13:42 +0100 Subject: [PATCH 1/2] Add fast path for top level extra name --- .../src/metadata/build_requires.rs | 4 ++-- crates/uv-distribution/src/metadata/lowering.rs | 2 +- .../uv-distribution/src/metadata/requires_dist.rs | 6 +++--- crates/uv-pep508/src/marker/tree.rs | 15 ++++++++++++--- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/crates/uv-distribution/src/metadata/build_requires.rs b/crates/uv-distribution/src/metadata/build_requires.rs index 60724dfcb253..91c47c4bf2d1 100644 --- a/crates/uv-distribution/src/metadata/build_requires.rs +++ b/crates/uv-distribution/src/metadata/build_requires.rs @@ -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(), @@ -181,7 +181,7 @@ impl BuildRequires { workspace.install_path(), project_sources, project_indexes, - extra.as_ref(), + extra.as_deref(), group, locations, workspace, diff --git a/crates/uv-distribution/src/metadata/lowering.rs b/crates/uv-distribution/src/metadata/lowering.rs index 33d3d3a3141b..ef6cf0a30476 100644 --- a/crates/uv-distribution/src/metadata/lowering.rs +++ b/crates/uv-distribution/src/metadata/lowering.rs @@ -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() diff --git a/crates/uv-distribution/src/metadata/requires_dist.rs b/crates/uv-distribution/src/metadata/requires_dist.rs index 4c0ce7c71a61..d7fb3c55f68f 100644 --- a/crates/uv-distribution/src/metadata/requires_dist.rs +++ b/crates/uv-distribution/src/metadata/requires_dist.rs @@ -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(), @@ -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(), @@ -385,7 +385,7 @@ impl FlatRequiresDist { // Find the requirements for the extra. for requirement in &requirements { - if requirement.marker.top_level_extra_name().as_ref() == Some(&extra) { + if requirement.marker.top_level_extra_name().as_deref() == Some(&extra) { let requirement = { let mut marker = marker; marker.and(requirement.marker); diff --git a/crates/uv-pep508/src/marker/tree.rs b/crates/uv-pep508/src/marker/tree.rs index 2ad48a9ee620..7cdfad6f2502 100644 --- a/crates/uv-pep508/src/marker/tree.rs +++ b/crates/uv-pep508/src/marker/tree.rs @@ -1,3 +1,4 @@ +use std::borrow::Cow; use std::cmp::Ordering; use std::fmt::{self, Display, Formatter}; use std::ops::{Bound, Deref}; @@ -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; } @@ -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 { + pub fn top_level_extra_name(self) -> Option> { + // 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!(), } } From 6ee3df319c95c64d72f974122edfe00299e9e604 Mon Sep 17 00:00:00 2001 From: konstin Date: Fri, 7 Feb 2025 12:26:21 +0100 Subject: [PATCH 2/2] Memoize the top level extras for requirements --- .../src/metadata/requires_dist.rs | 59 +++++++++++-------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/crates/uv-distribution/src/metadata/requires_dist.rs b/crates/uv-distribution/src/metadata/requires_dist.rs index d7fb3c55f68f..17eb10906c30 100644 --- a/crates/uv-distribution/src/metadata/requires_dist.rs +++ b/crates/uv-distribution/src/metadata/requires_dist.rs @@ -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(); @@ -384,33 +390,34 @@ impl FlatRequiresDist { } // Find the requirements for the extra. - for requirement in &requirements { - if requirement.marker.top_level_extra_name().as_deref() == 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); } } }