GZIP压缩级别对解压缩有影响吗 [英] Does GZIP Compression Level Have Any Impact On Decompression

查看:1091
本文介绍了GZIP压缩级别对解压缩有影响吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道GZIP是LZ77和Huffman编码的组合,可以配置为1-9之间的级别,其中1表示最快的压缩(较少压缩),而9表示最慢的压缩方法(最佳压缩)。

I understand that GZIP is a combination of LZ77 and Huffman coding and can be configured with a level between 1-9 where 1 indicates the fastest compression (less compression) and 9 indicates the slowest compression method (best compression).

我的问题是,是否仅选择级别 会影响压缩过程,还是在减压方面会产生额外的成本,具体取决于使用的级别

My question is, does the choice of level only impact the compression process or is there an additional cost also incurred in decompression depending on the level used to compress?

我问,因为如果客户端支持的话,通常很多Web服务器都会动态响应GZIP,例如接受编码:gzip 。我很欣赏即时执行此操作时,对于一般情况,例如6的级别可能是个不错的选择,因为它可以在速度和压缩之间取得良好的平衡。

I ask because typically many web servers will GZIP responses on the fly if the client supports it, e.g. Accept-Encoding: gzip. I appreciate that when doing this on the fly a level such as 6 might be the good choice for the average case, since it gives a good balance between speed and compression.

但是,如果我有很多静态资产,可以提前一次进行GZIP压缩,而无需再执行此操作,那么使用它会有任何不利影响吗?最高但最慢的压缩级别?即如果使用较低的压缩级别,现在是否会为客户端带来额外的开销。

However, if I have a bunch of static assets that I can GZIP just once ahead of time - and never need to do this again - would there be any downside to using the highest but slowest compression level? I.e. is there now an additional overhead for the client that would not have been incurred had a lower compression level been used.

推荐答案

很好的问题,以及曝光不足的问题。您的直觉很扎实-对于某些压缩算法,解压缩时选择最大压缩级别可能需要解压缩器进行更多的工作。

Great question, and an underexposed issue. Your intuition is solid – for some compression algorithms, choosing the max level of compression can require more work from the decompressor when it's unpacked.

幸运的是,对于gzip而言并非如此–客户端/浏览器解压缩更多压缩的gzip文件没有额外的开销(例如,假设大多数服务器使用标准zlib代码库,则选择9进行压缩而不是6)。最好的衡量标准是解压缩率,目前,该解压缩率以MB /秒为单位,同时还监视内存和CPU等开销。仅仅通过解压缩时间是不好的,因为在较高的压缩设置下文件会变小,并且如果我们仅使用秒表,则我们不会控制该因素。

Luckily, that's not true for gzip – there's no extra overhead for the client/browser to decompress more heavily compressed gzip files (e.g. choosing 9 for compression instead of 6, assuming the standard zlib codebase that most servers use). The best measure for this is decompression rate, which for present purposes is in units of MB/sec, while also monitoring overhead like memory and CPU. Simply going by decompression time is no good because the file is smaller at higher compression settings, and we're not controlling for that factor if we're only using a stopwatch.


  1. gzip解压缩一旦达到6级压缩内容,就可以在解压缩时间和内存使用方面迅速达到渐近状态。 MarcusMüller链接的测试结果中第7、8和9级的解压缩平坦时间,尽管这是整秒给出的粗粒度数据。

  1. gzip decompression quickly gets asymptotic in terms of both time-to-decompress and memory usage once you get past level 6 compressed content. The time-to-decompress flatlines for levels 7, 8, and 9 in the test results linked by Marcus Müller, though that's coarse-grained data given in whole seconds.

您还将在这些结果中注意到,对于0.1 MiB的所有压缩级别,解压缩的内存要求均保持不变。这简直令人难以置信,只是我们很少看到的出色软件。马克·阿德勒(Mark Adler)及其同事应为自己取得的成就提供大量支持。 gzip是一种非常不错的格式。

You'll also notice in those results that the memory requirements for decompression are flat for all levels of compression at 0.1 MiB. That's almost unbelievable, just a degree of excellence in software that we rarely see. Mark Adler and colleagues deserve massive props for what they achieved. gzip is a very nice format.

内存使用问题是有关开销的问题。真的没有。就浏览器解压缩速度而言,您在第9级上不会获得多少收益,但是您不会损失任何东西。

The memory use gets at your question about overhead. There really is none. You don't gain much with level 9 in terms of browser decompression speed, but you don't lose anything.

现在,请查看这些测试结果可获得更多质感。您会发现,使用9级压缩内容时,gzip的解压缩速率要比使用较低级别的gzip解压缩速率稍快一些(例如,在9级时,解压缩速率比6级时快约0.9%)。这是有趣和令人惊讶的。我不希望这个比率会增加。那只是一组测试结果–在其他情况下可能不适用(在任何情况下,差异都很小)。

Now, check out these test results for a bit more texture. You'll see how the gzip decompression rate is slightly faster with level 9 compressed content than with lower levels (at level 9, decomp rate is about 0.9% faster than at level 6, for example). That is interesting and surprising. I wouldn't expect the rate to increase. That was just one set of test results – it may not hold for other scenarios (and the difference is quite small in any case).

分注:预压缩静态文件是一个好主意,但我不建议在9级使用gzip。通过使用zopfli或 libdeflate 。 Zopfli是Google完善的gzip压缩程序。 libdeflate是新的,但非常出色。在我的测试中,它始终击败gzip-9,但仍然落后于zopfli。您还可以使用7-Zip创建gzip文件,它将始终击败gzip-9。 (在上文中,gzip-9是指使用Apache和nginx使用的规范gzip或zlib应用程序。)

Parting note: Precompressing static files is a good idea, but I don't recommend gzip at level 9. You'll get smaller files than gzip-9 by instead using zopfli or libdeflate. Zopfli is a well-established gzip compressor from Google. libdeflate is new but quite excellent. In my testing it consistently beats gzip-9, but still trails zopfli. You can also use 7-Zip to create gzip files, and it will consistently beat gzip-9. (In the foregoing, gzip-9 refers to using the canonical gzip or zlib application that Apache and nginx use).

这篇关于GZIP压缩级别对解压缩有影响吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆