Move extras onto annotated distribution (#3592)

## Summary

Like hashes, we can now store these on `AnnotatedDist` rather than
creating a parallel hash map and performing lookups later on.
This commit is contained in:
Charlie Marsh 2024-05-14 19:25:06 -04:00 committed by GitHub
parent c7348589fa
commit 8971944a01
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -54,6 +54,7 @@ pub enum AnnotationStyle {
#[derive(Debug)] #[derive(Debug)]
pub struct AnnotatedDist { pub struct AnnotatedDist {
pub dist: ResolvedDist, pub dist: ResolvedDist,
pub extras: Vec<ExtraName>,
pub hashes: Vec<HashDigest>, pub hashes: Vec<HashDigest>,
pub metadata: Metadata23, pub metadata: Metadata23,
} }
@ -82,8 +83,6 @@ impl Display for AnnotatedDist {
pub struct ResolutionGraph { pub struct ResolutionGraph {
/// The underlying graph. /// The underlying graph.
petgraph: petgraph::graph::Graph<AnnotatedDist, Range<Version>, petgraph::Directed>, petgraph: petgraph::graph::Graph<AnnotatedDist, Range<Version>, petgraph::Directed>,
/// The enabled extras for every distribution in this resolution.
extras: FxHashMap<PackageName, Vec<ExtraName>>,
/// The set of editable requirements in this resolution. /// The set of editable requirements in this resolution.
editables: Editables, editables: Editables,
/// Any diagnostics that were encountered while building the graph. /// Any diagnostics that were encountered while building the graph.
@ -257,9 +256,13 @@ impl ResolutionGraph {
archive.metadata.clone() archive.metadata.clone()
}; };
// Extract the extras.
let extras = extras.get(package_name).cloned().unwrap_or_default();
// Add the distribution to the graph. // Add the distribution to the graph.
let index = petgraph.add_node(AnnotatedDist { let index = petgraph.add_node(AnnotatedDist {
dist, dist,
extras,
hashes, hashes,
metadata, metadata,
}); });
@ -273,6 +276,7 @@ impl ResolutionGraph {
// Add the distribution to the graph. // Add the distribution to the graph.
let index = petgraph.add_node(AnnotatedDist { let index = petgraph.add_node(AnnotatedDist {
dist: dist.into(), dist: dist.into(),
extras: editable.extras.clone(),
hashes: vec![], hashes: vec![],
metadata: metadata.clone(), metadata: metadata.clone(),
}); });
@ -324,9 +328,13 @@ impl ResolutionGraph {
archive.metadata.clone() archive.metadata.clone()
}; };
// Extract the extras.
let extras = extras.get(package_name).cloned().unwrap_or_default();
// Add the distribution to the graph. // Add the distribution to the graph.
let index = petgraph.add_node(AnnotatedDist { let index = petgraph.add_node(AnnotatedDist {
dist: dist.into(), dist: dist.into(),
extras,
hashes, hashes,
metadata, metadata,
}); });
@ -383,7 +391,6 @@ impl ResolutionGraph {
Ok(Self { Ok(Self {
petgraph, petgraph,
extras,
editables, editables,
diagnostics, diagnostics,
}) })
@ -659,7 +666,7 @@ enum Node<'a> {
/// A node linked to an editable distribution. /// A node linked to an editable distribution.
Editable(&'a LocalEditable), Editable(&'a LocalEditable),
/// A node linked to a non-editable distribution. /// A node linked to a non-editable distribution.
Distribution(&'a PackageName, &'a AnnotatedDist, &'a [ExtraName]), Distribution(&'a AnnotatedDist),
} }
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
@ -675,7 +682,7 @@ impl<'a> Node<'a> {
fn key(&self) -> NodeKey<'a> { fn key(&self) -> NodeKey<'a> {
match self { match self {
Node::Editable(editable) => NodeKey::Editable(editable.verbatim()), Node::Editable(editable) => NodeKey::Editable(editable.verbatim()),
Node::Distribution(name, _, _) => NodeKey::Distribution(name), Node::Distribution(annotated) => NodeKey::Distribution(annotated.name()),
} }
} }
@ -683,27 +690,15 @@ impl<'a> Node<'a> {
fn index(&self) -> Option<&IndexUrl> { fn index(&self) -> Option<&IndexUrl> {
match self { match self {
Node::Editable(_) => None, Node::Editable(_) => None,
Node::Distribution(_, package, _) => package.dist.index(), Node::Distribution(annotated) => annotated.dist.index(),
} }
} }
}
impl Verbatim for Node<'_> { /// Return the hashes of the distribution.
fn verbatim(&self) -> Cow<'_, str> { fn hashes(&self) -> &[HashDigest] {
match self { match self {
Node::Editable(editable) => Cow::Owned(format!("-e {}", editable.verbatim())), Node::Editable(_) => &[],
Node::Distribution(_, dist, &[]) => dist.verbatim(), Node::Distribution(annotated) => &annotated.hashes,
Node::Distribution(_, dist, extras) => {
let mut extras = extras.to_vec();
extras.sort_unstable();
extras.dedup();
Cow::Owned(format!(
"{}[{}]{}",
dist.name(),
extras.into_iter().join(", "),
dist.version_or_url().verbatim()
))
}
} }
} }
} }
@ -725,17 +720,8 @@ impl std::fmt::Display for DisplayResolutionGraph<'_> {
let node = if let Some((editable, _, _)) = self.resolution.editables.get(name) { let node = if let Some((editable, _, _)) = self.resolution.editables.get(name) {
Node::Editable(editable) Node::Editable(editable)
} else if self.include_extras {
Node::Distribution(
name,
dist,
self.resolution
.extras
.get(name)
.map_or(&[], |extras| extras.as_slice()),
)
} else { } else {
Node::Distribution(name, dist, &[]) Node::Distribution(dist)
}; };
Some((index, node)) Some((index, node))
}) })
@ -747,18 +733,33 @@ impl std::fmt::Display for DisplayResolutionGraph<'_> {
// Print out the dependency graph. // Print out the dependency graph.
for (index, node) in nodes { for (index, node) in nodes {
// Display the node itself. // Display the node itself.
let mut line = node.verbatim().to_string(); let mut line = match node {
Node::Editable(editable) => format!("-e {}", editable.verbatim()),
Node::Distribution(dist) => {
if self.include_extras && !dist.extras.is_empty() {
let mut extras = dist.extras.clone();
extras.sort_unstable();
extras.dedup();
format!(
"{}[{}]{}",
dist.name(),
extras.into_iter().join(", "),
dist.version_or_url().verbatim()
)
} else {
dist.verbatim().to_string()
}
}
};
// Display the distribution hashes, if any. // Display the distribution hashes, if any.
let mut has_hashes = false; let mut has_hashes = false;
if self.show_hashes { if self.show_hashes {
if let Node::Distribution(_, package, _) = node { for hash in node.hashes() {
for hash in &package.hashes { has_hashes = true;
has_hashes = true; line.push_str(" \\\n");
line.push_str(" \\\n"); line.push_str(" --hash=");
line.push_str(" --hash="); line.push_str(&hash.to_string());
line.push_str(&hash.to_string());
}
} }
} }
@ -783,7 +784,7 @@ impl std::fmt::Display for DisplayResolutionGraph<'_> {
Node::Editable(editable) => { Node::Editable(editable) => {
self.sources.get_editable(&editable.url).unwrap_or(&default) self.sources.get_editable(&editable.url).unwrap_or(&default)
} }
Node::Distribution(name, _, _) => self.sources.get(name).unwrap_or(&default), Node::Distribution(dist) => self.sources.get(dist.name()).unwrap_or(&default),
}; };
match self.annotation_style { match self.annotation_style {