Skip to content

Commit 67b3b44

Browse files
nipunn1313alexcrichton
authored andcommitted
DP Cache target_metadata. Update all the lifetimes
1 parent d2ca374 commit 67b3b44

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

src/cargo/ops/cargo_rustc/context.rs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ pub struct Context<'a, 'cfg: 'a> {
5555
host_info: TargetInfo,
5656
profiles: &'a Profiles,
5757
incremental_enabled: bool,
58+
5859
target_filenames: HashMap<Unit<'a>, Arc<Vec<(PathBuf, Option<PathBuf>, bool)>>>,
60+
target_metadatas: HashMap<Unit<'a>, Option<Metadata>>,
5961
}
6062

6163
#[derive(Clone, Default)]
@@ -154,7 +156,10 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
154156
used_in_plugin: HashSet::new(),
155157
incremental_enabled: incremental_enabled,
156158
jobserver: jobserver,
159+
160+
// TODO: Pre-Calculate these with a topo-sort, rather than lazy-calculating
157161
target_filenames: HashMap::new(),
162+
target_metadatas: HashMap::new(),
158163
})
159164
}
160165

@@ -362,21 +367,21 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
362367

363368
/// Returns the directory for the specified unit where fingerprint
364369
/// information is stored.
365-
pub fn fingerprint_dir(&mut self, unit: &Unit) -> PathBuf {
370+
pub fn fingerprint_dir(&mut self, unit: &Unit<'a>) -> PathBuf {
366371
let dir = self.pkg_dir(unit);
367372
self.layout(unit.kind).fingerprint().join(dir)
368373
}
369374

370375
/// Returns the appropriate directory layout for either a plugin or not.
371-
pub fn build_script_dir(&mut self, unit: &Unit) -> PathBuf {
376+
pub fn build_script_dir(&mut self, unit: &Unit<'a>) -> PathBuf {
372377
assert!(unit.target.is_custom_build());
373378
assert!(!unit.profile.run_custom_build);
374379
let dir = self.pkg_dir(unit);
375380
self.layout(Kind::Host).build().join(dir)
376381
}
377382

378383
/// Returns the appropriate directory layout for either a plugin or not.
379-
pub fn build_script_out_dir(&mut self, unit: &Unit) -> PathBuf {
384+
pub fn build_script_out_dir(&mut self, unit: &Unit<'a>) -> PathBuf {
380385
assert!(unit.target.is_custom_build());
381386
assert!(unit.profile.run_custom_build);
382387
let dir = self.pkg_dir(unit);
@@ -394,7 +399,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
394399

395400
/// Returns the appropriate output directory for the specified package and
396401
/// target.
397-
pub fn out_dir(&mut self, unit: &Unit) -> PathBuf {
402+
pub fn out_dir(&mut self, unit: &Unit<'a>) -> PathBuf {
398403
if unit.profile.doc {
399404
self.layout(unit.kind).root().parent().unwrap().join("doc")
400405
} else if unit.target.is_custom_build() {
@@ -406,7 +411,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
406411
}
407412
}
408413

409-
fn pkg_dir(&mut self, unit: &Unit) -> String {
414+
fn pkg_dir(&mut self, unit: &Unit<'a>) -> String {
410415
let name = unit.pkg.package_id().name();
411416
match self.target_metadata(unit) {
412417
Some(meta) => format!("{}-{}", name, meta),
@@ -440,7 +445,17 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
440445
/// We build to the path: "{filename}-{target_metadata}"
441446
/// We use a linking step to link/copy to a predictable filename
442447
/// like `target/debug/libfoo.{a,so,rlib}` and such.
443-
pub fn target_metadata(&mut self, unit: &Unit) -> Option<Metadata> {
448+
pub fn target_metadata(&mut self, unit: &Unit<'a>) -> Option<Metadata> {
449+
if let Some(cache) = self.target_metadatas.get(unit) {
450+
return cache.clone()
451+
}
452+
453+
let metadata = self.calc_target_metadata(unit);
454+
self.target_metadatas.insert(*unit, metadata.clone());
455+
metadata
456+
}
457+
458+
fn calc_target_metadata(&mut self, unit: &Unit<'a>) -> Option<Metadata> {
444459
// No metadata for dylibs because of a couple issues
445460
// - OSX encodes the dylib name in the executable
446461
// - Windows rustc multiple files of which we can't easily link all of them
@@ -521,7 +536,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
521536
}
522537

523538
/// Returns the file stem for a given target/profile combo (with metadata)
524-
pub fn file_stem(&mut self, unit: &Unit) -> String {
539+
pub fn file_stem(&mut self, unit: &Unit<'a>) -> String {
525540
match self.target_metadata(unit) {
526541
Some(ref metadata) => format!("{}-{}", unit.target.crate_name(),
527542
metadata),
@@ -546,7 +561,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
546561
547562
/// Returns an Option because in some cases we don't want to link
548563
/// (eg a dependent lib)
549-
pub fn link_stem(&mut self, unit: &Unit) -> Option<(PathBuf, String)> {
564+
pub fn link_stem(&mut self, unit: &Unit<'a>) -> Option<(PathBuf, String)> {
550565
let src_dir = self.out_dir(unit);
551566
let bin_stem = self.bin_stem(unit);
552567
let file_stem = self.file_stem(unit);
@@ -587,6 +602,15 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
587602
return Ok(cache.clone())
588603
}
589604

605+
let result = self.calc_target_filenames(unit);
606+
if let Ok(ref ret) = result {
607+
self.target_filenames.insert(*unit, ret.clone());
608+
}
609+
result
610+
}
611+
612+
fn calc_target_filenames(&mut self, unit: &Unit<'a>)
613+
-> CargoResult<Arc<Vec<(PathBuf, Option<PathBuf>, bool)>>> {
590614
let out_dir = self.out_dir(unit);
591615
let stem = self.file_stem(unit);
592616
let link_stem = self.link_stem(unit);
@@ -668,9 +692,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
668692
}
669693
info!("Target filenames: {:?}", ret);
670694

671-
let ret = Arc::new(ret);
672-
self.target_filenames.insert(*unit, ret.clone());
673-
Ok(ret)
695+
Ok(Arc::new(ret))
674696
}
675697

676698
fn used_deps(&self, unit: &Unit<'a>) -> CargoResult<Vec<Unit<'a>>> {

src/cargo/ops/cargo_rustc/fingerprint.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ fn write_fingerprint(loc: &Path, fingerprint: &Fingerprint) -> CargoResult<()> {
538538
}
539539

540540
/// Prepare for work when a package starts to build
541-
pub fn prepare_init(cx: &mut Context, unit: &Unit) -> CargoResult<()> {
541+
pub fn prepare_init<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult<()> {
542542
let new1 = cx.fingerprint_dir(unit);
543543

544544
if fs::metadata(&new1).is_err() {
@@ -548,7 +548,7 @@ pub fn prepare_init(cx: &mut Context, unit: &Unit) -> CargoResult<()> {
548548
Ok(())
549549
}
550550

551-
pub fn dep_info_loc(cx: &mut Context, unit: &Unit) -> PathBuf {
551+
pub fn dep_info_loc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> PathBuf {
552552
cx.fingerprint_dir(unit).join(&format!("dep-{}", filename(cx, unit)))
553553
}
554554

@@ -670,7 +670,7 @@ fn mtime_if_fresh<I>(output: &Path, paths: I) -> Option<FileTime>
670670
}
671671
}
672672

673-
fn filename(cx: &mut Context, unit: &Unit) -> String {
673+
fn filename<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> String {
674674
// file_stem includes metadata hash. Thus we have a different
675675
// fingerprint for every metadata hash version. This works because
676676
// even if the package is fresh, we'll still link the fresh target

src/cargo/ops/cargo_rustc/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -684,10 +684,10 @@ fn root_path(cx: &Context, unit: &Unit) -> PathBuf {
684684
}
685685
}
686686

687-
fn build_base_args(cx: &mut Context,
688-
cmd: &mut ProcessBuilder,
689-
unit: &Unit,
690-
crate_types: &[&str]) {
687+
fn build_base_args<'a, 'cfg>(cx: &mut Context<'a, 'cfg>,
688+
cmd: &mut ProcessBuilder,
689+
unit: &Unit<'a>,
690+
crate_types: &[&str]) {
691691
let Profile {
692692
ref opt_level, lto, codegen_units, ref rustc_args, debuginfo,
693693
debug_assertions, overflow_checks, rpath, test, doc: _doc,

0 commit comments

Comments
 (0)