From d6ebd537e5d212995984152d57e16029b3726de5 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 31 Oct 2021 19:10:13 +0100 Subject: lib/modules: Short-circuit unmatchedDefns when configs is empty --- lib/modules.nix | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'lib/modules.nix') diff --git a/lib/modules.nix b/lib/modules.nix index 46ae3f136310b..b2ae51c8e618a 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -430,10 +430,16 @@ rec { # an attrset 'name' => list of unmatched definitions for 'name' unmatchedDefnsByName = - # Propagate all unmatched definitions from nested option sets - mapAttrs (n: v: v.unmatchedDefns) resultsByName - # Plus the definitions for the current prefix that don't have a matching option - // removeAttrs defnsByName' (attrNames matchedOptions); + if configs == [] + then + # When no config values exist, there can be no unmatched config, so + # we short circuit and avoid evaluating more _options_ than necessary. + {} + else + # Propagate all unmatched definitions from nested option sets + mapAttrs (n: v: v.unmatchedDefns) resultsByName + # Plus the definitions for the current prefix that don't have a matching option + // removeAttrs defnsByName' (attrNames matchedOptions); in { inherit matchedOptions; -- cgit 1.4.1 From bfaa9426c0e70b387f58bce6248b454b556018c2 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 3 Nov 2021 19:05:26 +0100 Subject: lib/modules: Short-circuit unmatchedDefns earlier --- lib/modules.nix | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'lib/modules.nix') diff --git a/lib/modules.nix b/lib/modules.nix index b2ae51c8e618a..4bbf2947bc4e9 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -430,26 +430,27 @@ rec { # an attrset 'name' => list of unmatched definitions for 'name' unmatchedDefnsByName = + # Propagate all unmatched definitions from nested option sets + mapAttrs (n: v: v.unmatchedDefns) resultsByName + # Plus the definitions for the current prefix that don't have a matching option + // removeAttrs defnsByName' (attrNames matchedOptions); + in { + inherit matchedOptions; + + # Transforms unmatchedDefnsByName into a list of definitions + unmatchedDefns = if configs == [] then # When no config values exist, there can be no unmatched config, so # we short circuit and avoid evaluating more _options_ than necessary. - {} + [] else - # Propagate all unmatched definitions from nested option sets - mapAttrs (n: v: v.unmatchedDefns) resultsByName - # Plus the definitions for the current prefix that don't have a matching option - // removeAttrs defnsByName' (attrNames matchedOptions); - in { - inherit matchedOptions; - - # Transforms unmatchedDefnsByName into a list of definitions - unmatchedDefns = concatLists (mapAttrsToList (name: defs: - map (def: def // { - # Set this so we know when the definition first left unmatched territory - prefix = [name] ++ (def.prefix or []); - }) defs - ) unmatchedDefnsByName); + concatLists (mapAttrsToList (name: defs: + map (def: def // { + # Set this so we know when the definition first left unmatched territory + prefix = [name] ++ (def.prefix or []); + }) defs + ) unmatchedDefnsByName); }; /* Merge multiple option declarations into a single declaration. In -- cgit 1.4.1 From 8b584158a5cf3f00ee87297030874dbe35373037 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 3 Nov 2021 19:34:27 +0100 Subject: lib/modules: Remove a lib.flip In hot code, the overhead (envs, applies) can matter. --- lib/modules.nix | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'lib/modules.nix') diff --git a/lib/modules.nix b/lib/modules.nix index 4bbf2947bc4e9..fd90c51b94251 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -13,7 +13,6 @@ let elem filter findFirst - flip foldl foldl' getAttrFromPath @@ -403,7 +402,7 @@ rec { [{ inherit (module) file; inherit value; }] ) configs; - resultsByName = flip mapAttrs declsByName (name: decls: + resultsByName = mapAttrs (name: decls: # We're descending into attribute ‘name’. let loc = prefix ++ [name]; @@ -424,7 +423,7 @@ rec { in throw "The option `${showOption loc}' in `${firstOption._file}' is a prefix of options in `${firstNonOption._file}'." else - mergeModules' loc decls defns); + mergeModules' loc decls defns) declsByName; matchedOptions = mapAttrs (n: v: v.matchedOptions) resultsByName; -- cgit 1.4.1 From 541ce53a3bfa9c769ce4aa755d1c452cb3bdc2dd Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 3 Nov 2021 19:39:31 +0100 Subject: lib/modules: Fix import* comments Very confusing otherwise. --- lib/modules.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/modules.nix') diff --git a/lib/modules.nix b/lib/modules.nix index fd90c51b94251..99f51d22dcd1a 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -947,7 +947,7 @@ rec { /* Use this function to import a JSON file as NixOS configuration. - importJSON -> path -> attrs + modules.importJSON :: path -> attrs */ importJSON = file: { _file = file; @@ -956,7 +956,7 @@ rec { /* Use this function to import a TOML file as NixOS configuration. - importTOML -> path -> attrs + modules.importTOML :: path -> attrs */ importTOML = file: { _file = file; -- cgit 1.4.1 From 844a9e746f92ac7278ece51c2b7819d0e6c5c05d Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 3 Nov 2021 19:45:06 +0100 Subject: lib/modules: Use strict fold' as recursiveUpdate is also strict recursiveUpdate does not produce an attrset until it has evaluated both its arguments to weak head normal form. nix-repl> lib.recursiveUpdate (throw "a") (throw "b") error: b nix-repl> lib.recursiveUpdate (throw "a") {} error: a --- lib/modules.nix | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib/modules.nix') diff --git a/lib/modules.nix b/lib/modules.nix index 99f51d22dcd1a..e3abf846625d0 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -13,7 +13,6 @@ let elem filter findFirst - foldl foldl' getAttrFromPath head @@ -863,7 +862,7 @@ rec { mkMergedOptionModule = from: to: mergeFn: { config, options, ... }: { - options = foldl recursiveUpdate {} (map (path: setAttrByPath path (mkOption { + options = foldl' recursiveUpdate {} (map (path: setAttrByPath path (mkOption { visible = false; # To use the value in mergeFn without triggering errors default = "_mkMergedOptionModule"; -- cgit 1.4.1