nix-super/src
Sergei Trofimovich edfc5b2f12 ca-specific-schema.sql: add index on RealisationsRefs(referrer) and (outputPath)
For a typical desktop system (~2K packages) we can easily get 100K
entries in RealisationsRefs. Without indices query for RealisationsRefs
requires linear scan.

RealisationsRefs(referrer)
--------------------------

Inefficiency is seen as a 100% CPU load of nix-daemon for the following
scenario:

    $ nix edit -f . bash # add unused environment variable, like FOO="1"
    # populate RealisationsRefs, build fresh system
    $ nix build -f nixos system --arg config '{ contentAddressedByDefault = true; }'
    $ nix edit -f . bash # add unused environment variable, like FOO="2"
    $ time nix build -f nixos system --arg config '{ contentAddressedByDefault = true; }'

In this case `bash `will be rebuilt a few times and then rest of CPU
time is spent on scanning RealisationsRefs table (about 5 CPU-minutes
on my machine).

Before the change:

    $ time nix build -f nixos system ... # step 4 above
    real    34m3,613s
    user    0m5,232s
    sys     0m0,758s

Of all this time about 29.5 minutes are taken by nix-daemon's CPU time.

After the change:

    $ time nix build -f nixos system ... # step 4 above
    real    4m50,061s
    user    0m5,038s
    sys     0m0,677s

Of all this time about 1 minute is taken by nix-daemon's CPU time.
Most of the time is spent polling for non-existent realisations on
cache-nixos.org.

Realisations(outputPath)
------------------------

After running CA system for two weeks I got ~1M entries in Realisations
table. `nix-collect-garbage` became very slow (seemingly 100 path deletions
per second). It happens due to a slow cascading delete from Realisations
triggered by deletion from ValidPaths.

The fix is to add an index on primary key from ValidPaths(id) that
triggers cascading deletions.

Before the change:
    $ time nix-collect-garbage -d --max-freed 100G
    <interrupted before finish, took too long>
    real    23m32.411s
    user    17m49.679s
    sys     4m50.609s

Most of time was spent in re-scanning Realisations table on each path deletion.

After the change:
    $ time nix-collect-garbage -d --max-freed 100G

    real    8m43.226s
    user    6m16.317s
    sys     1m40.188s

Time is spent scanning sqlite indices and in kernel when unlinking directories.
2021-11-10 08:32:05 +00:00
..
build-remote build-remote: Implicitly add the 'builtin' system type to all machines 2021-10-27 14:25:13 +02:00
cpptoml bump cpptoml to v0.1.1 2019-05-29 17:01:39 +08:00
libcmd Make experimental-features a proper type 2021-10-26 07:02:31 +02:00
libexpr Merge pull request #5494 from tweag/balsoft/allow-references-in-addPath 2021-11-09 15:57:39 +01:00
libfetchers Support building flakes from a Git repo url with submodules query parameter 2021-10-26 20:02:37 +07:00
libmain Unshare mount namespace in main() 2021-11-08 22:00:45 +01:00
libstore ca-specific-schema.sql: add index on RealisationsRefs(referrer) and (outputPath) 2021-11-10 08:32:05 +00:00
libutil Merge branch 'fix-writable-shell' of https://github.com/yorickvP/nix 2021-11-08 21:12:51 +01:00
nix Unshare mount namespace in main() 2021-11-08 22:00:45 +01:00
nix-build Make experimental-features a proper type 2021-10-26 07:02:31 +02:00
nix-channel Initialize plugins after handling initial command line flags 2021-02-24 08:22:17 -05:00
nix-collect-garbage Initialize plugins after handling initial command line flags 2021-02-24 08:22:17 -05:00
nix-copy-closure copyPaths: Pass store by reference 2021-07-22 09:59:51 +02:00
nix-env Fix quotes 2021-09-14 19:48:16 +02:00
nix-instantiate nix-instantiate: Fix --eval-store 2021-07-27 11:16:47 +02:00
nix-store nix-store --serve: pass on settings.keepFailed from SSH store 2021-08-31 13:11:46 +02:00
nlohmann nlohmann_json: Update to 3.9.1, fix use of internal copy 2021-07-15 12:25:53 +02:00
resolve-system-dependencies Apply OS checks to host platform, not build 2021-06-23 15:00:36 +00:00