From 7e07b3ecd55ac85675c26d9b09fab9996c410681 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Mon, 27 Nov 2023 01:41:01 +1300 Subject: [PATCH 1/2] lib.attrsets.hasAttrByPath: Don't allocate one extra list per lookup recursion Using `tail` in a recursive loop like this needlessly allocates. This changes the loop to look up by list index instead. --- lib/attrsets.nix | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/attrsets.nix b/lib/attrsets.nix index bf6c90bf1be6..107570627c66 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -58,13 +58,17 @@ rec { attrPath: # The nested attribute set to check e: - let attr = head attrPath; + let + lenAttrPath = length attrPath; + hasAttrByPath' = n: s: let + attr = elemAt attrPath n; + in ( + if n == lenAttrPath then true + else if s ? ${attr} then hasAttrByPath' (n + 1) s.${attr} + else false + ); in - if attrPath == [] then true - else if e ? ${attr} - then hasAttrByPath (tail attrPath) e.${attr} - else false; - + hasAttrByPath' 0 e; /* Create a new attribute set with `value` set at the nested attribute location specified in `attrPath`. From 544a1d375b47de09d6610c2059588032a9d8c192 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Mon, 27 Nov 2023 01:42:32 +1300 Subject: [PATCH 2/2] lib.attrsets.attrByPath: Don't allocate one extra list per lookup recursion Using `tail` in a recursive loop like this needlessly allocates. This changes the loop to look up by list index instead. --- lib/attrsets.nix | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/attrsets.nix b/lib/attrsets.nix index 107570627c66..52bc131ef40f 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -34,12 +34,20 @@ rec { default: # The nested attribute set to select values from set: - let attr = head attrPath; + let + lenAttrPath = length attrPath; + attrByPath' = n: s: ( + if n == lenAttrPath then s + else ( + let + attr = elemAt attrPath n; + in + if s ? ${attr} then attrByPath' (n + 1) s.${attr} + else default + ) + ); in - if attrPath == [] then set - else if set ? ${attr} - then attrByPath (tail attrPath) default set.${attr} - else default; + attrByPath' 0 set; /* Return if an attribute from nested attribute set exists. @@ -60,12 +68,14 @@ rec { e: let lenAttrPath = length attrPath; - hasAttrByPath' = n: s: let - attr = elemAt attrPath n; - in ( - if n == lenAttrPath then true - else if s ? ${attr} then hasAttrByPath' (n + 1) s.${attr} - else false + hasAttrByPath' = n: s: ( + n == lenAttrPath || ( + let + attr = elemAt attrPath n; + in + if s ? ${attr} then hasAttrByPath' (n + 1) s.${attr} + else false + ) ); in hasAttrByPath' 0 e;