Skip to content

Commit

Permalink
Auto merge of #14260 - Eh2406:summary_sync, r=weihanglo
Browse files Browse the repository at this point in the history
make summary sync by using Arc not Rc

### What does this PR try to resolve?

For my PubGrub testing work I want to be able to read the entire crates.io index into memory and then run lots of resolution questions against that data in parallel. Currently cargoes `Summary` and `Dependency` types use `Rc` internally which prevents this pattern. Using `Arc` in cargo makes my life a lot easier! It does not noticeably slow down single threaded performance. (Measured by running my PubGrub testing in single threaded mode before and after.)

The team largely agreed to this at a meeting and in discussions https://rust-lang.zulipchat.com/#narrow/stream/246057-t-cargo/topic/Make.20summary.20sync

### How should we test and review this PR?

Tests still pass

### Additional information

Should be add a test a this is sync?
  • Loading branch information
bors committed Jul 17, 2024
2 parents a2b58c3 + c27579a commit 52a2630
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
38 changes: 19 additions & 19 deletions src/cargo/core/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::Serialize;
use std::borrow::Cow;
use std::fmt;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::Arc;
use tracing::trace;

use crate::core::compiler::{CompileKind, CompileTarget};
Expand All @@ -18,7 +18,7 @@ use crate::util::OptVersionReq;
/// Cheap to copy.
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub struct Dependency {
inner: Rc<Inner>,
inner: Arc<Inner>,
}

/// The data underlying a `Dependency`.
Expand Down Expand Up @@ -152,7 +152,7 @@ impl Dependency {

let mut ret = Dependency::new_override(name, source_id);
{
let ptr = Rc::make_mut(&mut ret.inner);
let ptr = Arc::make_mut(&mut ret.inner);
ptr.only_match_name = false;
ptr.req = version_req;
ptr.specified_req = specified_req;
Expand All @@ -163,7 +163,7 @@ impl Dependency {
pub fn new_override(name: InternedString, source_id: SourceId) -> Dependency {
assert!(!name.is_empty());
Dependency {
inner: Rc::new(Inner {
inner: Arc::new(Inner {
name,
source_id,
registry_id: None,
Expand Down Expand Up @@ -241,7 +241,7 @@ impl Dependency {
}

pub fn set_registry_id(&mut self, registry_id: SourceId) -> &mut Dependency {
Rc::make_mut(&mut self.inner).registry_id = Some(registry_id);
Arc::make_mut(&mut self.inner).registry_id = Some(registry_id);
self
}

Expand All @@ -259,7 +259,7 @@ impl Dependency {
// Setting 'public' only makes sense for normal dependencies
assert_eq!(self.kind(), DepKind::Normal);
}
Rc::make_mut(&mut self.inner).public = public;
Arc::make_mut(&mut self.inner).public = public;
self
}

Expand All @@ -286,7 +286,7 @@ impl Dependency {
// Setting 'public' only makes sense for normal dependencies
assert_eq!(kind, DepKind::Normal);
}
Rc::make_mut(&mut self.inner).kind = kind;
Arc::make_mut(&mut self.inner).kind = kind;
self
}

Expand All @@ -295,44 +295,44 @@ impl Dependency {
&mut self,
features: impl IntoIterator<Item = impl Into<InternedString>>,
) -> &mut Dependency {
Rc::make_mut(&mut self.inner).features = features.into_iter().map(|s| s.into()).collect();
Arc::make_mut(&mut self.inner).features = features.into_iter().map(|s| s.into()).collect();
self
}

/// Sets whether the dependency requests default features of the package.
pub fn set_default_features(&mut self, default_features: bool) -> &mut Dependency {
Rc::make_mut(&mut self.inner).default_features = default_features;
Arc::make_mut(&mut self.inner).default_features = default_features;
self
}

/// Sets whether the dependency is optional.
pub fn set_optional(&mut self, optional: bool) -> &mut Dependency {
Rc::make_mut(&mut self.inner).optional = optional;
Arc::make_mut(&mut self.inner).optional = optional;
self
}

/// Sets the source ID for this dependency.
pub fn set_source_id(&mut self, id: SourceId) -> &mut Dependency {
Rc::make_mut(&mut self.inner).source_id = id;
Arc::make_mut(&mut self.inner).source_id = id;
self
}

/// Sets the version requirement for this dependency.
pub fn set_version_req(&mut self, req: OptVersionReq) -> &mut Dependency {
Rc::make_mut(&mut self.inner).req = req;
Arc::make_mut(&mut self.inner).req = req;
self
}

pub fn set_platform(&mut self, platform: Option<Platform>) -> &mut Dependency {
Rc::make_mut(&mut self.inner).platform = platform;
Arc::make_mut(&mut self.inner).platform = platform;
self
}

pub fn set_explicit_name_in_toml(
&mut self,
name: impl Into<InternedString>,
) -> &mut Dependency {
Rc::make_mut(&mut self.inner).explicit_name_in_toml = Some(name.into());
Arc::make_mut(&mut self.inner).explicit_name_in_toml = Some(name.into());
self
}

Expand All @@ -346,7 +346,7 @@ impl Dependency {
self.source_id(),
id
);
let me = Rc::make_mut(&mut self.inner);
let me = Arc::make_mut(&mut self.inner);
me.req.lock_to(id.version());

// Only update the `precise` of this source to preserve other
Expand All @@ -361,7 +361,7 @@ impl Dependency {
/// Mainly used in dependency patching like `[patch]` or `[replace]`, which
/// doesn't need to lock the entire dependency to a specific [`PackageId`].
pub fn lock_version(&mut self, version: &semver::Version) -> &mut Dependency {
let me = Rc::make_mut(&mut self.inner);
let me = Arc::make_mut(&mut self.inner);
me.req.lock_to(version);
self
}
Expand Down Expand Up @@ -430,7 +430,7 @@ impl Dependency {
}

pub(crate) fn set_artifact(&mut self, artifact: Artifact) {
Rc::make_mut(&mut self.inner).artifact = Some(artifact);
Arc::make_mut(&mut self.inner).artifact = Some(artifact);
}

pub(crate) fn artifact(&self) -> Option<&Artifact> {
Expand All @@ -453,7 +453,7 @@ impl Dependency {
/// This information represents a requirement in the package this dependency refers to.
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub struct Artifact {
inner: Rc<Vec<ArtifactKind>>,
inner: Arc<Vec<ArtifactKind>>,
is_lib: bool,
target: Option<ArtifactTarget>,
}
Expand Down Expand Up @@ -492,7 +492,7 @@ impl Artifact {
.collect::<Result<Vec<_>, _>>()?,
)?;
Ok(Artifact {
inner: Rc::new(kinds),
inner: Arc::new(kinds),
is_lib,
target: target.map(ArtifactTarget::parse).transpose()?,
})
Expand Down
22 changes: 14 additions & 8 deletions src/cargo/core/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::mem;
use std::rc::Rc;
use std::sync::Arc;

/// Subset of a `Manifest`. Contains only the most important information about
/// a package.
///
/// Summaries are cloned, and should not be mutated after creation
#[derive(Debug, Clone)]
pub struct Summary {
inner: Rc<Inner>,
inner: Arc<Inner>,
}

#[derive(Debug, Clone)]
struct Inner {
package_id: PackageId,
dependencies: Vec<Dependency>,
features: Rc<FeatureMap>,
features: Arc<FeatureMap>,
checksum: Option<String>,
links: Option<InternedString>,
rust_version: Option<RustVersion>,
Expand Down Expand Up @@ -82,10 +82,10 @@ impl Summary {
}
let feature_map = build_feature_map(features, &dependencies)?;
Ok(Summary {
inner: Rc::new(Inner {
inner: Arc::new(Inner {
package_id: pkg_id,
dependencies,
features: Rc::new(feature_map),
features: Arc::new(feature_map),
checksum: None,
links: links.map(|l| l.into()),
rust_version,
Expand Down Expand Up @@ -124,12 +124,12 @@ impl Summary {
}

pub fn override_id(mut self, id: PackageId) -> Summary {
Rc::make_mut(&mut self.inner).package_id = id;
Arc::make_mut(&mut self.inner).package_id = id;
self
}

pub fn set_checksum(&mut self, cksum: String) {
Rc::make_mut(&mut self.inner).checksum = Some(cksum);
Arc::make_mut(&mut self.inner).checksum = Some(cksum);
}

pub fn map_dependencies<F>(self, mut f: F) -> Summary
Expand All @@ -144,7 +144,7 @@ impl Summary {
F: FnMut(Dependency) -> CargoResult<Dependency>,
{
{
let slot = &mut Rc::make_mut(&mut self.inner).dependencies;
let slot = &mut Arc::make_mut(&mut self.inner).dependencies;
*slot = mem::take(slot)
.into_iter()
.map(f)
Expand Down Expand Up @@ -178,6 +178,12 @@ impl Hash for Summary {
}
}

// A check that only compiles if Summary is Sync
const _: fn() = || {
fn is_sync<T: Sync>() {}
is_sync::<Summary>();
};

/// Checks features for errors, bailing out a CargoResult:Err if invalid,
/// and creates FeatureValues for each feature.
fn build_feature_map(
Expand Down

0 comments on commit 52a2630

Please sign in to comment.