git.iko.soy/typeable/nixpkgs
on master help sign in
lib.lists.unique: Switch from recursive function to using a fold

This improves performance by ~30-40% for smaller test cases and makes larger cases where my laptop would OOM pass in seconds.

commit85605c8a296bd54e9c12e1bd1c33bf1e100d40b7 authoradisbladis <adisbladis@gmail.com> dateTue 24 Nov 2020, 06:20 (6 years ago) parent8d8532c
1 file changed+1−7
lib/lists.nix+1−7
lib/lists.nix+1−7
@@ -640,13 +640,7 @@ rec {
640640 unique [ 3 2 3 4 ]
641641 => [ 3 2 4 ]
642642 */
643− unique = list:
644− if list == [] then
645− []
646− else
647− let
648− x = head list;
649− in [x] ++ unique (remove x list);
643+ unique = foldl' (acc: e: if elem e acc then acc else acc ++ [ e ]) [];
650644
651645 /* Intersects list 'e' and another list. O(nm) complexity.
652646