Skip to content

Commit

Permalink
pkgs/top-level/unit-files.nix: Benchmark concatMap-based evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
infinisil committed Jun 13, 2023
1 parent 2bf137c commit 7930b80
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pkgs/top-level/unit-files.nix
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,24 @@ let
# m m m m m m m m
#
# Giving a total complexity of only O(log2(n)nm)

# Another potential approach is for `namesForShard` to return [ { name, value } ] instead, and then use builtins to merge it together:
#
# listToAttrs (concatMap namesForShard shards)
#
# This way the shard merging can be done by Nix's C++ code.

# We benchmark the three approaches with the following command with 300 runs:
#
# NIX_SHOW_STATS=1 nix-instantiate --eval --strict 'builtins.seq (import pkgs/top-level/unit-files.nix) null'
# Approach | cpuTime [ms] | gc.totalBytes [MB]
# linear merge | 33.4 ± 4.8 | 126.075
# concatMap | 21.9 ± 2.6 | 5.824
# binary merge | 16.6 ± 2.5 | 7.102
#
# Benchmarking with the top-level attribute distribution in Nixpkgs shows that doing this reduces evaluation time by ~47% (from 36ms to 19ms) and allocated bytes by ~94% (from ~120MB to ~7MB)
# The lowest evaluation time is achieved with the binary merge algorithm, even despite the slightly higher memory usage.
# However if https://github.com/NixOS/nix/issues/7676 were implemented in the future, the concatMap-based approach could be much more performant.
# Alternatively, other Nix builtins to speed up this computation would help too.

in
if shards == [ ] then
Expand Down

0 comments on commit 7930b80

Please sign in to comment.