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

use more Rc in the part of resolver that gets cloned a lot 2 #5302

Merged
merged 2 commits into from
Apr 6, 2018
Merged
Changes from 1 commit
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
13 changes: 8 additions & 5 deletions src/cargo/core/resolver/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct Context {
// switch to persistent hash maps if we can at some point or otherwise
// make these much cheaper to clone in general.
pub activations: Activations,
pub resolve_features: HashMap<PackageId, HashSet<InternedString>>,
pub resolve_features: HashMap<PackageId, Rc<HashSet<InternedString>>>,
pub links: HashMap<InternedString, PackageId>,

// These are two cheaply-cloneable lists (O(1) clone) which are effectively
Expand All @@ -36,6 +36,8 @@ pub struct Context {
pub warnings: RcList<String>,
}

pub type Activations = HashMap<(InternedString, SourceId), Rc<Vec<Summary>>>;

impl Context {
pub fn new() -> Context {
Context {
Expand Down Expand Up @@ -246,10 +248,13 @@ impl Context {

let set = self.resolve_features
.entry(pkgid.clone())
.or_insert_with(HashSet::new);
.or_insert_with(|| Rc::new(HashSet::new()));

let mut inner: HashSet<_> = (**set).clone();
Copy link
Member

Choose a reason for hiding this comment

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

Could this perhaps use Rc::make_mut? That way we could have a fast path as well for when the Context hasn't been cloned?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is an algorithmic improvement, I think HashSet.clone() is now O(# activated * ticks) and this pr bings it to O(# activated).

I can experiment with that, here and for #5118, but I suspect it will be rare. Not only does the context need not to be cloned, but it needs not to have been cloned since last time that this package was added. Would you like me to do that expremint in this PR or in a follow up?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did it in this PR as it made the diff much smaller. New performance numbers on the way.

for feature in reqs.used {
set.insert(InternedString::new(feature));
inner.insert(InternedString::new(feature));
}
*set = Rc::new(inner);
}

Ok(ret)
Expand Down Expand Up @@ -405,5 +410,3 @@ impl<'r> Requirements<'r> {
}
}
}

pub type Activations = HashMap<(InternedString, SourceId), Rc<Vec<Summary>>>;