Skip to content

Commit 0c891a0

Browse files
committed
Auto merge of #6853 - Eh2406:caching-the-dependency, r=alexcrichton
Caching the dependencies There are 2 sources of facts for the resolver: 1. The `Registry` tells us for a Dependency what versions are available to fulfil it. 2. The `Summary` tells us for a version (and features) what dependencies need to be fulfilled for it to be activated. The `Registry` was cached with a `RegistryQueryer` back in #5112. This adds a `DepsCache` to cache the calculation of which dependencies are activated by features. In the happy path `flag_activated` means that we don't get to reuse `build_deps`, but the more we backtrack the more time we save. In pathological cases like #6258 (comment), I have measured this as a 10% improvement with release. This also means that `build_deps` can be run in a context free way, which may be useful in a follow up PR to solve #6258 (comment).
2 parents c9330fe + 79714ba commit 0c891a0

File tree

14 files changed

+594
-542
lines changed

14 files changed

+594
-542
lines changed

src/cargo/core/compiler/fingerprint.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,7 @@ enum LocalFingerprint {
478478
/// The `dep_info` file, when present, also lists a number of other files
479479
/// for us to look at. If any of those files are newer than this file then
480480
/// we need to recompile.
481-
CheckDepInfo {
482-
dep_info: PathBuf,
483-
},
481+
CheckDepInfo { dep_info: PathBuf },
484482

485483
/// This represents a nonempty set of `rerun-if-changed` annotations printed
486484
/// out by a build script. The `output` file is a arelative file anchored at
@@ -500,10 +498,7 @@ enum LocalFingerprint {
500498
/// build script. The exact env var and value are hashed here. There's no
501499
/// filesystem dependence here, and if the values are changed the hash will
502500
/// change forcing a recompile.
503-
RerunIfEnvChanged {
504-
var: String,
505-
val: Option<String>,
506-
},
501+
RerunIfEnvChanged { var: String, val: Option<String> },
507502
}
508503

509504
enum StaleFile {
@@ -762,7 +757,7 @@ impl Fingerprint {
762757
let t = FileTime::from_system_time(SystemTime::now());
763758
drop(filetime::set_file_times(f, t, t));
764759
}
765-
return mtime;
760+
mtime
766761
})
767762
.min();
768763

@@ -1350,7 +1345,7 @@ fn compare_old_fingerprint(
13501345
debug_assert_eq!(util::to_hex(old_fingerprint.hash()), old_fingerprint_short);
13511346
let result = new_fingerprint.compare(&old_fingerprint);
13521347
assert!(result.is_err());
1353-
return result;
1348+
result
13541349
}
13551350

13561351
fn log_compare(unit: &Unit<'_>, compare: &CargoResult<()>) {
@@ -1444,7 +1439,7 @@ where
14441439
});
14451440
}
14461441

1447-
return None;
1442+
None
14481443
}
14491444

14501445
fn filename<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> String {

src/cargo/core/package_id_spec.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use semver::Version;
55
use serde::{de, ser};
66
use url::Url;
77

8+
use crate::core::interning::InternedString;
89
use crate::core::PackageId;
910
use crate::util::errors::{CargoResult, CargoResultExt};
1011
use crate::util::{validate_package_name, ToSemver, ToUrl};
@@ -20,7 +21,7 @@ use crate::util::{validate_package_name, ToSemver, ToUrl};
2021
/// sufficient to uniquely define a package ID.
2122
#[derive(Clone, PartialEq, Eq, Debug, Hash, Ord, PartialOrd)]
2223
pub struct PackageIdSpec {
23-
name: String,
24+
name: InternedString,
2425
version: Option<Version>,
2526
url: Option<Url>,
2627
}
@@ -66,7 +67,7 @@ impl PackageIdSpec {
6667
};
6768
validate_package_name(name, "pkgid", "")?;
6869
Ok(PackageIdSpec {
69-
name: name.to_string(),
70+
name: InternedString::new(name),
7071
version,
7172
url: None,
7273
})
@@ -86,7 +87,7 @@ impl PackageIdSpec {
8687
/// fields filled in.
8788
pub fn from_package_id(package_id: PackageId) -> PackageIdSpec {
8889
PackageIdSpec {
89-
name: package_id.name().to_string(),
90+
name: package_id.name(),
9091
version: Some(package_id.version().clone()),
9192
url: Some(package_id.source_id().url().clone()),
9293
}
@@ -117,19 +118,19 @@ impl PackageIdSpec {
117118
match parts.next() {
118119
Some(part) => {
119120
let version = part.to_semver()?;
120-
(name_or_version.to_string(), Some(version))
121+
(InternedString::new(name_or_version), Some(version))
121122
}
122123
None => {
123124
if name_or_version.chars().next().unwrap().is_alphabetic() {
124-
(name_or_version.to_string(), None)
125+
(InternedString::new(name_or_version), None)
125126
} else {
126127
let version = name_or_version.to_semver()?;
127-
(path_name.to_string(), Some(version))
128+
(InternedString::new(path_name), Some(version))
128129
}
129130
}
130131
}
131132
}
132-
None => (path_name.to_string(), None),
133+
None => (InternedString::new(path_name), None),
133134
}
134135
};
135136
Ok(PackageIdSpec {
@@ -139,8 +140,8 @@ impl PackageIdSpec {
139140
})
140141
}
141142

142-
pub fn name(&self) -> &str {
143-
&self.name
143+
pub fn name(&self) -> InternedString {
144+
self.name
144145
}
145146

146147
pub fn version(&self) -> Option<&Version> {
@@ -157,7 +158,7 @@ impl PackageIdSpec {
157158

158159
/// Checks whether the given `PackageId` matches the `PackageIdSpec`.
159160
pub fn matches(&self, package_id: PackageId) -> bool {
160-
if self.name() != &*package_id.name() {
161+
if self.name() != package_id.name() {
161162
return false;
162163
}
163164

@@ -234,7 +235,7 @@ impl fmt::Display for PackageIdSpec {
234235
} else {
235236
write!(f, "{}", url)?;
236237
}
237-
if url.path_segments().unwrap().next_back().unwrap() != self.name {
238+
if url.path_segments().unwrap().next_back().unwrap() != &*self.name {
238239
printed_name = true;
239240
write!(f, "#{}", self.name)?;
240241
}
@@ -273,6 +274,7 @@ impl<'de> de::Deserialize<'de> for PackageIdSpec {
273274
#[cfg(test)]
274275
mod tests {
275276
use super::PackageIdSpec;
277+
use crate::core::interning::InternedString;
276278
use crate::core::{PackageId, SourceId};
277279
use crate::util::ToSemver;
278280
use url::Url;
@@ -288,63 +290,63 @@ mod tests {
288290
ok(
289291
"https://crates.io/foo#1.2.3",
290292
PackageIdSpec {
291-
name: "foo".to_string(),
293+
name: InternedString::new("foo"),
292294
version: Some("1.2.3".to_semver().unwrap()),
293295
url: Some(Url::parse("https://crates.io/foo").unwrap()),
294296
},
295297
);
296298
ok(
297299
"https://crates.io/foo#bar:1.2.3",
298300
PackageIdSpec {
299-
name: "bar".to_string(),
301+
name: InternedString::new("bar"),
300302
version: Some("1.2.3".to_semver().unwrap()),
301303
url: Some(Url::parse("https://crates.io/foo").unwrap()),
302304
},
303305
);
304306
ok(
305307
"crates.io/foo",
306308
PackageIdSpec {
307-
name: "foo".to_string(),
309+
name: InternedString::new("foo"),
308310
version: None,
309311
url: Some(Url::parse("cargo://crates.io/foo").unwrap()),
310312
},
311313
);
312314
ok(
313315
"crates.io/foo#1.2.3",
314316
PackageIdSpec {
315-
name: "foo".to_string(),
317+
name: InternedString::new("foo"),
316318
version: Some("1.2.3".to_semver().unwrap()),
317319
url: Some(Url::parse("cargo://crates.io/foo").unwrap()),
318320
},
319321
);
320322
ok(
321323
"crates.io/foo#bar",
322324
PackageIdSpec {
323-
name: "bar".to_string(),
325+
name: InternedString::new("bar"),
324326
version: None,
325327
url: Some(Url::parse("cargo://crates.io/foo").unwrap()),
326328
},
327329
);
328330
ok(
329331
"crates.io/foo#bar:1.2.3",
330332
PackageIdSpec {
331-
name: "bar".to_string(),
333+
name: InternedString::new("bar"),
332334
version: Some("1.2.3".to_semver().unwrap()),
333335
url: Some(Url::parse("cargo://crates.io/foo").unwrap()),
334336
},
335337
);
336338
ok(
337339
"foo",
338340
PackageIdSpec {
339-
name: "foo".to_string(),
341+
name: InternedString::new("foo"),
340342
version: None,
341343
url: None,
342344
},
343345
);
344346
ok(
345347
"foo:1.2.3",
346348
PackageIdSpec {
347-
name: "foo".to_string(),
349+
name: InternedString::new("foo"),
348350
version: Some("1.2.3".to_semver().unwrap()),
349351
url: None,
350352
},

src/cargo/core/profiles.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ impl ProfileMaker {
282282
let name_matches: Vec<String> = packages
283283
.package_ids()
284284
.filter_map(|pkg_id| {
285-
if pkg_id.name().as_str() == spec.name() {
285+
if pkg_id.name() == spec.name() {
286286
Some(pkg_id.to_string())
287287
} else {
288288
None
@@ -292,7 +292,7 @@ impl ProfileMaker {
292292
if name_matches.is_empty() {
293293
let suggestion = packages
294294
.package_ids()
295-
.map(|p| (lev_distance(spec.name(), &p.name()), p.name()))
295+
.map(|p| (lev_distance(&*spec.name(), &p.name()), p.name()))
296296
.filter(|&(d, _)| d < 4)
297297
.min_by_key(|p| p.0)
298298
.map(|p| p.1);

src/cargo/core/resolver/conflict_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl ConflictCache {
234234
/// all the `PackageId` entries are activated.
235235
pub fn contains(&self, dep: &Dependency, con: &ConflictMap) -> bool {
236236
if let Some(cst) = self.con_from_dep.get(dep) {
237-
cst.contains(con.keys().cloned(), &con)
237+
cst.contains(con.keys().cloned(), con)
238238
} else {
239239
false
240240
}

0 commit comments

Comments
 (0)