Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
x-hgg-x committed Dec 14, 2024
1 parent e9170cb commit b0ddb5f
Show file tree
Hide file tree
Showing 38 changed files with 3,814 additions and 2,563 deletions.
426 changes: 230 additions & 196 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ keywords = ["dependency", "pubgrub", "semver", "solver", "version"]
categories = ["algorithms"]
include = ["Cargo.toml", "LICENSE", "README.md", "src/**", "tests/**", "examples/**", "benches/**"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
indexmap = "2.6.0"
# for debug logs in tests
log = "0.4.22"
priority-queue = "2.1.1"
rustc-hash = ">=1.0.0, <3.0.0"
serde = { version = "1.0", features = ["derive"], optional = true }
smallvec = { version = "1.13.2", features = ["union"] }
thiserror = "2.0"
version-ranges = { version = "0.1.0", path = "version-ranges" }

Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ So, because root depends on both menu >=1.0.0 and intl >=5.0.0,
```

This pubgrub crate provides a Rust implementation of PubGrub.
It is generic and works for any type of dependency system
as long as packages (P) and versions (V) implement
the provided `Package` and `Version` traits.


## Using the pubgrub crate

Expand Down
6 changes: 3 additions & 3 deletions benches/backtracking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn backtracking_singletons(c: &mut Criterion, package_count: u32, version_count:

c.bench_function("backtracking_singletons", |b| {
b.iter(|| {
let _ = pubgrub::resolve(&dependency_provider, 0u32, 0u32);
let _ = dependency_provider.resolve(0u32, 0u32);
})
});
}
Expand Down Expand Up @@ -59,7 +59,7 @@ fn backtracking_disjoint_versions(c: &mut Criterion, package_count: u32, version

c.bench_function("backtracking_disjoint_versions", |b| {
b.iter(|| {
let _ = pubgrub::resolve(&dependency_provider, 0u32, 0u32);
let _ = dependency_provider.resolve(0u32, 0u32);
})
});
}
Expand All @@ -83,7 +83,7 @@ fn backtracking_ranges(c: &mut Criterion, package_count: u32, version_count: u32

c.bench_function("backtracking_ranges", |b| {
b.iter(|| {
let _ = pubgrub::resolve(&dependency_provider, 0u32, 0u32);
let _ = dependency_provider.resolve(0u32, 0u32);
})
});
}
Expand Down
38 changes: 24 additions & 14 deletions benches/large_case.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
// SPDX-License-Identifier: MPL-2.0

use std::fmt::{Debug, Display};
use std::hash::Hash;
use std::time::Duration;

use criterion::*;
use pubgrub::{Map, OfflineDependencyProvider, Range, VersionRanges};
use serde::de::Deserialize;

use pubgrub::{resolve, OfflineDependencyProvider, Package, Range, SemanticVersion, VersionSet};

fn bench<'a, P: Package + Deserialize<'a>, VS: VersionSet + Deserialize<'a>>(
fn bench<
'a,
P: Debug + Display + Clone + Eq + Hash + Deserialize<'a>,
R: VersionRanges + Deserialize<'a>,
>(
b: &mut Bencher,
case: &'a str,
) where
<VS as VersionSet>::V: Deserialize<'a>,
R::V: Deserialize<'a>,
{
let dependency_provider: OfflineDependencyProvider<P, VS> = ron::de::from_str(case).unwrap();
let mut dependency_provider: OfflineDependencyProvider<P, R> = ron::de::from_str(case).unwrap();

let dependencies = dependency_provider
.packages()
.map(|p| {
(
p.clone(),
dependency_provider.versions(p).unwrap().cloned().collect(),
)
})
.collect::<Map<_, Vec<_>>>();

b.iter(|| {
for p in dependency_provider.packages() {
for n in dependency_provider.versions(p).unwrap() {
let _ = resolve(&dependency_provider, p.clone(), n.clone());
for (p, versions) in &dependencies {
for v in versions {
let _ = dependency_provider.resolve(p.clone(), v.clone());
}
}
});
Expand All @@ -32,14 +46,10 @@ fn bench_nested(c: &mut Criterion) {
let case = case.unwrap().path();
let name = case.file_name().unwrap().to_string_lossy();
let data = std::fs::read_to_string(&case).unwrap();
if name.ends_with("u16_NumberVersion.ron") || name.ends_with("u16_u32.ron") {
if name.ends_with("u16_NumberVersion.ron") {
group.bench_function(name, |b| {
bench::<u16, Range<u32>>(b, &data);
});
} else if name.ends_with("str_SemanticVersion.ron") {
group.bench_function(name, |b| {
bench::<&str, Range<SemanticVersion>>(b, &data);
});
}
}

Expand Down
6 changes: 3 additions & 3 deletions benches/sudoku.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
//! Uses `Arc<usize>` for being closer to real versions.
// SPDX-License-Identifier: MPL-2.0

use pubgrub::{resolve, OfflineDependencyProvider, Range};
use std::fmt;
use std::sync::Arc;
use version_ranges::Ranges;

use criterion::*;
use pubgrub::{OfflineDependencyProvider, Range};
use version_ranges::Ranges;

/// The size of a box in the board.
const BOARD_BASE: usize = 3;
Expand Down Expand Up @@ -122,7 +122,7 @@ fn solve(c: &mut Criterion, board: Vec<(SudokuPackage, Ranges<Arc<usize>>)>, cas
dependency_provider.add_dependencies(SudokuPackage::Root, Arc::new(1usize), board);
c.bench_function(case, |b| {
b.iter(|| {
let _ = resolve(&dependency_provider, SudokuPackage::Root, Arc::new(1usize));
let _ = dependency_provider.resolve(SudokuPackage::Root, Arc::new(1usize));
})
});
}
Expand Down
24 changes: 14 additions & 10 deletions examples/branching_error_reporting.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MPL-2.0

use pubgrub::{
resolve, DefaultStringReporter, OfflineDependencyProvider, PubGrubError, Ranges, Reporter,
DefaultStringReporter, OfflineDependencyProvider, PubGrubError, Ranges, Reporter,
SemanticVersion,
};

Expand All @@ -10,15 +10,16 @@ type SemVS = Ranges<SemanticVersion>;
// https://github.com/dart-lang/pub/blob/master/doc/solver.md#branching-error-reporting
fn main() {
let mut dependency_provider = OfflineDependencyProvider::<&str, SemVS>::new();

#[rustfmt::skip]
// root 1.0.0 depends on foo ^1.0.0
dependency_provider.add_dependencies(
dependency_provider.add_dependencies(
"root", (1, 0, 0),
[("foo", Ranges::from_range_bounds((1, 0, 0)..(2, 0, 0)))],
);
#[rustfmt::skip]
// foo 1.0.0 depends on a ^1.0.0 and b ^1.0.0
dependency_provider.add_dependencies(
dependency_provider.add_dependencies(
"foo", (1, 0, 0),
[
("a", Ranges::from_range_bounds((1, 0, 0)..(2, 0, 0))),
Expand All @@ -27,7 +28,7 @@ fn main() {
);
#[rustfmt::skip]
// foo 1.1.0 depends on x ^1.0.0 and y ^1.0.0
dependency_provider.add_dependencies(
dependency_provider.add_dependencies(
"foo", (1, 1, 0),
[
("x", Ranges::from_range_bounds((1, 0, 0)..(2, 0, 0))),
Expand All @@ -36,7 +37,7 @@ fn main() {
);
#[rustfmt::skip]
// a 1.0.0 depends on b ^2.0.0
dependency_provider.add_dependencies(
dependency_provider.add_dependencies(
"a", (1, 0, 0),
[("b", Ranges::from_range_bounds((2, 0, 0)..(3, 0, 0)))],
);
Expand All @@ -45,7 +46,7 @@ fn main() {
dependency_provider.add_dependencies("b", (2, 0, 0), []);
#[rustfmt::skip]
// x 1.0.0 depends on y ^2.0.0.
dependency_provider.add_dependencies(
dependency_provider.add_dependencies(
"x", (1, 0, 0),
[("y", Ranges::from_range_bounds((2, 0, 0)..(3, 0, 0)))],
);
Expand All @@ -54,11 +55,14 @@ fn main() {
dependency_provider.add_dependencies("y", (2, 0, 0), []);

// Run the algorithm.
match resolve(&dependency_provider, "root", (1, 0, 0)) {
match dependency_provider.resolve("root", (1, 0, 0)) {
Ok(sol) => println!("{:?}", sol),
Err(PubGrubError::NoSolution(mut derivation_tree)) => {
derivation_tree.collapse_no_versions();
eprintln!("{}", DefaultStringReporter::report(&derivation_tree));
Err(PubGrubError::NoSolution(mut error)) => {
error.derivation_tree.collapse_no_versions();
eprintln!(
"{}",
DefaultStringReporter::report(&error, &dependency_provider)
);
std::process::exit(1);
}
Err(err) => panic!("{:?}", err),
Expand Down
Loading

0 comments on commit b0ddb5f

Please sign in to comment.