Skip to content

Support zstd compression for binary caches #2255

Closed

Description

@domenkozar

Activity

  1. nh2 commented on Jun 28, 2018

    @nh2
    Contributor

    Moving discussion from cachix/cachix#55 (comment) over here:

    Note that in my experience, zstd and brotli don't compress binaries better than xz.

    Nix does already support brotli compression (e.g. nix copy --to file:///bla?compression=br). Unfortunately, the default compression quality (11) makes it superslow, about 10x slower than xz -6 (with a worse compression ratio). OTOH with quality 6 it's 6x faster than xz -6. So we really should have a setting in Nix for the compression quality.

    @edolstra That's correct, xz remains to have the best compression ratio (this is also stated in the article I linked). The idea is that it compresses a lot faster.

    Here are some examples:

    zstd v1.3.4
    brotli 1.0.4
    xz 5.1.0alpha
    gzip 1.6
    lzop 1.03, LZO library 2.08
    
    % du -sh /nix/store/cn8yyi1z0y79hjnvs7qpmrb23drphhb6-chromium-65.0.3325.162/
    310M  /nix/store/cn8yyi1z0y79hjnvs7qpmrb23drphhb6-chromium-65.0.3325.162/
    
    % tar c /nix/store/cn8yyi1z0y79hjnvs7qpmrb23drphhb6-chromium-65.0.3325.162/ | lzop | pv > /dev/null
     140MiB 0:00:01 [ 130MiB/s]
    
    % tar c /nix/store/cn8yyi1z0y79hjnvs7qpmrb23drphhb6-chromium-65.0.3325.162/ | gzip | pv > /dev/null
    98.2MiB 0:00:11 [8.85MiB/s]
    
    % tar c /nix/store/cn8yyi1z0y79hjnvs7qpmrb23drphhb6-chromium-65.0.3325.162/ | zstd | pv > /dev/null
    94.5MiB 0:00:01 [60.8MiB/s]
    
    % tar c /nix/store/cn8yyi1z0y79hjnvs7qpmrb23drphhb6-chromium-65.0.3325.162/ | xz | pv > /dev/null
    66.6MiB 0:02:10 [ 524KiB/s]
    
    % tar c /nix/store/cn8yyi1z0y79hjnvs7qpmrb23drphhb6-chromium-65.0.3325.162/ | brotli --quality=6 - | pv > /dev/null
    79.6MiB 0:00:13 [5.77MiB/s]
    
    % tar c /nix/store/cn8yyi1z0y79hjnvs7qpmrb23drphhb6-chromium-65.0.3325.162/ | brotli --quality=3 - | pv > /dev/null
      93MiB 0:00:02 [  31MiB/s]
    

    zstd is

    • 7x faster than gzip while having the same output size
    • 120x faster than xz while having 30% increased output size
    • 2x faster than brotli --quality=4 while having the same output size

    It also supports mutliple cores; when used, that bumps the above numbers by some more integer factors:

    % tar c /nix/store/cn8yyi1z0y79hjnvs7qpmrb23drphhb6-chromium-65.0.3325.162/ | zstd -T4 | pv > /dev/null
    94.5MiB 0:00:00 [ 140MiB/s]
    
  2. nh2 commented on Jun 28, 2018

    @nh2
    Contributor

    So we really should have a setting in Nix for the compression quality.

    @edolstra That would be very appreciated; while zstd seems to do even better than brotli, being able to use brotli at level 4 would already be a huge improvement (4x vs gzip, 60x vs xz).

  3. dtzWill commented on Jul 4, 2018

    @dtzWill
    Member

    We already have an option for enabling multithreaded compression, options for compression level seem like an easy addition (I say this to indicate that anyone interested can tackle this).

    Hardest part seems to be sorting out how this should be specified in context of each compression method having different ranges and defaults. But I think they all support a basic notion of integer level so this seems workable...

    As for zstd, implementing support for it is likely also rather straightforward -- I haven't seen it's API but AFAIK they are all loosely similar. I was able to add brotli support pretty easily, for example, and I'd never used it's API before.

    I can't promise support for every compression technique will be eagerly merged but zstd seems reasonable addition...

    In short: these features likely take only an hour or two of an interested party's dev time, so if you want this supported consider tackling it and submitting a PR! The relevant parts of Nix involved here are rather accessible and don't rely on much beyond the task at hand (and maybe source/sink abstractions).

  4. davidak commented on Jan 5, 2020

    @davidak
    Member
  5. AleXoundOS commented on Apr 14, 2020

    @AleXoundOS

    How did they achieve only 0.8% package size increase? Impressive ratio for zstd. Maybe xz compressed package size was a factor of 0.8 compared to zstd and not percentage?
    Max compression level? Looking and the zstd manual, it's not promising, affecting decompression:

    --ultra
                  unlocks high compression levels 20+ (maximum 22), using a lot more memory. Note that decompression will also require more memory when using these levels.
    

    Nix already uses a lot of RAM.

  6. AleXoundOS commented on Apr 14, 2020

    @AleXoundOS

    The request is interesting. But the current points for xz (lzma) are saved network bandwidth and server storage costs.
    If it happens that download speed is slower than decompression while both are done in a streaming fashion (currently not), then we (users) hardly win anything.

    $ dd if=/nix/store/dbc7wxisaax83lsg54wwrlhy9xnd5n1y-linux-4.19.84.tar.xz | xzcat > /dev/null
    201853+0 records in
    201853+0 records out
    103348736 bytes (103 MB, 99 MiB) copied, 6.89651 s, 15.0 MB/s

    15 MiB/s is 120 Mibit/s. I wonder if many of us do really have this download speed from cache.nixos.org. So the request is more oriented towards uploading caches to private servers.

    As for common users I don't know if it is tricky to implement download->decompression streaming for nix store (most notably hash validation should happen after? (but xz -d checks integrity too)). But this brings to a question what are we going to solve and by which means?

  7. edolstra commented on Apr 15, 2020

    @edolstra
    Member

    Nix already does download -> decompression streaming.

  8. yorickvP commented on Apr 15, 2020

    @yorickvP
    Contributor

    Yes, I get 120 mbit/s from the cache, but I think the real benefit is on the compression side, where zstd uses significantly less CPU.

  9. nagisa commented on Apr 15, 2020

    @nagisa

    I think the real benefit is on the compression side

    Seconding this. nix copy for large outputs generally spends a ton of time just compressing and failing to saturate the uplink. Parallel compression helps (and also comes at a cost of compression ratio), but is still insufficient to saturate the uplink in certain setups.

  10. AleXoundOS commented on Apr 15, 2020

    @AleXoundOS

    Nix already does download -> decompression streaming

    Oh, good. My fault I haven't observe it yet.
    Then it's solely the compression side benefit (contrary to Arch Linux's aims).

  11. jonringer commented on Nov 28, 2020

    @jonringer
    Contributor

    I think zstd dictionaries for storing the logs would be an interesting use case as well, we currently use bz2

    I took a 728 file subset of /nix/var/log/drvs/

    the base behavior with bz2 compression:

    bzip lvl initial size compressed size ratio compress speed decompress speed
    9 43508286 3185439 13.6 ~10.35MB/s ~77MB/s

    training the dictionary: zstd --train-cover ./uncompressed/* -o dictionary

    running zstd -b1 -e19 -D dictionary -T4 ./uncompressed/*

    zstd lvl initial size compressed size ratio compress speed decompress speed
    1 43508286 3142304 13.85 1014.2 MB/s 2648.2 MB/s
    2 43508286 3095281 14.06 958.6 MB/s 2593.5 MB/s
    3 43508286 2936054 14.82 803.0 MB/s 2726.6 MB/s
    4 43508286 2934241 14.83 757.2 MB/s 2668.4 MB/s
    5 43508286 2674490 16.27 373.5 MB/s 2807.1 MB/s
    6 43508286 2524864 17.23 264.9 MB/s 2913.2 MB/s
    7 43508286 2484827 17.51 212.7 MB/s 2917.7 MB/s
    8 43508286 2458598 17.70 179.7 MB/s 3010.1 MB/s
    9 43508286 2442455 17.81 144.3 MB/s 3031.6 MB/s
    10 43508286 2430818 17.90 111.2 MB/s 3078.1 MB/s
    11 43508286 2437933 17.85 70.0 MB/s 3030.9 MB/s
    12 43508286 2410240 18.05 43.4 MB/s 3043.5 MB/s
    13 43508286 2548331 17.07 21.7 MB/s 2863.6 MB/s
    14 43508286 2451175 17.75 17.2 MB/s 2870.9 MB/s
    15 43508286 2301899 18.90 11.5 MB/s 2965.4 MB/s
    16 43508286 2321034 18.75 7.16 MB/s 2922.0 MB/s
    17 43508286 2284404 19.05 3.99 MB/s 2977.3 MB/s
    18 43508286 2275008 19.12 3.06 MB/s 2920.3 MB/s
    19 43508286 2286946 19.02 4.26 MB/s 2970.5 MB/s

    I ran it again without the the -T4, and it's mostly the same.

    even just running without a dict, just running zstd -b1 -e19 ./uncompressed/*, zstd seems to beat bzip2 at around lvl 6 in every category.

    zstd lvl initial size compressed size ratio compress speed decompress speed
    1 43508286 3662032 11.88 970.5 MB/s 2488.3 MB/s
    2 43508286 3541985 12.28 972.9 MB/s 2431.2 MB/s
    3 43508286 3453467 12.60 859.2 MB/s 2473.9 MB/s
    4 43508286 3404249 12.78 729.9 MB/s 2534.4 MB/s
    5 43508286 3229788 13.47 397.0 MB/s 2634.3 MB/s
    6 43508286 3165174 13.75 331.5 MB/s 2702.5 MB/s
    7 43508286 3092148 14.07 264.6 MB/s 2784.8 MB/s
    8 43508286 3059460 14.22 229.5 MB/s 2817.2 MB/s
    9 43508286 3042780 14.30 169.2 MB/s 2828.2 MB/s
    10 43508286 3026834 14.37 144.3 MB/s 2812.6 MB/s
    11 43508286 3025961 14.38 101.2 MB/s 2864.8 MB/s
    12 43508286 3013855 14.44 77.0 MB/s 2824.8 MB/s
    13 43508286 3019742 14.41 41.1 MB/s 2786.0 MB/s
    14 43508286 2996384 14.52 34.8 MB/s 2771.2 MB/s
    15 43508286 2953291 14.73 24.4 MB/s 2783.6 MB/s
    16 43508286 2967239 14.66 10.8 MB/s 2745.2 MB/s
    17 43508286 2939443 14.80 8.70 MB/s 2677.3 MB/s
    18 43508286 2943286 14.78 6.81 MB/s 2655.3 MB/s
    19 43508286 2868519 15.17 3.18 MB/s 2698.0 MB/s

    obviously, nix is not log bottle necked, but it would be nice to get some of that 17x compression ratio on logs.

    Also, bzip2 maintainership has been downhill for the past decade

  12. jonringer commented on Nov 28, 2020

    @jonringer
    Contributor

    and for small files, they get compressed to almost nothing:

    $ du --apparent-size --bytes ./*
    2077	./a0q5r02kzszz6la7daccn4xmbhklwl-ruby2.6.6-commander-fastlane-4.4.6.drv
    779	./a0q5r02kzszz6la7daccn4xmbhklwl-ruby2.6.6-commander-fastlane-4.4.6.drv.bz2
    215	./a0q5r02kzszz6la7daccn4xmbhklwl-ruby2.6.6-commander-fastlane-4.4.6.drv.dict.15.zstd
    

    given, this dictionary wasn't trained on a very large dataset, so it may be "overtrained", but it does help

  13. jyrkialakuijala commented on Feb 16, 2021

    @jyrkialakuijala

    You can use a dictionary with brotli, too. Even if you train it with zstd dictionary search.

    If you make the window size the same as in zstd, and the LZ77 search is done exactly the same way, brotli will be the same speed, but ~5 % more efficient due to the context modeling (depends on your data). From purely theoretical point of view, brotli format is better-suited for high compression speed and zstd better suited for fast decompression. Brotli also has slightly better streaming properties -- not hiding as much data as zstd from the data you already received as the data is multiplexed ideally for streaming in brotli and for decoding speed in zstd.

    You can control brotli's window size with --window 24 or --large_window 30.

  14. domenkozar commented on Feb 16, 2021

    @domenkozar
    MemberAuthor

    (^^ co-designer of brotli)

    @jyrkialakuijala our biggest problem right now is that brotli doesn't have the magic byte for decompression detection: #3333

    Since our data is compressed once and decompressed a lot of times, that still talks in favor of zstd for this use case.

  15. jyrkialakuijala commented on Feb 17, 2021

    @jyrkialakuijala

    Oi. Brotli was made only as a data stream, not to a file format (think deflate stream vs gzip). We need to place it into a container like shared brotli.

  16. domenkozar commented on Feb 21, 2021

    @domenkozar
    MemberAuthor

    I might have a budget for this feature to get done & merged (email me at [email protected]).

    Currently it seems like #3333 is the quickest way to zstd support, it needs to handle brotli as a special case as magic header detection doesn't work.

  17. nixos-discourse commented on Dec 12, 2022

    @nixos-discourse
  18. nixos-discourse commented on Jul 3, 2024

    @nixos-discourse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions