From 98b85b4580982f268664caf00fbc75c68c9f669f Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Sun, 28 Apr 2024 16:43:32 +0300 Subject: refactor(tvix/glue): clone less Prepare the NixAttr to return without an intermediate Vec<(String, NixString)>, and without into_iter(), and send off the unmoved Derivation struct to known_paths without having to clone it. Change-Id: Icdb9f78938e998a27d0313c5d9ab15b93af5821d Reviewed-on: https://cl.tvl.fyi/c/depot/+/11531 Tested-by: BuildkiteCI Autosubmit: flokli Reviewed-by: edef --- tvix/glue/src/builtins/derivation.rs | 58 +++++++++++++++++------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/tvix/glue/src/builtins/derivation.rs b/tvix/glue/src/builtins/derivation.rs index 8c7df96f91c1..37d2460d9f93 100644 --- a/tvix/glue/src/builtins/derivation.rs +++ b/tvix/glue/src/builtins/derivation.rs @@ -474,38 +474,36 @@ pub(crate) mod derivation_builtins { .calculate_derivation_path(name) .map_err(DerivationError::InvalidDerivation)?; - // TODO: avoid cloning - known_paths.add_derivation(drv_path.clone(), drv.clone()); - - let mut new_attrs: Vec<(String, NixString)> = drv - .outputs - .into_iter() - .map(|(name, output)| { - ( - name.clone(), + // Assemble the attrset to return from this builtin. + let out = Value::Attrs(Box::new(NixAttrs::from_iter( + drv.outputs + .iter() + .map(|(name, output)| { + ( + name.clone(), + NixString::new_context_from( + NixContextElement::Single { + name: name.clone(), + derivation: drv_path.to_absolute_path(), + } + .into(), + output.path.as_ref().unwrap().to_absolute_path(), + ), + ) + }) + .chain(std::iter::once(( + "drvPath".to_owned(), NixString::new_context_from( - NixContextElement::Single { - name, - derivation: drv_path.to_absolute_path(), - } - .into(), - output.path.unwrap().to_absolute_path(), + NixContextElement::Derivation(drv_path.to_absolute_path()).into(), + drv_path.to_absolute_path(), ), - ) - }) - .collect(); - - new_attrs.push(( - "drvPath".to_string(), - NixString::new_context_from( - NixContextElement::Derivation(drv_path.to_absolute_path()).into(), - drv_path.to_absolute_path(), - ), - )); - - Ok(Value::Attrs(Box::new(NixAttrs::from_iter( - new_attrs.into_iter(), - )))) + ))), + ))); + + // Register the Derivation in known_paths. + known_paths.add_derivation(drv_path, drv); + + Ok(out) } #[builtin("toFile")] -- cgit 1.4.1