The memoize function is not a pretty thing in terms of implementation, but it's exactly what we need to solve this UX problem and performance problem. Without this, every distinct `withSystem` call will cause a re-evaluation of the `perSystem` module, which is inefficient. Now, it's a one-time 13KB and length(system) attribute lookups: negligible compared to any instantiations and such. Nix doesn't offer memoization for functions yet, so this is the best we can do.
23 lines
771 B
Nix
23 lines
771 B
Nix
# Run with:
|
|
# NIX_SHOW_STATS=1 nix eval --expr 'import ./measure-bytes-per-char.nix { control = false; size = 10; }' --impure
|
|
# NIX_SHOW_STATS=1 nix eval --expr 'import ./measure-bytes-per-char.nix { control = true; size = 10; }' --impure
|
|
|
|
{ control ? false, size ? 10 }:
|
|
|
|
let
|
|
lib = import <nixpkgs/lib>;
|
|
inherit (import ./memoize.nix { inherit lib; }) memoizeStr;
|
|
|
|
# Create a string of the specified size
|
|
key = lib.concatStrings (lib.genList (i: "a") size);
|
|
|
|
# Memoized identity function
|
|
memoId = memoizeStr (x: x);
|
|
|
|
# Prime the trie with a minimal query to force its construction
|
|
prime = memoId "";
|
|
|
|
in
|
|
if control
|
|
then builtins.seq prime key # Return key after priming
|
|
else builtins.seq prime (memoId key) # Pass through memoization after priming
|