Remove clone from marker evaluation (#11562)

## Summary

Something I noticed while looking at
https://github.com/astral-sh/uv/issues/11548.
This commit is contained in:
Charlie Marsh 2025-02-16 15:55:27 -05:00 committed by GitHub
parent 346e6b7e8b
commit 4e6c07665c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 17 deletions

View File

@ -1,10 +1,10 @@
use std::collections::hash_map::Entry;
use petgraph::graph::{EdgeIndex, NodeIndex}; use petgraph::graph::{EdgeIndex, NodeIndex};
use petgraph::visit::EdgeRef; use petgraph::visit::EdgeRef;
use petgraph::{Direction, Graph}; use petgraph::{Direction, Graph};
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet}; use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
use std::collections::hash_map::Entry;
use uv_normalize::{ExtraName, GroupName, PackageName};
use uv_pep508::MarkerTree; use uv_pep508::MarkerTree;
use uv_pypi_types::{ConflictItem, Conflicts}; use uv_pypi_types::{ConflictItem, Conflicts};
@ -240,18 +240,18 @@ pub(crate) fn simplify_conflict_markers(
if !inf.included { if !inf.included {
return None; return None;
} }
Some((inf.item.package().clone(), inf.item.extra()?.clone())) Some((inf.item.package(), inf.item.extra()?))
}) })
.collect::<Vec<(PackageName, ExtraName)>>(); .collect::<Vec<_>>();
let groups = set let groups = set
.iter() .iter()
.filter_map(|inf| { .filter_map(|inf| {
if !inf.included { if !inf.included {
return None; return None;
} }
Some((inf.item.package().clone(), inf.item.group()?.clone())) Some((inf.item.package(), inf.item.group()?))
}) })
.collect::<Vec<(PackageName, GroupName)>>(); .collect::<Vec<_>>();
graph[edge_index].conflict().evaluate(&extras, &groups) graph[edge_index].conflict().evaluate(&extras, &groups)
}); });
if !all_paths_satisfied { if !all_paths_satisfied {

View File

@ -421,11 +421,12 @@ impl ConflictMarker {
/// Returns true if this conflict marker is satisfied by the given /// Returns true if this conflict marker is satisfied by the given
/// list of activated extras and groups. /// list of activated extras and groups.
pub(crate) fn evaluate( pub(crate) fn evaluate<P, E, G>(self, extras: &[(P, E)], groups: &[(P, G)]) -> bool
self, where
extras: &[(PackageName, ExtraName)], P: Borrow<PackageName>,
groups: &[(PackageName, GroupName)], E: Borrow<ExtraName>,
) -> bool { G: Borrow<GroupName>,
{
static DUMMY: std::sync::LazyLock<MarkerEnvironment> = std::sync::LazyLock::new(|| { static DUMMY: std::sync::LazyLock<MarkerEnvironment> = std::sync::LazyLock::new(|| {
MarkerEnvironment::try_from(MarkerEnvironmentBuilder { MarkerEnvironment::try_from(MarkerEnvironmentBuilder {
implementation_name: "", implementation_name: "",
@ -444,10 +445,10 @@ impl ConflictMarker {
}); });
let extras = extras let extras = extras
.iter() .iter()
.map(|(package, extra)| encode_package_extra(package, extra)); .map(|(package, extra)| encode_package_extra(package.borrow(), extra.borrow()));
let groups = groups let groups = groups
.iter() .iter()
.map(|(package, group)| encode_package_group(package, group)); .map(|(package, group)| encode_package_group(package.borrow(), group.borrow()));
self.marker self.marker
.evaluate(&DUMMY, &extras.chain(groups).collect::<Vec<ExtraName>>()) .evaluate(&DUMMY, &extras.chain(groups).collect::<Vec<ExtraName>>())
} }
@ -597,9 +598,10 @@ mod tests {
.iter() .iter()
.copied() .copied()
.map(|name| (create_package("pkg"), create_extra(name))) .map(|name| (create_package("pkg"), create_extra(name)))
.collect::<Vec<_>>(); .collect::<Vec<(PackageName, ExtraName)>>();
let groups = Vec::<(PackageName, GroupName)>::new();
assert!( assert!(
!cm.evaluate(&extras, &[]), !cm.evaluate(&extras, &groups),
"expected `{extra_names:?}` to evaluate to `false` in `{cm:?}`" "expected `{extra_names:?}` to evaluate to `false` in `{cm:?}`"
); );
} }
@ -619,9 +621,10 @@ mod tests {
.iter() .iter()
.copied() .copied()
.map(|name| (create_package("pkg"), create_extra(name))) .map(|name| (create_package("pkg"), create_extra(name)))
.collect::<Vec<_>>(); .collect::<Vec<(PackageName, ExtraName)>>();
let groups = Vec::<(PackageName, GroupName)>::new();
assert!( assert!(
cm.evaluate(&extras, &[]), cm.evaluate(&extras, &groups),
"expected `{extra_names:?}` to evaluate to `true` in `{cm:?}`" "expected `{extra_names:?}` to evaluate to `true` in `{cm:?}`"
); );
} }