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::visit::EdgeRef;
use petgraph::{Direction, Graph};
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
use std::collections::hash_map::Entry;
use uv_normalize::{ExtraName, GroupName, PackageName};
use uv_pep508::MarkerTree;
use uv_pypi_types::{ConflictItem, Conflicts};
@ -240,18 +240,18 @@ pub(crate) fn simplify_conflict_markers(
if !inf.included {
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
.iter()
.filter_map(|inf| {
if !inf.included {
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)
});
if !all_paths_satisfied {

View File

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