mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-06-10 03:23:29 +03:00
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.
This commit is contained in:
parent
7e07b3ecd5
commit
544a1d375b
1 changed files with 21 additions and 11 deletions
|
@ -34,12 +34,20 @@ rec {
|
||||||
default:
|
default:
|
||||||
# The nested attribute set to select values from
|
# The nested attribute set to select values from
|
||||||
set:
|
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
|
in
|
||||||
if attrPath == [] then set
|
attrByPath' 0 set;
|
||||||
else if set ? ${attr}
|
|
||||||
then attrByPath (tail attrPath) default set.${attr}
|
|
||||||
else default;
|
|
||||||
|
|
||||||
/* Return if an attribute from nested attribute set exists.
|
/* Return if an attribute from nested attribute set exists.
|
||||||
|
|
||||||
|
@ -60,12 +68,14 @@ rec {
|
||||||
e:
|
e:
|
||||||
let
|
let
|
||||||
lenAttrPath = length attrPath;
|
lenAttrPath = length attrPath;
|
||||||
hasAttrByPath' = n: s: let
|
hasAttrByPath' = n: s: (
|
||||||
attr = elemAt attrPath n;
|
n == lenAttrPath || (
|
||||||
in (
|
let
|
||||||
if n == lenAttrPath then true
|
attr = elemAt attrPath n;
|
||||||
else if s ? ${attr} then hasAttrByPath' (n + 1) s.${attr}
|
in
|
||||||
else false
|
if s ? ${attr} then hasAttrByPath' (n + 1) s.${attr}
|
||||||
|
else false
|
||||||
|
)
|
||||||
);
|
);
|
||||||
in
|
in
|
||||||
hasAttrByPath' 0 e;
|
hasAttrByPath' 0 e;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue