Skip to content

Commit 8fef0eb

Browse files
committed
remove cache key and use present fields to generate hash
1 parent 76ee4cb commit 8fef0eb

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

src/bootstrap/src/utils/exec.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use std::collections::HashMap;
88
use std::ffi::OsStr;
99
use std::fmt::{Debug, Formatter};
10-
use std::hash::{Hash, Hasher};
10+
use std::hash::{DefaultHasher, Hash, Hasher};
1111
use std::path::Path;
1212
use std::process::{Command, CommandArgs, CommandEnvs, ExitStatus, Output, Stdio};
1313
use std::sync::Mutex;
@@ -55,14 +55,6 @@ impl OutputMode {
5555
}
5656
}
5757

58-
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
59-
pub struct CommandCacheKey {
60-
program: String,
61-
args: Vec<String>,
62-
envs: Vec<(String, String)>,
63-
cwd: Option<PathBuf>,
64-
}
65-
6658
/// Wrapper around `std::process::Command`.
6759
///
6860
/// By default, the command will exit bootstrap if it fails.
@@ -234,13 +226,13 @@ impl<'a> BootstrapCommand {
234226
}
235227
}
236228

237-
pub fn cache_key(&self) -> CommandCacheKey {
238-
CommandCacheKey {
239-
program: self.program.clone(),
240-
args: self.args.clone(),
241-
envs: self.envs.clone(),
242-
cwd: self.cwd.clone(),
243-
}
229+
pub fn compute_cache_hash(&self) -> u64 {
230+
let mut hasher = DefaultHasher::new();
231+
self.program.hash(&mut hasher);
232+
self.args.hash(&mut hasher);
233+
self.envs.hash(&mut hasher);
234+
self.cwd.hash(&mut hasher);
235+
hasher.finish()
244236
}
245237
}
246238

@@ -387,6 +379,26 @@ impl Default for CommandOutput {
387379
}
388380
}
389381

382+
impl Hash for BootstrapCommand {
383+
fn hash<H: Hasher>(&self, state: &mut H) {
384+
self.program.hash(state);
385+
self.args.hash(state);
386+
self.envs.hash(state);
387+
self.cwd.hash(state);
388+
}
389+
}
390+
391+
impl PartialEq for BootstrapCommand {
392+
fn eq(&self, other: &Self) -> bool {
393+
self.program == other.program
394+
&& self.args == other.args
395+
&& self.envs == other.envs
396+
&& self.cwd == other.cwd
397+
}
398+
}
399+
400+
impl Eq for BootstrapCommand {}
401+
390402
/// Helper trait to format both Command and BootstrapCommand as a short execution line,
391403
/// without all the other details (e.g. environment variables).
392404
#[cfg(feature = "tracing")]

src/bootstrap/src/utils/execution_context.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use std::sync::{Arc, Mutex};
1212
use crate::core::config::DryRun;
1313
#[cfg(feature = "tracing")]
1414
use crate::trace_cmd;
15-
use crate::utils::exec::CommandCacheKey;
1615
use crate::{BehaviorOnFailure, BootstrapCommand, CommandOutput, OutputMode, exit};
1716

1817
#[derive(Clone, Default)]
@@ -26,19 +25,19 @@ pub struct ExecutionContext {
2625

2726
#[derive(Default)]
2827
pub struct CommandCache {
29-
cache: Mutex<HashMap<CommandCacheKey, CommandOutput>>,
28+
cache: Mutex<HashMap<u64, CommandOutput>>,
3029
}
3130

3231
impl CommandCache {
3332
pub fn new() -> Self {
3433
Self { cache: Mutex::new(HashMap::new()) }
3534
}
3635

37-
pub fn get(&self, key: &CommandCacheKey) -> Option<CommandOutput> {
38-
self.cache.lock().unwrap().get(key).cloned()
36+
pub fn get(&self, key: u64) -> Option<CommandOutput> {
37+
self.cache.lock().unwrap().get(&key).cloned()
3938
}
4039

41-
pub fn insert(&self, key: CommandCacheKey, output: CommandOutput) {
40+
pub fn insert(&self, key: u64, output: CommandOutput) {
4241
self.cache.lock().unwrap().insert(key, output);
4342
}
4443
}
@@ -146,9 +145,9 @@ impl ExecutionContext {
146145
stdout: OutputMode,
147146
stderr: OutputMode,
148147
) -> CommandOutput {
149-
let cache_key = command.cache_key();
148+
let cache_key = command.compute_cache_hash();
150149

151-
if let Some(cached_output) = self.command_cache.get(&cache_key) {
150+
if let Some(cached_output) = self.command_cache.get(cache_key) {
152151
command.mark_as_executed();
153152
if self.dry_run() && !command.run_always {
154153
return CommandOutput::default();
@@ -159,9 +158,11 @@ impl ExecutionContext {
159158

160159
let output = self.start(command, stdout, stderr).wait_for_output(self);
161160

162-
if output != CommandOutput::default() {
163-
self.command_cache.insert(cache_key, output.clone());
164-
}
161+
self.command_cache.insert(cache_key, output.clone());
162+
163+
// if output != CommandOutput::default() {
164+
// self.command_cache.insert(cache_key, output.clone());
165+
// }
165166

166167
output
167168
}

0 commit comments

Comments
 (0)