Skip to content

Commit 3695b2f

Browse files
committed
Use a shared cargo directory
This will drastically improve: - Speed: Not everything will need to be recompiled once making a minor change to a script. - Size: Will avoid downloading and building dependencies for each minor edit.
1 parent d87e407 commit 3695b2f

File tree

3 files changed

+42
-67
lines changed

3 files changed

+42
-67
lines changed

src/consts.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,12 @@ where F: FnMut(&str, usize) -> T, T: 'static {
125125
}
126126
"#;
127127

128-
/// Substitution for the identifier-safe name of the script.
128+
/// Substitution for the identifier-safe package name of the script.
129129
pub const MANI_NAME_SUB: &str = "name";
130130

131+
/// Substitution for the identifier-safe bin name of the script.
132+
pub const MANI_BIN_NAME_SUB: &str = "bin_name";
133+
131134
/// Substitution for the filesystem-safe name of the script.
132135
pub const MANI_FILE_SUB: &str = "file";
133136

@@ -142,7 +145,7 @@ authors = ["Anonymous"]
142145
edition = "2018"
143146
144147
[[bin]]
145-
name = "#{name}"
148+
name = "#{bin_name}"
146149
path = "#{file}.rs"
147150
"##;
148151

@@ -161,7 +164,7 @@ When generating a package's unique ID, how many hex nibbles of the digest should
161164
162165
The largest meaningful value is `40`.
163166
*/
164-
pub const ID_DIGEST_LEN_MAX: usize = 16;
167+
pub const ID_DIGEST_LEN_MAX: usize = 24;
165168

166169
/**
167170
How old can stuff in the cache be before we automatically clear it out?

src/main.rs

Lines changed: 21 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ extern crate lazy_static;
55
#[macro_use]
66
extern crate log;
77

8-
/**
9-
If this is set to `true`, the digests used for package IDs will be replaced with "stub" to make testing a bit easier. Obviously, you don't want this `true` for release...
10-
*/
11-
const STUB_HASHES: bool = false;
12-
138
/**
149
If this is set to `false`, then code that automatically deletes stuff *won't*.
1510
*/
@@ -637,9 +632,6 @@ This represents what to do with the input provided by the user.
637632
*/
638633
#[derive(Debug)]
639634
struct InputAction {
640-
/// Compile the input into a fresh executable?
641-
compile: bool,
642-
643635
/// Always show cargo output?
644636
cargo_output: bool,
645637

@@ -737,13 +729,6 @@ struct PackageMetadata {
737729
script_hash: String,
738730
}
739731

740-
impl PackageMetadata {
741-
pub fn sha1_hash(&self) -> String {
742-
// Yes, I *do* feel dirty for doing it like this. :D
743-
hash_str(&format!("{:?}", self))
744-
}
745-
}
746-
747732
/**
748733
For the given input, this constructs the package metadata and checks the cache to see what should be done.
749734
*/
@@ -753,28 +738,26 @@ fn decide_action_for(
753738
prelude: Vec<String>,
754739
args: &Args,
755740
) -> MainResult<InputAction> {
741+
let input_id = {
742+
let deps_iter = deps.iter().map(|&(ref n, ref v)| (n as &str, v as &str));
743+
// Again, also fucked if we can't work this out.
744+
input.compute_id(deps_iter).unwrap()
745+
};
746+
info!("id: {:?}", input_id);
747+
756748
let (pkg_path, using_cache) = args
757749
.pkg_path
758750
.as_ref()
759751
.map(|p| (p.into(), false))
760752
.unwrap_or_else(|| {
761753
// This can't fail. Seriously, we're *fucked* if we can't work this out.
762754
let cache_path = platform::generated_projects_cache_path().unwrap();
763-
764-
let id = {
765-
let deps_iter = deps.iter().map(|&(ref n, ref v)| (n as &str, v as &str));
766-
767-
// Again, also fucked if we can't work this out.
768-
input.compute_id(deps_iter).unwrap()
769-
};
770-
info!("id: {:?}", id);
771-
772-
(cache_path.join(&id), true)
755+
(cache_path.join(&input_id), true)
773756
});
774757
info!("pkg_path: {:?}", pkg_path);
775758
info!("using_cache: {:?}", using_cache);
776759

777-
let (mani_str, script_str) = manifest::split_input(input, &deps, &prelude)?;
760+
let (mani_str, script_str) = manifest::split_input(input, &deps, &prelude, &input_id)?;
778761

779762
// Forcibly override some flags based on build kind.
780763
let (debug, force) = match args.build_kind {
@@ -806,7 +789,6 @@ fn decide_action_for(
806789
info!("input_meta: {:?}", input_meta);
807790

808791
let mut action = InputAction {
809-
compile: force,
810792
cargo_output: args.cargo_output,
811793
force_compile: force,
812794
emit_metadata: true,
@@ -832,38 +814,29 @@ fn decide_action_for(
832814

833815
// If we were told to only generate the package, we need to stop *now*
834816
if args.gen_pkg_only {
835-
bail!(compile: false, execute: false)
817+
bail!(execute: false)
836818
}
837819

838820
// If we're not doing a regular build, stop.
839821
match action.build_kind {
840822
BuildKind::Normal => (),
841823
BuildKind::Test | BuildKind::Bench => {
842824
info!("not recompiling because: user asked for test/bench");
843-
bail!(compile: false, force_compile: false)
825+
bail!(force_compile: false)
844826
}
845827
}
846828

847-
let cache_meta = match get_pkg_metadata(&action.pkg_path) {
848-
Ok(meta) => meta,
829+
action.old_metadata = match get_pkg_metadata(&action.pkg_path) {
830+
Ok(meta) => Some(meta),
849831
Err(err) => {
850832
info!(
851833
"recompiling since failed to load metadata: {}",
852834
err.to_string()
853835
);
854-
bail!(compile: true)
836+
None
855837
}
856838
};
857839

858-
if cache_meta != action.metadata {
859-
info!("recompiling because: metadata did not match");
860-
debug!("input metadata: {:?}", action.metadata);
861-
debug!("cache metadata: {:?}", cache_meta);
862-
bail!(old_metadata: Some(cache_meta), compile: true)
863-
}
864-
865-
action.old_metadata = Some(cache_meta);
866-
867840
Ok(action)
868841
}
869842

@@ -1063,7 +1036,7 @@ impl<'a> Input<'a> {
10631036
};
10641037

10651038
match *self {
1066-
File(name, path, _, _) => {
1039+
File(_, path, _, _) => {
10671040
let mut hasher = Sha1::new();
10681041

10691042
// Hash the path to the script.
@@ -1072,10 +1045,7 @@ impl<'a> Input<'a> {
10721045
digest.truncate(consts::ID_DIGEST_LEN_MAX);
10731046

10741047
let mut id = OsString::new();
1075-
id.push("file-");
1076-
id.push(name);
1077-
id.push("-");
1078-
id.push(if STUB_HASHES { "stub" } else { &*digest });
1048+
id.push(&*digest );
10791049
Ok(id)
10801050
}
10811051
Expr(content, template) => {
@@ -1090,8 +1060,7 @@ impl<'a> Input<'a> {
10901060
digest.truncate(consts::ID_DIGEST_LEN_MAX);
10911061

10921062
let mut id = OsString::new();
1093-
id.push("expr-");
1094-
id.push(if STUB_HASHES { "stub" } else { &*digest });
1063+
id.push(&*digest);
10951064
Ok(id)
10961065
}
10971066
Loop(content, count) => {
@@ -1106,8 +1075,7 @@ impl<'a> Input<'a> {
11061075
digest.truncate(consts::ID_DIGEST_LEN_MAX);
11071076

11081077
let mut id = OsString::new();
1109-
id.push("loop-");
1110-
id.push(if STUB_HASHES { "stub" } else { &*digest });
1078+
id.push(&*digest);
11111079
Ok(id)
11121080
}
11131081
}
@@ -1176,12 +1144,11 @@ fn cargo(
11761144
cmd.arg("--color").arg("always");
11771145
}
11781146

1179-
let script_specific_binary_cache = format!(
1180-
"{}/{}",
1147+
let cargo_target_dir = format!(
1148+
"{}",
11811149
platform::binary_cache_path()?.display(),
1182-
meta.sha1_hash()
11831150
);
1184-
cmd.env("CARGO_TARGET_DIR", script_specific_binary_cache);
1151+
cmd.env("CARGO_TARGET_DIR", cargo_target_dir);
11851152

11861153
// Block `--release` on `bench`.
11871154
if !meta.debug && cmd_name != "bench" {

src/manifest.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::consts;
1212
use crate::error::{MainError, MainResult};
1313
use crate::templates;
1414
use crate::Input;
15+
use std::ffi::OsString;
1516

1617
lazy_static! {
1718
static ref RE_SHORT_MANIFEST: Regex =
@@ -41,6 +42,7 @@ pub fn split_input(
4142
input: &Input,
4243
deps: &[(String, String)],
4344
prelude_items: &[String],
45+
input_id: &OsString,
4446
) -> MainResult<(String, String)> {
4547
let template_buf;
4648
let (part_mani, source, template, sub_prelude) = match *input {
@@ -98,7 +100,7 @@ pub fn split_input(
98100
info!("part_mani: {:?}", part_mani);
99101

100102
// It's-a mergin' time!
101-
let def_mani = default_manifest(input)?;
103+
let def_mani = default_manifest(input, input_id)?;
102104
let dep_mani = deps_manifest(deps)?;
103105

104106
let mani = merge_manifest(def_mani, part_mani)?;
@@ -116,9 +118,10 @@ pub fn split_input(
116118

117119
#[test]
118120
fn test_split_input() {
121+
let input_id = OsString::from("input_id");
119122
macro_rules! si {
120123
($i:expr) => {
121-
split_input(&$i, &[], &[]).ok()
124+
split_input(&$i, &[], &[], &input_id).ok()
122125
};
123126
}
124127

@@ -136,7 +139,7 @@ fn test_split_input() {
136139
si!(f(r#"fn main() {}"#)),
137140
r!(
138141
r#"[[bin]]
139-
name = "n"
142+
name = "n_input_id"
140143
path = "n.rs"
141144
142145
[dependencies]
@@ -159,7 +162,7 @@ fn main() {}
159162
"#)),
160163
r!(
161164
r#"[[bin]]
162-
name = "n"
165+
name = "n_input_id"
163166
path = "n.rs"
164167
165168
[dependencies]
@@ -185,7 +188,7 @@ fn main() {}
185188
"#)),
186189
r!(
187190
r#"[[bin]]
188-
name = "n"
191+
name = "n_input_id"
189192
path = "n.rs"
190193
191194
[dependencies]
@@ -211,7 +214,7 @@ fn main() {}
211214
"#)),
212215
r!(
213216
r#"[[bin]]
214-
name = "n"
217+
name = "n_input_id"
215218
path = "n.rs"
216219
217220
[dependencies]
@@ -237,7 +240,7 @@ fn main() {}
237240
"#)),
238241
r!(
239242
r#"[[bin]]
240-
name = "n"
243+
name = "n_input_id"
241244
path = "n.rs"
242245
243246
[dependencies]
@@ -271,7 +274,7 @@ fn main() {}
271274
"#)),
272275
r!(
273276
r#"[[bin]]
274-
name = "n"
277+
name = "n_input_id"
275278
path = "n.rs"
276279
277280
[dependencies]
@@ -1015,11 +1018,13 @@ time = "*"
10151018
/**
10161019
Generates a default Cargo manifest for the given input.
10171020
*/
1018-
fn default_manifest(input: &Input) -> MainResult<toml::value::Table> {
1021+
fn default_manifest(input: &Input, input_id: &OsString) -> MainResult<toml::value::Table> {
10191022
let mani_str = {
10201023
let pkg_name = input.package_name();
1021-
let mut subs = HashMap::with_capacity(2);
1024+
let bin_name = format!("{}_{}", &*pkg_name, input_id.to_str().unwrap());
1025+
let mut subs = HashMap::with_capacity(3);
10221026
subs.insert(consts::MANI_NAME_SUB, &*pkg_name);
1027+
subs.insert(consts::MANI_BIN_NAME_SUB, &*bin_name);
10231028
subs.insert(consts::MANI_FILE_SUB, &input.safe_name()[..]);
10241029
templates::expand(consts::DEFAULT_MANIFEST, &subs)?
10251030
};

0 commit comments

Comments
 (0)