mirror of
https://github.com/NixOS/nixpkgs.git
synced 2024-11-22 06:53:01 +00:00
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.
This commit is contained in:
parent
627af21e87
commit
7e07b3ecd5
@ -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`.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user