Skip to content

Commit

Permalink
Intern PackageId
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Nov 26, 2018
1 parent 2e35475 commit 0593e1c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 46 deletions.
100 changes: 54 additions & 46 deletions src/cargo/core/package_id.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::cmp::Ordering;
use std::collections::HashSet;
use std::fmt::{self, Formatter};
use std::hash;
use std::hash::Hash;
use std::path::Path;
use std::sync::Arc;
use std::sync::Mutex;

use semver;
use serde::de;
Expand All @@ -13,19 +13,41 @@ use core::interning::InternedString;
use core::source::SourceId;
use util::{CargoResult, ToSemver};

lazy_static! {
static ref PACKAGE_ID_CACHE: Mutex<HashSet<&'static PackageIdInner>> = Mutex::new(HashSet::new());
}

/// Identifier for a specific version of a package in a specific source.
#[derive(Clone)]
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PackageId {
inner: Arc<PackageIdInner>,
inner: &'static PackageIdInner,
}

#[derive(PartialEq, PartialOrd, Eq, Ord)]
#[derive(PartialOrd, Eq, Ord)]
struct PackageIdInner {
name: InternedString,
version: semver::Version,
source_id: SourceId,
}

// Custom equality that uses full equality of SourceId, rather than its custom equality.
impl PartialEq for PackageIdInner {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& self.version == other.version
&& self.source_id.full_eq(&other.source_id)
}
}

// Custom hash that is coherent with the custom equality above.
impl Hash for PackageIdInner {
fn hash<S: hash::Hasher>(&self, into: &mut S) {
self.name.hash(into);
self.version.hash(into);
self.source_id.full_hash(into);
}
}

impl ser::Serialize for PackageId {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
Expand Down Expand Up @@ -64,51 +86,37 @@ impl<'de> de::Deserialize<'de> for PackageId {
};
let source_id = SourceId::from_url(url).map_err(de::Error::custom)?;

Ok(PackageId {
inner: Arc::new(PackageIdInner {
Ok(PackageId::wrap(
PackageIdInner {
name: InternedString::new(name),
version,
source_id,
}),
})
}
}

impl Hash for PackageId {
fn hash<S: hash::Hasher>(&self, state: &mut S) {
self.inner.name.hash(state);
self.inner.version.hash(state);
self.inner.source_id.hash(state);
}
}

impl PartialEq for PackageId {
fn eq(&self, other: &PackageId) -> bool {
(*self.inner).eq(&*other.inner)
}
}
impl PartialOrd for PackageId {
fn partial_cmp(&self, other: &PackageId) -> Option<Ordering> {
(*self.inner).partial_cmp(&*other.inner)
}
}
impl Eq for PackageId {}
impl Ord for PackageId {
fn cmp(&self, other: &PackageId) -> Ordering {
(*self.inner).cmp(&*other.inner)
}
))
}
}

impl PackageId {
pub fn new<T: ToSemver>(name: &str, version: T, sid: SourceId) -> CargoResult<PackageId> {
let v = version.to_semver()?;
Ok(PackageId {
inner: Arc::new(PackageIdInner {

Ok(PackageId::wrap(
PackageIdInner {
name: InternedString::new(name),
version: v,
source_id: sid,
}),
})
}
))
}

fn wrap(inner: PackageIdInner) -> PackageId {
let mut cache = PACKAGE_ID_CACHE.lock().unwrap();
let inner = cache.get(&inner).map(|&x| x).unwrap_or_else(|| {
let inner = Box::leak(Box::new(inner));
cache.insert(inner);
inner
});
PackageId { inner }
}

pub fn name(&self) -> InternedString {
Expand All @@ -122,23 +130,23 @@ impl PackageId {
}

pub fn with_precise(&self, precise: Option<String>) -> PackageId {
PackageId {
inner: Arc::new(PackageIdInner {
PackageId::wrap(
PackageIdInner {
name: self.inner.name,
version: self.inner.version.clone(),
source_id: self.inner.source_id.with_precise(precise),
}),
}
}
)
}

pub fn with_source_id(&self, source: SourceId) -> PackageId {
PackageId {
inner: Arc::new(PackageIdInner {
PackageId::wrap(
PackageIdInner {
name: self.inner.name,
version: self.inner.version.clone(),
source_id: source,
}),
}
}
)
}

pub fn stable_hash<'a>(&'a self, workspace: &'a Path) -> PackageIdStableHash<'a> {
Expand Down
8 changes: 8 additions & 0 deletions src/cargo/core/source/source_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ impl SourceId {
}
self.hash(into)
}

pub fn full_eq(&self, other: &SourceId) -> bool {
self.inner == other.inner
}

pub fn full_hash<S: hash::Hasher>(&self, into: &mut S) {
self.inner.hash(into)
}
}

impl PartialOrd for SourceId {
Expand Down

0 comments on commit 0593e1c

Please sign in to comment.