nix-gitignore: Optimise performance
There are a few operations in this library that naively runs on every iteration while they could be cached. For a simple test repository with a small number of files and ~1000 gitignore patterns this brings memory usage down from ~233M to ~157M and wall time from 2.6s down to 0.78s. This should scale similarly with the number of files in a repository.
This commit is contained in:
parent
a817ce3ada
commit
ef3ed45c12
@ -20,14 +20,13 @@ let
|
|||||||
in rec {
|
in rec {
|
||||||
# [["good/relative/source/file" true] ["bad.tmpfile" false]] -> root -> path
|
# [["good/relative/source/file" true] ["bad.tmpfile" false]] -> root -> path
|
||||||
filterPattern = patterns: root:
|
filterPattern = patterns: root:
|
||||||
(name: _type:
|
let
|
||||||
|
filters = map (pair: relPath: if match (head pair) relPath == null then true else last pair) patterns;
|
||||||
|
in
|
||||||
|
name: _type:
|
||||||
let
|
let
|
||||||
relPath = lib.removePrefix ((toString root) + "/") name;
|
relPath = lib.removePrefix ((toString root) + "/") name;
|
||||||
matches = pair: (match (head pair) relPath) != null;
|
in foldl' (acc: f: if acc == true then f relPath else acc) true filters;
|
||||||
matched = map (pair: [(matches pair) (last pair)]) patterns;
|
|
||||||
in
|
|
||||||
last (last ([[true true]] ++ (filter head matched)))
|
|
||||||
);
|
|
||||||
|
|
||||||
# string -> [[regex bool]]
|
# string -> [[regex bool]]
|
||||||
gitignoreToPatterns = gitignore:
|
gitignoreToPatterns = gitignore:
|
||||||
@ -91,7 +90,9 @@ in rec {
|
|||||||
(filter (l: !isList l && !isComment l)
|
(filter (l: !isList l && !isComment l)
|
||||||
(split "\n" gitignore));
|
(split "\n" gitignore));
|
||||||
|
|
||||||
gitignoreFilter = ign: root: filterPattern (gitignoreToPatterns ign) root;
|
gitignoreFilter = ign: let
|
||||||
|
patterns = gitignoreToPatterns ign;
|
||||||
|
in root: filterPattern patterns root;
|
||||||
|
|
||||||
# string|[string|file] (→ [string|file] → [string]) -> string
|
# string|[string|file] (→ [string|file] → [string]) -> string
|
||||||
gitignoreCompileIgnore = file_str_patterns: root:
|
gitignoreCompileIgnore = file_str_patterns: root:
|
||||||
@ -100,9 +101,10 @@ in rec {
|
|||||||
str_patterns = map (onPath readFile) (lib.toList file_str_patterns);
|
str_patterns = map (onPath readFile) (lib.toList file_str_patterns);
|
||||||
in concatStringsSep "\n" str_patterns;
|
in concatStringsSep "\n" str_patterns;
|
||||||
|
|
||||||
gitignoreFilterPure = filter: patterns: root: name: type:
|
gitignoreFilterPure = filter: patterns: root: let
|
||||||
gitignoreFilter (gitignoreCompileIgnore patterns root) root name type
|
compiledFilter = gitignoreCompileIgnore patterns root;
|
||||||
&& filter name type;
|
filterFn = gitignoreFilter compiledFilter;
|
||||||
|
in name: type: filterFn root name type && filter name type;
|
||||||
|
|
||||||
# This is a very hacky way of programming this!
|
# This is a very hacky way of programming this!
|
||||||
# A better way would be to reuse existing filtering by making multiple gitignore functions per each root.
|
# A better way would be to reuse existing filtering by making multiple gitignore functions per each root.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user