From a54f2231c9c4ea9e456511e855df11fe7df5ba71 Mon Sep 17 00:00:00 2001 From: polykernel <81340136+polykernel@users.noreply.github.com> Date: Mon, 27 Dec 2021 17:16:14 -0500 Subject: [PATCH] lib/attrset: optimize element access in recursiveUpdateUntil - Eta reduce formal arguments of `recursiveUpdate'. - Access elements in `recursiveUpdateUntil` using `elemAt` and `head` directly instead of `head (tail xs)` which copies a singleton unnecessarily. (`elemAt` is used instead of `last` to save a primitive call to `length`, this is possible because the 2-tuple structure is guranteed) - Use `length` instead of comparison to empty list to save a copy. --- lib/attrsets.nix | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/attrsets.nix b/lib/attrsets.nix index 812521ce6d1c..4e88601dbd3e 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -369,7 +369,7 @@ rec { value = f name (catAttrs name sets); }) names); - /* Implementation note: Common names appear multiple times in the list of + /* Implementation note: Common names appear multiple times in the list of names, hopefully this does not affect the system because the maximal laziness avoid computing twice the same expression and listToAttrs does not care about duplicated attribute names. @@ -419,8 +419,8 @@ rec { let f = attrPath: zipAttrsWith (n: values: let here = attrPath ++ [n]; in - if tail values == [] - || pred here (head (tail values)) (head values) then + if length values == 1 + || pred here (elemAt values 1) (head values) then head values else f here values @@ -446,10 +446,7 @@ rec { } */ - recursiveUpdate = lhs: rhs: - recursiveUpdateUntil (path: lhs: rhs: - !(isAttrs lhs && isAttrs rhs) - ) lhs rhs; + recursiveUpdate = recursiveUpdateUntil (path: lhs: rhs: !(isAttrs lhs && isAttrs rhs)); /* Returns true if the pattern is contained in the set. False otherwise.