Skip to content

Commit 6063c92

Browse files
committed
perf: Eliminate env map cloning and increase cache write buffer
- Change CommandProvider trait to take &EnvironmentVariableMap instead of by value, eliminating all HashMap clones of the environment variable map in the per-task command creation path. Previously each task execution cloned the entire env map (which can contain 100+ variables) once at the call site and again per command provider. - Increase async cache mpsc channel buffer from 1 to max_workers so cache write requests don't block waiting for the semaphore permit inside the worker loop. The semaphore already limits concurrency. - Remove unused get_global_hash_inputs re-export. https://claude.ai/code/session_01CfUraGs872goKHRKSRyFXi
1 parent 58a7068 commit 6063c92

File tree

5 files changed

+27
-24
lines changed

5 files changed

+27
-24
lines changed

crates/turborepo-cache/src/async_cache.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ impl AsyncCache {
4949
api_auth,
5050
analytics_recorder,
5151
)?);
52-
let (writer_sender, mut write_consumer) = mpsc::channel(1);
52+
// Buffer up to max_workers requests so that callers don't block
53+
// waiting for a semaphore permit inside the worker loop. The
54+
// semaphore already limits actual concurrency.
55+
let (writer_sender, mut write_consumer) = mpsc::channel(max_workers);
5356

5457
// start a task to manage workers
5558
let worker_real_cache = real_cache.clone();

crates/turborepo-lib/src/task_graph/visitor/command.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ mod test {
3939
fn command(
4040
&self,
4141
_task_id: &TaskId,
42-
_environment: EnvironmentVariableMap,
42+
_environment: &EnvironmentVariableMap,
4343
) -> Result<Option<Command>, Error> {
4444
Ok(Some(Command::new("echo")))
4545
}
@@ -51,7 +51,7 @@ mod test {
5151
fn command(
5252
&self,
5353
_task_id: &TaskId,
54-
_environment: EnvironmentVariableMap,
54+
_environment: &EnvironmentVariableMap,
5555
) -> Result<Option<Command>, Error> {
5656
Err(Error::InternalErrors("oops!".into()))
5757
}
@@ -63,7 +63,7 @@ mod test {
6363
fn command(
6464
&self,
6565
_task_id: &TaskId,
66-
_environment: EnvironmentVariableMap,
66+
_environment: &EnvironmentVariableMap,
6767
) -> Result<Option<Command>, Error> {
6868
Ok(None)
6969
}
@@ -77,7 +77,7 @@ mod test {
7777
.add_provider(ErrProvider);
7878
let task_id = TaskId::new("foo", "build");
7979
let cmd = factory
80-
.command(&task_id, EnvironmentVariableMap::default())
80+
.command(&task_id, &EnvironmentVariableMap::default())
8181
.unwrap()
8282
.unwrap();
8383
assert_eq!(cmd.program(), OsStr::new("echo"));
@@ -91,7 +91,7 @@ mod test {
9191
.add_provider(EchoCmdFactory);
9292
let task_id = TaskId::new("foo", "build");
9393
let cmd = factory
94-
.command(&task_id, EnvironmentVariableMap::default())
94+
.command(&task_id, &EnvironmentVariableMap::default())
9595
.unwrap_err();
9696
assert_snapshot!(cmd.to_string(), @"Internal errors encountered: oops!");
9797
}
@@ -104,7 +104,7 @@ mod test {
104104
.add_provider(NoneProvider);
105105
let task_id = TaskId::new("foo", "build");
106106
let cmd = factory
107-
.command(&task_id, EnvironmentVariableMap::default())
107+
.command(&task_id, &EnvironmentVariableMap::default())
108108
.unwrap()
109109
.unwrap();
110110
assert_eq!(cmd.program(), OsStr::new("echo"));
@@ -115,7 +115,7 @@ mod test {
115115
let factory = CommandFactory::new();
116116
let task_id = TaskId::new("foo", "build");
117117
let cmd = factory
118-
.command(&task_id, EnvironmentVariableMap::default())
118+
.command(&task_id, &EnvironmentVariableMap::default())
119119
.unwrap();
120120
assert!(cmd.is_none(), "expected no cmd, got {cmd:?}");
121121
}
@@ -193,7 +193,7 @@ mod test {
193193
let cmd = factory
194194
.command(
195195
&TaskId::new("web", "proxy"),
196-
EnvironmentVariableMap::default(),
196+
&EnvironmentVariableMap::default(),
197197
)
198198
.unwrap()
199199
.unwrap();

crates/turborepo-lib/src/task_graph/visitor/exec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl<'a> ExecContextFactory<'a> {
9494

9595
let Some(cmd) = self
9696
.command_factory
97-
.command(&task_id, execution_env.clone())?
97+
.command(&task_id, &execution_env)?
9898
else {
9999
return Ok(None);
100100
};

crates/turborepo-lib/src/task_hash.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
66
// Re-export all public types from turborepo-task-hash
77
pub use turborepo_task_hash::{
8-
collect_global_file_hash_inputs, get_external_deps_hash, get_global_hash_inputs,
9-
get_internal_deps_hash, global_hash, Error, GlobalHashableInputs, PackageInputsHashes,
8+
collect_global_file_hash_inputs, get_external_deps_hash, get_internal_deps_hash, global_hash,
9+
Error, GlobalHashableInputs, PackageInputsHashes,
1010
TaskHashTracker, TaskHashTrackerState,
1111
};
1212

crates/turborepo-task-executor/src/command.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub trait CommandProvider<E> {
4242
fn command(
4343
&self,
4444
task_id: &TaskId,
45-
environment: EnvironmentVariableMap,
45+
environment: &EnvironmentVariableMap,
4646
) -> Result<Option<Command>, E>;
4747
}
4848

@@ -82,10 +82,10 @@ impl<'a, E> CommandFactory<'a, E> {
8282
pub fn command(
8383
&self,
8484
task_id: &TaskId,
85-
environment: EnvironmentVariableMap,
85+
environment: &EnvironmentVariableMap,
8686
) -> Result<Option<Command>, E> {
8787
for provider in self.providers.iter() {
88-
let cmd = provider.command(task_id, environment.clone())?;
88+
let cmd = provider.command(task_id, environment)?;
8989
if cmd.is_some() {
9090
return Ok(cmd);
9191
}
@@ -175,7 +175,7 @@ impl<'a, M: MfeConfigProvider, E: From<CommandProviderError>> CommandProvider<E>
175175
fn command(
176176
&self,
177177
task_id: &TaskId,
178-
environment: EnvironmentVariableMap,
178+
environment: &EnvironmentVariableMap,
179179
) -> Result<Option<Command>, E> {
180180
let workspace_info = self.package_info(task_id)?;
181181

@@ -305,7 +305,7 @@ impl<'a, T: PackageInfoProvider + Send + Sync, M: MfeConfigProvider, E: From<Com
305305
fn command(
306306
&self,
307307
task_id: &TaskId,
308-
_environment: EnvironmentVariableMap,
308+
_environment: &EnvironmentVariableMap,
309309
) -> Result<Option<Command>, E> {
310310
debug!(
311311
"MicroFrontendProxyProvider::command - called for task: {}",
@@ -421,7 +421,7 @@ mod tests {
421421
fn command(
422422
&self,
423423
_task_id: &TaskId,
424-
_environment: EnvironmentVariableMap,
424+
_environment: &EnvironmentVariableMap,
425425
) -> Result<Option<Command>, String> {
426426
Ok(Some(Command::new("echo")))
427427
}
@@ -433,7 +433,7 @@ mod tests {
433433
fn command(
434434
&self,
435435
_task_id: &TaskId,
436-
_environment: EnvironmentVariableMap,
436+
_environment: &EnvironmentVariableMap,
437437
) -> Result<Option<Command>, String> {
438438
Ok(None)
439439
}
@@ -445,7 +445,7 @@ mod tests {
445445
fn command(
446446
&self,
447447
_task_id: &TaskId,
448-
_environment: EnvironmentVariableMap,
448+
_environment: &EnvironmentVariableMap,
449449
) -> Result<Option<Command>, String> {
450450
Err("error".to_string())
451451
}
@@ -457,7 +457,7 @@ mod tests {
457457
factory.add_provider(EchoProvider).add_provider(ErrProvider);
458458
let task_id = TaskId::new("foo", "build");
459459
let cmd = factory
460-
.command(&task_id, EnvironmentVariableMap::default())
460+
.command(&task_id, &EnvironmentVariableMap::default())
461461
.unwrap()
462462
.unwrap();
463463
assert_eq!(cmd.program(), OsStr::new("echo"));
@@ -469,7 +469,7 @@ mod tests {
469469
factory.add_provider(ErrProvider).add_provider(EchoProvider);
470470
let task_id = TaskId::new("foo", "build");
471471
let err = factory
472-
.command(&task_id, EnvironmentVariableMap::default())
472+
.command(&task_id, &EnvironmentVariableMap::default())
473473
.unwrap_err();
474474
assert_eq!(err, "error");
475475
}
@@ -482,7 +482,7 @@ mod tests {
482482
.add_provider(EchoProvider);
483483
let task_id = TaskId::new("foo", "build");
484484
let cmd = factory
485-
.command(&task_id, EnvironmentVariableMap::default())
485+
.command(&task_id, &EnvironmentVariableMap::default())
486486
.unwrap()
487487
.unwrap();
488488
assert_eq!(cmd.program(), OsStr::new("echo"));
@@ -493,7 +493,7 @@ mod tests {
493493
let factory: CommandFactory<String> = CommandFactory::new();
494494
let task_id = TaskId::new("foo", "build");
495495
let cmd = factory
496-
.command(&task_id, EnvironmentVariableMap::default())
496+
.command(&task_id, &EnvironmentVariableMap::default())
497497
.unwrap();
498498
assert!(cmd.is_none(), "expected no cmd, got {cmd:?}");
499499
}

0 commit comments

Comments
 (0)