Skip to content

Commit

Permalink
Use smallvec crate in pubgrub
Browse files Browse the repository at this point in the history
  • Loading branch information
x-hgg-x committed Nov 27, 2024
1 parent 0974ddb commit bda6b97
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 248 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
12 changes: 7 additions & 5 deletions src/internal/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
use std::sync::Arc;

use smallvec::SmallVec;

use crate::{
internal::{
Arena, DecisionLevel, IncompDpId, Incompatibility, PartialSolution, Relation,
SatisfierSearch, SmallVec,
SatisfierSearch,
},
DependencyProvider, DerivationTree, Map, PackageArena, PackageId, Set, Term, VersionIndex,
VersionSet,
Expand All @@ -26,7 +28,7 @@ pub(crate) struct State<DP: DependencyProvider> {
/// All incompatibilities expressing dependencies,
/// with common dependents merged.
#[allow(clippy::type_complexity)]
merged_dependencies: Map<(PackageId, PackageId), SmallVec<IncompDpId<DP>>>,
merged_dependencies: Map<(PackageId, PackageId), SmallVec<[IncompDpId<DP>; 4]>>,

/// Partial solution.
/// TODO: remove pub.
Expand All @@ -38,7 +40,7 @@ pub(crate) struct State<DP: DependencyProvider> {
/// This is a stack of work to be done in `unit_propagation`.
/// It can definitely be a local variable to that method, but
/// this way we can reuse the same allocation for better performance.
unit_propagation_buffer: SmallVec<PackageId>,
unit_propagation_buffer: Vec<PackageId>,
}

impl<DP: DependencyProvider> State<DP> {
Expand All @@ -57,7 +59,7 @@ impl<DP: DependencyProvider> State<DP> {
incompatibilities,
partial_solution: PartialSolution::empty(),
incompatibility_store,
unit_propagation_buffer: SmallVec::Empty,
unit_propagation_buffer: Vec::new(),
merged_dependencies: Map::default(),
}
}
Expand Down Expand Up @@ -254,7 +256,7 @@ impl<DP: DependencyProvider> State<DP> {
if let Some((pid1, pid2)) = self.incompatibility_store[id].as_dependency() {
// If we are a dependency, there's a good chance we can be merged with a previous dependency
let deps_lookup = self.merged_dependencies.entry((pid1, pid2)).or_default();
if let Some((past, merged)) = deps_lookup.as_mut_slice().iter_mut().find_map(|past| {
if let Some((past, merged)) = deps_lookup.iter_mut().find_map(|past| {
self.incompatibility_store[id]
.merge_dependents(&self.incompatibility_store[*past])
.map(|m| (past, m))
Expand Down
2 changes: 0 additions & 2 deletions src/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ mod core;
mod incompatibility;
mod partial_solution;
mod small_map;
mod small_vec;

pub(crate) use arena::{Arena, Id};
pub(crate) use core::State;
pub(crate) use incompatibility::{IncompDpId, IncompId, Incompatibility, Relation};
pub(crate) use partial_solution::{DecisionLevel, PartialSolution, SatisfierSearch};
pub(crate) use small_map::SmallMap;
pub(crate) use small_vec::SmallVec;
14 changes: 8 additions & 6 deletions src/internal/partial_solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ use std::hash::BuildHasherDefault;

use priority_queue::PriorityQueue;
use rustc_hash::FxHasher;
use smallvec::{smallvec, SmallVec};

use crate::Map;
use crate::{
internal::{Arena, IncompDpId, IncompId, Incompatibility, Relation, SmallMap, SmallVec},
internal::{Arena, IncompDpId, IncompId, Incompatibility, Relation, SmallMap},
DependencyProvider, FxIndexMap, PackageArena, PackageId, SelectedDependencies, Term,
VersionIndex, VersionSet,
};
Expand Down Expand Up @@ -97,7 +98,7 @@ impl<'a, DP: DependencyProvider> Display for PartialSolutionDisplay<'a, DP> {
struct PackageAssignments<M: Eq + Clone + Debug + Display> {
smallest_decision_level: DecisionLevel,
highest_decision_level: DecisionLevel,
dated_derivations: SmallVec<DatedDerivation<M>>,
dated_derivations: SmallVec<[DatedDerivation<M>; 1]>,
assignments_intersection: AssignmentsIntersection,
}

Expand Down Expand Up @@ -298,7 +299,7 @@ impl<DP: DependencyProvider> PartialSolution<DP> {
v.insert(PackageAssignments {
smallest_decision_level: self.current_decision_level,
highest_decision_level: self.current_decision_level,
dated_derivations: SmallVec::One([dated_derivation]),
dated_derivations: smallvec![dated_derivation],
assignments_intersection: AssignmentsIntersection::derivations(term),
});
}
Expand Down Expand Up @@ -599,14 +600,15 @@ impl<M: Eq + Clone + Debug + Display> PackageAssignments<M> {
start_term: Term,
package_store: &PackageArena<DP::P>,
) -> (Option<IncompId<M>>, u32, DecisionLevel) {
let empty = Term::empty();
// Indicate if we found a satisfier in the list of derivations, otherwise it will be the decision.
let idx = self
.dated_derivations
.as_slice()
.partition_point(|dd| !dd.accumulated_intersection.is_disjoint(start_term));
if let Some(dd) = self.dated_derivations.get(idx) {
debug_assert_eq!(dd.accumulated_intersection.intersection(start_term), empty);
debug_assert_eq!(
dd.accumulated_intersection.intersection(start_term),
Term::empty(),
);
return (Some(dd.cause), dd.global_index, dd.decision_level);
}
// If it wasn't found in the derivations, it must be the decision which is last (if called in the right context).
Expand Down
232 changes: 0 additions & 232 deletions src/internal/small_vec.rs

This file was deleted.

2 changes: 1 addition & 1 deletion version-ranges/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ impl<V: Ord> Ranges<V> {
/// See [`Ranges`] for the invariants checked.
fn check_invariants(self) -> Self {
if cfg!(debug_assertions) {
for p in self.segments.as_slice().windows(2) {
for p in self.segments.windows(2) {
assert!(end_before_start_with_gap(&p[0].1, &p[1].0));
}
for (s, e) in self.segments.iter() {
Expand Down

0 comments on commit bda6b97

Please sign in to comment.