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 #5118

Merged
merged 1 commit into from
Mar 4, 2018
Merged
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
10 changes: 6 additions & 4 deletions src/cargo/core/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ struct Context {
warnings: RcList<String>,
}

type Activations = HashMap<String, HashMap<SourceId, Vec<Summary>>>;
type Activations = HashMap<String, HashMap<SourceId, Rc<Vec<Summary>>>>;

/// Builds the list of all packages required to build the first argument.
pub fn resolve(summaries: &[(Summary, Method)],
Expand Down Expand Up @@ -1287,15 +1287,17 @@ impl Context {
.entry(id.name().to_string())
.or_insert_with(HashMap::new)
.entry(id.source_id().clone())
.or_insert_with(Vec::new);
.or_insert_with(||Rc::new(Vec::new()));
if !prev.iter().any(|c| c == summary) {
self.resolve_graph.push(GraphNode::Add(id.clone()));
if let Some(link) = summary.links() {
ensure!(self.links.insert(link.to_owned(), id.clone()).is_none(),
"Attempting to resolve a with more then one crate with the links={}. \n\
This will not build as is. Consider rebuilding the .lock file.", link);
}
prev.push(summary.clone());
let mut inner: Vec<_> = (**prev).clone();
inner.push(summary.clone());
*prev = Rc::new(inner);
Copy link
Member

Choose a reason for hiding this comment

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

If we're optimizing for clones here, I wonder if this'd be better managed through an RcList perhaps?

Copy link
Contributor Author

@Eh2406 Eh2406 Mar 4, 2018

Choose a reason for hiding this comment

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

I was thinking of that, but we very often iter over the activated items. Specifically is_active, prev_active, and flag_activated. So it would be a pretty invasive change. I can give it a try if you want.

Also before we cloned every vec for every clone of Context for # clone vecs = O(# activated * ticks). Now we are cloning a vec each time we activate for # clone vecs = O(# activated). If I am miss reading it please correct me.

Copy link
Member

Choose a reason for hiding this comment

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

Ah an excellent point!

return Ok(false)
}
debug!("checking if {} is already activated", summary.package_id());
Expand Down Expand Up @@ -1463,7 +1465,7 @@ fn check_cycles(resolve: &Resolve, activations: &Activations)
-> CargoResult<()> {
let summaries: HashMap<&PackageId, &Summary> = activations.values()
.flat_map(|v| v.values())
.flat_map(|v| v)
.flat_map(|v| v.iter())
.map(|s| (s.package_id(), s))
.collect();

Expand Down