From 85605c8a296bd54e9c12e1bd1c33bf1e100d40b7 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Tue, 24 Nov 2020 06:20:01 +0100 Subject: [PATCH] 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. --- lib/lists.nix | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/lists.nix b/lib/lists.nix index 6c97e0686aa..06cee2eb112 100644 --- a/lib/lists.nix +++ b/lib/lists.nix @@ -640,13 +640,7 @@ rec { unique [ 3 2 3 4 ] => [ 3 2 4 ] */ - unique = list: - if list == [] then - [] - else - let - x = head list; - in [x] ++ unique (remove x list); + unique = foldl' (acc: e: if elem e acc then acc else acc ++ [ e ]) []; /* Intersects list 'e' and another list. O(nm) complexity.