Skip to content

Commit 358159e

Browse files
authored
Tiny build-time optimization for config handling (#2526)
This change reworks build_notifications from re-loading the task config from an environment variable, and then parsing as toml, once for every task. Instead, we now just get and parse the config once, and grab what we want from the pre-parsed table. This was noticed incidentally while debugging #2524
1 parent cdb2949 commit 358159e

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

build/util/src/lib.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,20 +198,31 @@ pub fn task_extern_regions<T: DeserializeOwned>() -> Result<IndexMap<String, T>>
198198
Ok(t)
199199
}
200200

201+
/// Pulls the entire config for all tasks
202+
pub fn all_tasks_full_config<T: DeserializeOwned>()
203+
-> Result<IndexMap<String, toml_task::Task<T>>> {
204+
toml_from_env::<IndexMap<String, toml_task::Task<_>>>(
205+
"HUBRIS_ALL_TASK_CONFIGS",
206+
)?
207+
.ok_or_else(|| anyhow!("HUBRIS_ALL_TASK_CONFIGS is not defined"))
208+
}
209+
201210
/// Pulls the full task configuration block of a different task
202211
pub fn other_task_full_config<T: DeserializeOwned>(
203212
name: &str,
204213
) -> Result<toml_task::Task<T>> {
205-
let mut t = toml_from_env::<IndexMap<String, toml_task::Task<_>>>(
206-
"HUBRIS_ALL_TASK_CONFIGS",
207-
)?
208-
.ok_or_else(|| anyhow!("HUBRIS_ALL_TASK_CONFIGS is not defined"))?;
214+
let mut t = all_tasks_full_config::<T>()?;
209215
let out = t
210216
.remove(name)
211217
.ok_or_else(|| anyhow!("Could not find {name} in tasks"))?;
212218
Ok(out)
213219
}
214220

221+
pub fn all_tasks_full_config_toml()
222+
-> Result<IndexMap<String, toml_task::Task<ordered_toml::Value>>> {
223+
all_tasks_full_config()
224+
}
225+
215226
pub fn other_task_full_config_toml(
216227
name: &str,
217228
) -> Result<toml_task::Task<ordered_toml::Value>> {
@@ -325,11 +336,15 @@ pub fn build_notifications() -> Result<()> {
325336

326337
write_task_notifications(&mut out, &full_task_config.notifications)?;
327338

328-
for task in env_var("HUBRIS_TASKS")
329-
.expect("missing HUBRIS_TASKS")
330-
.split(',')
331-
{
332-
let full_task_config = other_task_full_config_toml(task)?;
339+
// Rather than re-parse the toml on every task iter, we just get the full
340+
// task toml, and pluck out each task as we need it.
341+
let all_task_configs = all_tasks_full_config_toml()?;
342+
let all_tasks = env_var("HUBRIS_TASKS").expect("missing HUBRIS_TASKS");
343+
344+
for task in all_tasks.split(',') {
345+
let full_task_config = all_task_configs
346+
.get(task)
347+
.ok_or_else(|| anyhow!("Could not find {task} in tasks"))?;
333348
writeln!(&mut out, "pub mod {task} {{")?;
334349
write_task_notifications(&mut out, &full_task_config.notifications)?;
335350
writeln!(&mut out, "}}")?;

0 commit comments

Comments
 (0)