Skip to content
Draft
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
33 changes: 33 additions & 0 deletions crates/uv-bench/benches/marker_conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,39 @@ fn marker_conflicts(criterion: &mut Criterion<WallTime>) {
},
);
}
let package = PackageName::from_str("repro").expect("valid benchmark package");
for pairs in [4, 12, 20] {
let mut marker = MarkerTree::TRUE;
let mut known_conflicts = FxHashMap::default();
for index in 0..pairs {
let left =
ExtraName::from_str(&format!("pair-{index:03}-a")).expect("valid benchmark extra");
let right =
ExtraName::from_str(&format!("pair-{index:03}-b")).expect("valid benchmark extra");
marker = marker.and(
MarkerTree::from_str(&format!("extra == '{left}' or extra == '{right}'"))
.expect("valid benchmark marker"),
);
known_conflicts.insert(ConflictItem::from((package.clone(), left)), darwin);
known_conflicts.insert(
ConflictItem::from((package.clone(), right)),
darwin.negate(),
);
}
group.bench_with_input(
BenchmarkId::new("resolve_shared_extra_pairs", pairs),
&(marker, known_conflicts),
|benchmark, (marker, known_conflicts)| {
benchmark.iter(|| {
black_box(resolve_activated_extras(
black_box(*marker),
Some(black_box(&package)),
black_box(known_conflicts),
))
});
},
);
}
group.finish();
}

Expand Down
74 changes: 52 additions & 22 deletions crates/uv-pep508/src/marker/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,24 +550,39 @@ impl InternerGuard<'_> {
&mut self,
i: NodeId,
f: &impl Fn(&Variable) -> Option<bool>,
) -> NodeId {
self.restrict_by_cached(i, f, &mut FxHashMap::default())
}

fn restrict_by_cached(
&mut self,
i: NodeId,
f: &impl Fn(&Variable) -> Option<bool>,
cache: &mut FxHashMap<NodeId, NodeId>,
) -> NodeId {
if matches!(i, NodeId::TRUE | NodeId::FALSE) {
return i;
}

let node = self.shared.node(i);
if let Edges::Boolean { high, low } = node.children {
if let Some(value) = f(&node.var) {
// Restrict this variable to the given output by merging it
// with the relevant child.
let node = if value { high } else { low };
return self.restrict_by(node.negate(i), f);
}
if let Some(&result) = cache.get(&i) {
return result;
}

// Restrict all nodes recursively.
let children = node.children.map(i, |node| self.restrict_by(node, f));
self.create_node(node.var.clone(), children)
let node = self.shared.node(i);
let result = if let Edges::Boolean { high, low } = node.children
&& let Some(value) = f(&node.var)
{
// Restrict this variable to the given output by merging it with the relevant child.
let node = if value { high } else { low };
self.restrict_by_cached(node.negate(i), f, cache)
} else {
// Restrict each shared subgraph once, including when its root variable is unchanged.
let children = node
.children
.map(i, |node| self.restrict_by_cached(node, f, cache));
self.create_node(node.var.clone(), children)
};
cache.insert(i, result);
result
}

/// Restrict a marker by assuming that another marker is true.
Expand Down Expand Up @@ -1697,18 +1712,10 @@ impl Edges {
fn map(&self, parent: NodeId, mut f: impl FnMut(NodeId) -> NodeId) -> Self {
match self {
Self::Version { edges: map } => Self::Version {
edges: map
.iter()
.cloned()
.map(|(range, node)| (range, f(node.negate(parent))))
.collect(),
edges: Self::map_ranges(map, parent, f),
},
Self::String { edges: map } => Self::String {
edges: map
.iter()
.cloned()
.map(|(range, node)| (range, f(node.negate(parent))))
.collect(),
edges: Self::map_ranges(map, parent, f),
},
Self::Boolean { high, low } => Self::Boolean {
low: f(low.negate(parent)),
Expand All @@ -1717,6 +1724,29 @@ impl Edges {
}
}

fn map_ranges<T>(
edges: &SmallVec<(Ranges<T>, NodeId)>,
parent: NodeId,
mut f: impl FnMut(NodeId) -> NodeId,
) -> SmallVec<(Ranges<T>, NodeId)>
where
T: Clone + Ord,
{
let mut mapped = SmallVec::new();
for (range, node) in edges {
let node = f(node.negate(parent));
match mapped.last_mut() {
Some((previous_range, previous))
if *previous == node && can_conjoin(previous_range, range) =>
{
*previous_range = previous_range.union(range);
}
_ => mapped.push((range.clone(), node)),
}
}
mapped
}

// Returns an iterator over all direct children of this node.
fn nodes(&self) -> impl Iterator<Item = NodeId> + '_ {
match self {
Expand Down
98 changes: 88 additions & 10 deletions crates/uv-pep508/src/marker/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::str::FromStr;

use arcstr::ArcStr;
use itertools::Itertools;
use rustc_hash::FxHashSet;
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
use version_ranges::Ranges;

Expand Down Expand Up @@ -1410,42 +1411,51 @@ impl MarkerTree {
Self(INTERNER.lock().only_extras(self.0))
}

/// Calls the provided function on every `extra` in this tree.
/// Calls the provided function on every `extra` decision node in this tree.
///
/// Shared subgraphs are visited only once.
///
/// The operator provided to the function is guaranteed to be
/// `MarkerOperator::Equal` or `MarkerOperator::NotEqual`.
pub fn visit_extras(self, mut f: impl FnMut(MarkerOperator, &ExtraName)) {
fn imp(tree: MarkerTree, f: &mut impl FnMut(MarkerOperator, &ExtraName)) {
fn imp(
tree: MarkerTree,
f: &mut impl FnMut(MarkerOperator, &ExtraName),
visited: &mut FxHashSet<MarkerTree>,
) {
if tree.is_true() || tree.is_false() || !visited.insert(tree) {
return;
}
match tree.kind() {
MarkerTreeKind::True | MarkerTreeKind::False => {}
MarkerTreeKind::Version(kind) => {
for (tree, _) in simplify::collect_edges(kind.edges()) {
imp(tree, f);
imp(tree, f, visited);
}
}
MarkerTreeKind::VersionString(kind) => {
for (tree, _) in simplify::collect_edges(kind.edges()) {
imp(tree, f);
imp(tree, f, visited);
}
}
MarkerTreeKind::String(kind) => {
for (tree, _) in simplify::collect_edges(kind.children()) {
imp(tree, f);
imp(tree, f, visited);
}
}
MarkerTreeKind::In(kind) => {
for (_, tree) in kind.children() {
imp(tree, f);
imp(tree, f, visited);
}
}
MarkerTreeKind::Contains(kind) => {
for (_, tree) in kind.children() {
imp(tree, f);
imp(tree, f, visited);
}
}
MarkerTreeKind::List(kind) => {
for (_, tree) in kind.children() {
imp(tree, f);
imp(tree, f, visited);
}
}
MarkerTreeKind::Extra(kind) => {
Expand All @@ -1455,12 +1465,12 @@ impl MarkerTree {
f(MarkerOperator::NotEqual, kind.name().extra());
}
for (_, tree) in kind.children() {
imp(tree, f);
imp(tree, f, visited);
}
}
}
}
imp(self, &mut f);
imp(self, &mut f, &mut FxHashSet::default());
}

fn simplify_extras_with_impl(self, is_extra: &impl Fn(&ExtraName) -> bool) -> Self {
Expand Down Expand Up @@ -1873,6 +1883,7 @@ impl schemars::JsonSchema for MarkerTree {

#[cfg(test)]
mod test {
use std::cell::Cell;
use std::ops::Bound;
use std::str::FromStr;

Expand Down Expand Up @@ -2574,6 +2585,39 @@ mod test {
assert_eq!(simplified, expected);
}

#[test]
fn simplify_extras_preserves_canonical_ranges() {
let marker = MarkerTree::from_str(
"(python_version >= '3.10' or sys_platform == 'linux') \
and extra != 'encoded' and extra == 'local'",
)
.expect("valid marker expression");
let encoded = ExtraName::from_str("encoded").expect("valid extra name");
let local = ExtraName::from_str("local").expect("valid extra name");
let encoded_activation =
MarkerTree::from_str("sys_platform == 'darwin'").expect("valid marker expression");
let local_activation =
MarkerTree::from_str("python_version >= '3.12'").expect("valid marker expression");
let expected =
MarkerTree::from_str("python_version >= '3.12' and sys_platform != 'darwin'")
.expect("valid marker expression");

for substitutions in [
[(&encoded, encoded_activation), (&local, local_activation)],
[(&local, local_activation), (&encoded, encoded_activation)],
] {
let mut transformed = marker;
for (extra, activation) in substitutions {
let activated = transformed.simplify_extras_with(|candidate| candidate == extra);
let activated = activated.and(activation);
let inactive = transformed.simplify_not_extras_with(|candidate| candidate == extra);
let inactive = inactive.and(activation.negate());
transformed = activated.or(inactive);
}
assert_eq!(transformed, expected);
}
}

#[test]
fn test_marker_simplification() {
assert_false("python_version == '3.9.1'");
Expand Down Expand Up @@ -3687,6 +3731,40 @@ mod test {
);
}

#[test]
fn visit_and_restrict_shared_extra_subgraphs() {
let mut marker = MarkerTree::TRUE;
for index in 0..12 {
marker = marker.and(m(&format!(
"extra == 'pair-{index:02}-a' or extra == 'pair-{index:02}-b'"
)));
}

let mut visits = 0;
marker.visit_extras(|_, _| visits += 1);
assert_eq!(visits, 24);

let restrictions = Cell::new(0);
assert_eq!(
marker.simplify_extras_with(|_| {
restrictions.set(restrictions.get() + 1);
false
}),
marker
);
assert_eq!(restrictions.get(), 24);

restrictions.set(0);
assert_eq!(
marker.simplify_not_extras_with(|_| {
restrictions.set(restrictions.get() + 1);
false
}),
marker
);
assert_eq!(restrictions.get(), 24);
}

#[test]
fn visit_extras() {
let collect = |m: MarkerTree| -> Vec<(&'static str, String)> {
Expand Down
Loading