summary refs log tree commit diff
path: root/lib/modules.nix
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2022-02-24 14:23:02 +0100
committerRobert Hensing <robert@roberthensing.nl>2022-03-03 00:28:35 +0100
commit0c09eb343dfa186c0fbf2bb6cbc36e7a8f369ea5 (patch)
treef7b7b56694f1ee59e3e7fec56c57834a1875aebb /lib/modules.nix
parent58a8a48e9d14cf397181d1223eabeb001f499049 (diff)
downloadnixpkgs-0c09eb343dfa186c0fbf2bb6cbc36e7a8f369ea5.tar
nixpkgs-0c09eb343dfa186c0fbf2bb6cbc36e7a8f369ea5.tar.gz
nixpkgs-0c09eb343dfa186c0fbf2bb6cbc36e7a8f369ea5.tar.bz2
nixpkgs-0c09eb343dfa186c0fbf2bb6cbc36e7a8f369ea5.tar.lz
nixpkgs-0c09eb343dfa186c0fbf2bb6cbc36e7a8f369ea5.tar.xz
nixpkgs-0c09eb343dfa186c0fbf2bb6cbc36e7a8f369ea5.tar.zst
nixpkgs-0c09eb343dfa186c0fbf2bb6cbc36e7a8f369ea5.zip
lib.modules: Refactor option scanning slightly
This scans the options with fewer function calls, improving performance.

It also removes a let Env from the happy flow of the new logic.
Diffstat (limited to 'lib/modules.nix')
-rw-r--r--lib/modules.nix18
1 files changed, 8 insertions, 10 deletions
diff --git a/lib/modules.nix b/lib/modules.nix
index 540eba1dd3d..e6812625f98 100644
--- a/lib/modules.nix
+++ b/lib/modules.nix
@@ -9,7 +9,6 @@ let
     catAttrs
     concatLists
     concatMap
-    count
     elem
     filter
     findFirst
@@ -492,20 +491,16 @@ rec {
           loc = prefix ++ [name];
           defns = defnsByName.${name} or [];
           defns' = defnsByName'.${name} or [];
-          nrOptions = count (m: isOption m.options) decls;
+          optionDecls = filter (m: isOption m.options) decls;
         in
-          if nrOptions == length decls then
+          if length optionDecls == length decls then
             let opt = fixupOptionType loc (mergeOptionDecls loc decls);
             in {
               matchedOptions = evalOptionValue loc opt defns';
               unmatchedDefns = [];
             }
-          else if nrOptions != 0 then
-            let
-              firstOption = findFirst (m: isOption m.options) "" decls;
-              firstNonOption = findFirst (m: !isOption m.options) "" decls;
-            in
-              if firstOption.options.type.name == "submodule"
+          else if optionDecls != [] then
+              if (lib.head optionDecls).options.type.name == "submodule"
               then
                 let opt = fixupOptionType loc (mergeOptionDecls loc (map optionTreeToOption decls));
                 in {
@@ -513,7 +508,10 @@ rec {
                   unmatchedDefns = [];
                 }
               else
-                throw "The option `${showOption loc}' in `${firstOption._file}' is a prefix of options in `${firstNonOption._file}'."
+                let
+                  firstNonOption = findFirst (m: !isOption m.options) "" decls;
+                in
+                throw "The option `${showOption loc}' in `${(lib.head optionDecls)._file}' is a prefix of options in `${firstNonOption._file}'."
           else
             mergeModules' loc decls defns) declsByName;