Skip to content

Switch from zlib to zstd #413

Closed
Closed

Description

@jrosdahl

Background

zstd is better than zlib in all aspects, and with negative compression levels it's as good as lz4 even for compression performance. The low overhead means that we can enable compression by default without negative consequences for the absolute majority of users.

What to do

  • Add zstd support.
    • Bundle libzstd for optional usage, just like we do with zlib. Let's try not bundling it.
  • Remove zlib support.
    • Rationale: Not keeping support for zlib means less dependencies and less complex code.

Edit: Changed from lz4 to zstd according to discussion below.

Activity

  1. jrosdahl commented on May 5, 2019

    @jrosdahl
    MemberAuthor

    Related old pull request with broader scope: #118.

  2. afbjorklund commented on May 6, 2019

    @afbjorklund
    Contributor

    This could probably also do with some benchmarks.

  3. afbjorklund commented on May 9, 2019

    @afbjorklund
    Contributor

    Not sure if a rebase is needed, seems that the old code still works "good enough" without it...
    There's some strange things going on with latest LZ4 versions, so I've stayed on the previous:

    LZ4 v1.9.1
    LZ4 v1.9.0

    Warning : this version has a known bug in the decompression function which makes it read a few bytes beyond input limit. Upgrade to v1.9.1 is recommended.

    LZ4 v1.8.3
    LZ4 v1.8.2

    This is maintenance release, mainly triggered by a data corruption that can only occur in v1.8.2, at level 9 (only), for some "large enough" data blocks (> 64 KB)

    LZ4 v1.7.5

    The main reason for staying with gzip (-1) as default was that the difference was not "big enough",
    adding a new compression meant invalidating the cache - and everyone has gzip, noone has lz4.

  4. afbjorklund commented on May 9, 2019

    @afbjorklund
    Contributor

    Rebased the old code: #421 (could be cleaned up further, but #118 had too much cruft in it now)

  5. afbjorklund commented on May 9, 2019

    @afbjorklund
    Contributor

    The performance seems to again vary a lot, so thankful for someone else testing it out as well...

    $ ./misc/performance --compression --compression-type=gzip --compression-level=-1 cc c++_includes.cc
    Compilation command: cc c++_includes.cc -c -o c++_includes.o
    Compilercheck: mtime
    Compression: on
        Type: gzip
        Level: -1
    Hardlink: off
    Nostats: off
    Without ccache:                               6.25 s (100.00 %) ( 1.00 x)
    With ccache, preprocessor mode, cache miss:   7.77 s (124.33 %) ( 0.80 x)
    With ccache, preprocessor mode, cache hit:    1.33 s ( 21.29 %) ( 4.70 x)
    With ccache, direct mode, cache miss:         8.68 s (138.89 %) ( 0.72 x)
    With ccache, direct mode, cache hit:          0.19 s (  3.11 %) (32.15 x)
    With ccache, depend mode, cache miss:         8.38 s (134.03 %) ( 0.75 x)
    With ccache, depend mode, cache hit:          0.28 s (  4.41 %) (22.67 x)
    $ ./misc/performance --compression --compression-type=gzip --compression-level=1 cc c++_includes.cc 
    Compilation command: cc c++_includes.cc -c -o c++_includes.o
    Compilercheck: mtime
    Compression: on
        Type: gzip
        Level: 1
    Hardlink: off
    Nostats: off
    Without ccache:                               6.09 s (100.00 %) ( 1.00 x)
    With ccache, preprocessor mode, cache miss:   8.03 s (131.98 %) ( 0.76 x)
    With ccache, preprocessor mode, cache hit:    1.18 s ( 19.35 %) ( 5.17 x)
    With ccache, direct mode, cache miss:         7.70 s (126.52 %) ( 0.79 x)
    With ccache, direct mode, cache hit:          0.28 s (  4.58 %) (21.82 x)
    With ccache, depend mode, cache miss:         8.09 s (132.97 %) ( 0.75 x)
    With ccache, depend mode, cache hit:          0.22 s (  3.69 %) (27.08 x)
    $ ./misc/performance --compression --compression-type=lz4f --compression-level=1 cc c++_includes.cc 
    Compilation command: cc c++_includes.cc -c -o c++_includes.o
    Compilercheck: mtime
    Compression: on
        Type: lz4f
        Level: 1
    Hardlink: off
    Nostats: off
    Without ccache:                               6.75 s (100.00 %) ( 1.00 x)
    With ccache, preprocessor mode, cache miss:   7.64 s (113.20 %) ( 0.88 x)
    With ccache, preprocessor mode, cache hit:    1.23 s ( 18.20 %) ( 5.49 x)
    With ccache, direct mode, cache miss:         8.23 s (121.83 %) ( 0.82 x)
    With ccache, direct mode, cache hit:          0.24 s (  3.50 %) (28.60 x)
    With ccache, depend mode, cache miss:         9.01 s (133.40 %) ( 0.75 x)
    With ccache, depend mode, cache hit:          0.25 s (  3.69 %) (27.10 x)
  6. afbjorklund commented on May 9, 2019

    @afbjorklund
    Contributor

    Hmm, the above was only headers - and the stderr and manifest are still using gzip compression...

    So that was a bad test, basically not using lz4f. Unfortunately per on ccache.c doesn't work anymore ?

    Seems like there might be some merit to the later versions of LZ4 and the special path for short files:

    plotcycles

  7. afbjorklund commented on May 26, 2019

    @afbjorklund
    Contributor

    If we want to do this for the manifest and result files, we need some kind of gzFile replacement...
    Either work with a local memory buffer, or something implementing a similar stdio buffer interface.

    Earlier implementations only used alternative compressions for the cache files, not the manifest.
    So it worked directly on the individual files, rather than attempting to do any stream compression.

  8. jrosdahl commented on Jun 5, 2019

    @jrosdahl
    MemberAuthor

    It should be relatively straight-forward to implement lz4 support now after a41ea79 and fa398de.

    Since the compression abstraction already is done and does not impose much complexity on other code, I'm now positive to keep zlib support in addition to lz4. I'm now also OK with keeping support for not compressing since it's essentially of no implementation cost. @afbjorklund: Opinions?

  9. shawnl commented on Jun 12, 2019

    @shawnl

    zstd fills all the use cases of both lz4 and gzip. I think you should go straight to zstd, as then the compression setting means alot more. With gzip the setting means hardly anything (and everyone uses -9).

  10. shawnl commented on Jun 12, 2019

    @shawnl

    There also is the possibility of fixing hard-link mode by updating mtimes when a hard link gets a cache hit. (so it doesn't confuse make/ninja)

    if hard link mode worked, then only heavier compression levels really make much sense, as you can't hard link with compression (unless you have transparent compression, but that is a differn't matter entirely)

  11. jrosdahl commented on Jun 13, 2019

    @jrosdahl
    MemberAuthor

    zstd fills all the use cases of both lz4 and gzip.

    That's a bold claim. My impression from when I looked at different alternatives was that zstd is better than gzip/libz in all aspects, but it does not match lz4 when it comes to speed. Do you have more information or insights than what's indicated by benchmarks and articles found on the net?

    Yes, I think that replacing zlib with zstd makes sense. I also think that it makes sense to let the default compression method be the fastest method available so that we can say that the performance overhead is negligible.

    Note that since a compression abstraction now is in place, the complexity cost of having several algorithms to choose from is less than what I originally imagined when I wrote this issue. I'll update the issue description to reflect this.

    (and everyone uses -9).

    Off topic, of course, but have you ever run tar -czf foo.tar.gz something? Then you've used gzip without using -9. My guess is that more people use the default -6 level than anything else.

  12. jrosdahl commented on Jun 13, 2019

    @jrosdahl
    MemberAuthor

    There also is the possibility of fixing hard-link mode by updating mtimes when a hard link gets a cache hit. (so it doesn't confuse make/ninja)

    I don't understand what you mean. The mtime is already updated when there is a cache hit. That's one of the problems with the hard link mode. What do you mean should/could be done?

    Or did you mean to say "fixing hard-link mode by not updating mtimes [on] a cache hit"? If so, yes, that would of course be possible, but it would be a backward incompatible change that I would like to avoid if possible.

    if hard link mode worked, then only heavier compression levels really make much sense

    That line of reasoning doesn't cover all use cases. For instance:

    • The cache can be on another file system, so hard links cannot be used.
    • The cache can, in the future, be stored in some other way, e.g. memcached, so hard links cannot be used.
    • One might not want to use hard links because modifying a hard-link object file in place will corrupt the cache entry.
    • One might not want to use hard links because they have the above mentioned mtime problem.
  13. changed the title [-]Switch from zlib to LZ4[/-] [+]Add support for compression using LZ4[/+] on Jun 13, 2019
  14. jrosdahl commented on Jun 14, 2019

    @jrosdahl
    MemberAuthor

    @shawnl: Your suggestion about zstd piqued my interest to have a look at zstd again.

    I learned that zstd has a --fast=# (AKA "negative levels") option. I'm not sure if I missed that when I evaluated different algorithms a little over a year ago or if it's something new (edit: yes, it seems to be a relatively new feature), but from some quick experiments they look very promising. I've mostly looked at compression performance since that's what I'm most interested in in this case; decompression speed doesn't feel like an issue. On a test corpus of a large file containing various concatenated object files which I hope are representative, zstd --fast=4 beat lz4 -1 on compression speed while at the same time producing a slightly smaller file! Decompression speed was not as high as lz4, but near enough.

    I've now changed my mind and agree that it looks like zstd fits all use cases I see for ccache regarding compression. Unless @afbjorklund disagrees (@pavsa maybe has an opinion as well?), I think that we'll go with zstd instead (and remove zlib support).

  15. 16 remaining items

  16. pavsa commented on Jun 16, 2019

    @pavsa
    Contributor

    or as a git submodule from their repository?

  17. jrosdahl commented on Jun 16, 2019

    @jrosdahl
    MemberAuthor

    or as a git submodule from their repository?

    That won't be helpful for people building from a release archive.

  18. afbjorklund commented on Jun 16, 2019

    @afbjorklund
    Contributor

    Implementation done, and rebased.

    There are currently some build errors on master, where the make analyze target is failing.

  19. llunak commented on Jun 23, 2019

    @llunak
    Contributor

    For instance one would have to figure out how to install it on Mac and Win, and on Travis...
    i.e. Travis run on Ubuntu 14.04, whereas the "libzstd-dev" package first appeared in 16.04

    Travis provides both 14.04 and 16.04, so unless you need to test on 14.04, this is not a problem (and even for 14.04 it's possible to get it from an extra repo, although there's not an official one). Icecream has recently added zstd support and it builds in Travis (including Mac).

  20. shawnl commented on Jun 23, 2019

    @shawnl
  21. llunak commented on Jun 23, 2019

    @llunak
    Contributor

    Why wasn't distcc ever told about this?

    Why should it? Distcc is practically an unrelated project to Icecream. (And in case you want to discuss this further, -> https://github.com/icecc/icecream/ , this discussion is about ccache.)

  22. shawnl commented on Jun 23, 2019

    @shawnl
  23. llunak commented on Jun 23, 2019

    @llunak
    Contributor

    I've told you what to do if you want to discuss that. It's off-topic here and I don't want to take part in hijacking this discussion.

  24. jrosdahl commented on Jun 24, 2019

    @jrosdahl
    MemberAuthor

    Travis provides both 14.04 and 16.04, so unless you need to test on 14.04, this is not a problem (and even for 14.04 it's possible to get it from an extra repo, although there's not an official one). Icecream has recently added zstd support and it builds in Travis (including Mac).

    Thanks @llunak, that's useful to know. (And thanks for not continuing the discussion about distcc here.)

  25. added this to the 3.8 milestone on Jun 29, 2019
  26. self-assigned this
    on Jun 29, 2019
  27. added a commit that references this issue on Nov 2, 2019
    bf868fc
  28. kreuzerkrieg commented on May 5, 2020

    @kreuzerkrieg

    What if you submit a good chunk of ccache files to Squash as new corpus and just see results? Or in case the dataset is too big just roll your own server for running these benchmarks - https://github.com/quixdb/squash-benchmark-web/

  29. added a commit that references this issue on Aug 16, 2026
    9e7d670
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

issue: feature

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions