Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small resolver cleanups #15040

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 13 additions & 12 deletions src/cargo/core/resolver/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use super::errors::ActivateResult;
use super::types::{ActivationsKey, ConflictMap, ConflictReason, FeaturesSet, ResolveOpts};
use super::RequestedFeatures;
use crate::core::{Dependency, PackageId, Summary};
use crate::util::interning::InternedString;
use crate::util::interning::{InternedString, INTERNED_DEFAULT};
use crate::util::Graph;
use anyhow::format_err;
use std::collections::HashMap;
use std::collections::{BTreeSet, HashMap};
use tracing::debug;

// A `Context` is basically a bunch of local resolution information which is
Expand Down Expand Up @@ -121,6 +121,7 @@ impl ResolverContext {
}
}
debug!("checking if {} is already activated", summary.package_id());
let empty_features = BTreeSet::new();
match &opts.features {
// This returns `false` for CliFeatures just for simplicity. It
// would take a bit of work to compare since they are not in the
Expand All @@ -135,16 +136,16 @@ impl ResolverContext {
features,
uses_default_features,
} => {
let has_default_feature = summary.features().contains_key("default");
Ok(match self.resolve_features.get(&id) {
Some(prev) => {
features.is_subset(prev)
&& (!uses_default_features
|| prev.contains("default")
|| !has_default_feature)
}
None => features.is_empty() && (!uses_default_features || !has_default_feature),
})
let has_default_feature = summary.features().contains_key(&INTERNED_DEFAULT);
let prev = self
.resolve_features
.get(&id)
.map(|f| &**f)
.unwrap_or(&empty_features);
Ok(features.is_subset(prev)
&& (!uses_default_features
|| prev.contains(&INTERNED_DEFAULT)
|| !has_default_feature))
}
}
}
Expand Down
38 changes: 18 additions & 20 deletions src/cargo/core/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
//! over the place.

use std::collections::{BTreeMap, HashMap, HashSet};
use std::mem;
use std::rc::Rc;
use std::time::{Duration, Instant};

Expand Down Expand Up @@ -142,9 +141,7 @@ pub fn resolve(
let mut past_conflicting_activations = conflict_cache::ConflictCache::new();

let resolver_ctx = loop {
let resolver_ctx = ResolverContext::new();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this more, I wonder if this was a (failed) attempt to reuse memory. Perhaps let mut resolver_ctx = ResolverContext::new(); outside the loop and resolver_ctx.clear() inside the loop was the original intent.

let resolver_ctx = activate_deps_loop(
resolver_ctx,
&mut registry,
summaries,
first_version,
Expand Down Expand Up @@ -199,13 +196,13 @@ pub fn resolve(
/// If all dependencies can be activated and resolved to a version in the
/// dependency graph, `cx` is returned.
fn activate_deps_loop(
mut resolver_ctx: ResolverContext,
registry: &mut RegistryQueryer<'_>,
summaries: &[(Summary, ResolveOpts)],
first_version: Option<VersionOrdering>,
gctx: Option<&GlobalContext>,
past_conflicting_activations: &mut conflict_cache::ConflictCache,
) -> CargoResult<ResolverContext> {
let mut resolver_ctx = ResolverContext::new();
let mut backtrack_stack = Vec::new();
let mut remaining_deps = RemainingDeps::new();

Expand Down Expand Up @@ -759,22 +756,8 @@ impl RemainingCandidates {
) -> Option<(Summary, bool)> {
for b in self.remaining.iter() {
let b_id = b.package_id();
// The `links` key in the manifest dictates that there's only one
// package in a dependency graph, globally, with that particular
// `links` key. If this candidate links to something that's already
// linked to by a different package then we've gotta skip this.
if let Some(link) = b.links() {
if let Some(&a) = cx.links.get(&link) {
if a != b_id {
conflicting_prev_active
.entry(a)
.or_insert_with(|| ConflictReason::Links(link));
continue;
}
}
}

// Otherwise the condition for being a valid candidate relies on
// The condition for being a valid candidate relies on
// semver. Cargo dictates that you can't duplicate multiple
// semver-compatible versions of a crate. For example we can't
// simultaneously activate `foo 1.0.2` and `foo 1.2.0`. We can,
Expand All @@ -791,12 +774,27 @@ impl RemainingCandidates {
}
}

// Otherwise the `links` key in the manifest dictates that there's only one
weihanglo marked this conversation as resolved.
Show resolved Hide resolved
// package in a dependency graph, globally, with that particular
// `links` key. If this candidate links to something that's already
// linked to by a different package then we've gotta skip this.
if let Some(link) = b.links() {
if let Some(&a) = cx.links.get(&link) {
if a != b_id {
conflicting_prev_active
.entry(a)
.or_insert_with(|| ConflictReason::Links(link));
continue;
}
}
}

// Well if we made it this far then we've got a valid dependency. We
// want this iterator to be inherently "peekable" so we don't
// necessarily return the item just yet. Instead we stash it away to
// get returned later, and if we replaced something then that was
// actually the candidate to try first so we return that.
if let Some(r) = mem::replace(&mut self.has_another, Some(b.clone())) {
if let Some(r) = self.has_another.replace(b.clone()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unused import: `std::mem`
  --> src/cargo/core/resolver/mod.rs:62:5
   |
62 | use std::mem;
   |     ^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

warning: `cargo` (lib) generated 1 warning (run `cargo fix --lib -p cargo` to apply 1 suggestion)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's what I get for trying to manually cherry pick.

return Some((r, true));
}
}
Expand Down
Loading