Stateless ZIP library - SLZ

BranchnameLast versionReleasedLinksNotes
Developmentmaster1.4-dev52026-11-08git / web / githubmay be broken
1.3stable-1.31.3.22026-10-25git / web / githubStable version
1.2stable-1.21.2.22026-04-09git / web / githubPrevious stable version

Introduction

SLZ is a fast and memory-less stream compressor which produces an output that can be decompressed with zlib, gzip or any browser. Since version 1.3 it also comes with uslz, a lightweight decompressor working with a 32 kB sliding buffer, but zlib remains perfectly fine for decompression when its memory usage is not a concern.

The purpose is to use SLZ in situations where a zlib-compatible stream is needed and zlib's resource usage would be too high while the compression ratio is not critical. The typical use case is in HTTP servers and gateways which have to compress many streams in parallel with little CPU resources to assign to this task, and without having to thottle the compression ratio due to the memory usage. In such an environment, the server's memory usage can easily be divided by 10 and the CPU usage by 3 or more. In addition its high performance made it fill a gap in network backup applications.

How does it work ?

While zlib uses 256 kB of memory per stream in addition to a few tens of bytes for the stream descriptor itself, SLZ only stores a stream descriptor made of 40 bytes on 64-bit machines (32 bytes on 32-bit ones). Thus it is particularly suited to environments having to deal with tens to hundreds of thousands of concurrent streams.

The key difference between zlib and SLZ is that SLZ is stateless in that it doesn't consider the data previously compressed as part of its dictionary. It doesn't hurt compression performance when it is fed in large enough chunks (at least a few kB at once), but it must not be used if it has to read less than a few kB at a time, or the caller will have to implement the input buffer to maximize the compression ratio. For example, an HTTP gateway may defragment all the parts from a chunked-encoded response buffer into a linear buffer that is then fed at once to SLZ. This temporary buffer would then be static and shared between all streams running on the same thread.

A side effect from its stateless nature is that it can emit a zlib-compatible stream without being vulnerable to CRIME-like attacks by processing the sensitive and attacker-controlled data in distinct batches. Since no dictionary persists between calls, there is no risk that attacker-controlled data is used to guess sensitive data using dictionary lookups.

Another induced benefit which was not initially seeked is that SLZ is about 5 times as fast as zlib in its fastest mode. This is the result of the simpler matching and encoding mechanisms involved which permit even more optimisations.

Since version 1.4, four compression levels are available. Level 1 is the fastest one and gives up early on hard-to-compress areas. Level 2 is the default: it gives up just as early but keeps checking the areas it skips, and stops skipping as soon as it recognizes something. Level 3 searches more before giving up. Level 4 never gives up and extends matches further backwards, for a slightly better ratio. In addition, a text content type may be selected for HTML, CSS, JS, JSON, SVG and similar contents: instead of the fixed huffman tree from RFC1951, it uses a tree trained on web contents, which saves about 15% of the huffman-coded bits on such contents. It is not suited to binary contents.

Reference tests

Some performance tests were run using version 1.0 on various types of data showing anything between 227 MB/s and 753 MB/s per core on the older hardware depending on the difficulty to find matching strings and on the amount of data emitted as literals (hence not compressed). In the end, comparative tests were performed using some reference datasets that are already being used for other libraries. This performance report was produced using the Silesia Corpus to get something comparable to what is reported by the LZ4 utility here : https://github.com/Cyan4973/lz4. Since then, tests have been performed on modern hardware, from 16-core ARM to 64-core AMD EPYC, and performance on x86_64 and ARMv8 has been further improved.

The following tests were run on a single core of an Intel Core Ultra 7 265K at 5.5 GHz (turbo mode). The figures for lz4, zstd, gzip and slz were all measured on the same machine. The input file is the Silesia Corpus tar file placed in a RAM disk (211947520 bytes). The output size was measured using "wc -c", but the bandwidth was measured by sending the output to /dev/null so as not to let the "wc" command slow everything down.

Compression ratio vs speed of slz, lz4, zstd and gzip on the Silesia corpus, one core of a Core Ultra 7 265K
SLZ was included in Inikep's lzbench benchmark suite, which compares a wide range of compression tools. The project maintains an updated performance table where other solutions can be compared.

Only gzip/zlib and SLZ can emit a stream that a regular zlib-compatible browser can decompress (RFC1950/1951/1952 aka zlib, deflate, gzip). Here SLZ is 4.5 to 5.7 times faster than zlib, for a payload 14 to 27% larger than zlib's. Slz 1.4's gzip compression is 1.9 times faster than slz-1.3's, its zlib compression 1.66 times faster, and its deflate compression 52% faster, all while compressing slightly more.

What is particularly surprising is to see slz-1.4's levels 1 and 2 both being faster and stronger than lz4 on this machine. This partly comes from the optimization level, as a generic build (which matches the build of the distro's package) shows 924 and 811 MB/s respectively: then only level 1 is faster, and level 2 is slightly slower, though both still compress better. Compared to zstd, slz is not put to shame either: zstd always compresses better and is always slower, at all levels. The closest pair is zstd --fast and slz -T, at compression ratios of 2.44 and 2.40 (2% better for zstd), where slz is 6.9% faster. Again, we are flirting with CPU-specific optimizations here.

The text content type was trained on web contents and Silesia does not match them, but it still brings the output within 14% of zlib's, because the corpus contains some source code, which is text not very different from web contents. The three output formats now run at almost the same speed: the zlib and gzip formats cost only 3.5% compared to raw deflate on this CPU.

Compressing the sources of a Linux 7.0 kernel better illustrates the gains to expect from text inputs, on the same machine:

Compression ratio vs speed of slz, zstd and gzip on the Linux 7.0 sources, one core of a Core Ultra 7 265K

This is more than one gigabyte per second of input data that is divided in size by 4 to 5. It is 3.7 times faster than gzip for a 16% larger output at level 1, and still 3.5 times faster than gzip for a 2.4% larger output in text mode. Here again slz completes the spectrum on the fast but lower compression side, just after zstd: at its highest compression ratio, it is 13% faster than zstd --fast for a 9% larger output.

Modern ARM cores deliver good performance as well, as seen below on an A720 core of a CIX P1 processor in a Radxa Orion O6, on the Silesia corpus file:

Compression ratio vs speed of slz levels on one Cortex A720 core

Since SLZ focuses a lot on performance savings gained by keeping most of the workload in CPU cache, it scales very well when multiple cores are used in parallel (low memory bandwidth) as shown in this tests run on a 16-core ARM Cortex A72 (NXP LX2160A) running at 2.2 GHz, using the same input data at level 1 in deflate format :

Total compression speed of zenc -D -1 vs number of cores on a 16-core Cortex A72

Here the scaling is perfectly linear: each core compresses as fast with all 16 cores busy as when it runs alone. At level 3, the same machine processes 141.7 MB/s on one core and 2328 MB/s on 16 cores.

On a more powerful machine (64-core 3rd gen AMD EPYC at 3.7 GHz), with the same settings, we observe this:
One thread per coreTwo threads per core
#CoresInput sizeBW/core(MB/s)Total BW(MB/s)
1211947520573.8573.8
2211947520574.91149.8
3211947520572.41717.2
4211947520569.42277.6
8211947520540.14320.8
16211947520472.77563.2
32211947520461.614771.2
64211947520380.024320.0
#CoresInput sizeBW/core(MB/s)Total BW(MB/s)
12*211947520757.0757.0
22*211947520762.61523.2
32*211947520754.02262.0
42*211947520756.23024.8
82*211947520720.05760.0
162*211947520624.49990.4
322*211947520610.419532.8
642*211947520447.628646.4

The per-core drop seen here is not caused by SLZ but by the CPU's frequency, which falls as more cores become active: it runs at 3.7 GHz on up to 4 cores, 3.1 GHz from 8 to 32 cores, and about 2.5 GHz on all 64 cores. With the frequency fixed at 2.5 GHz, the scaling is almost linear again, with each core retaining 94% of its single-core performance with all 64 cores busy, and running two threads per core brings 27 to 33% more:
One thread per core, 2.5 GHzTwo threads per core, 2.5 GHz
#CoresInput sizeBW/core(MB/s)Total BW(MB/s)
1211947520380.4380.4
2211947520380.6761.2
3211947520379.01137.0
4211947520378.91515.6
8211947520374.02992.0
16211947520376.16017.6
32211947520371.411885.1
64211947520356.022783.4
#CoresInput sizeBW/core(MB/s)Total BW(MB/s)
12*211947520506.3506.3
22*211947520507.21014.3
32*211947520505.01515.0
42*211947520504.22016.8
82*211947520496.53971.8
162*211947520494.17906.1
322*211947520490.115684.0
642*211947520450.928855.6

Total compression speed of zenc -D -1 vs number of cores on a 64-core AMD EPYC, at stock frequency and at 2.5 GHz, with 1 and 2 threads per core

With all 128 threads busy, the machine compresses the equivalent of about 230 Gbps of input TCP traffic to 107 Gbps of output TCP traffic at level 1. At level 3, which compresses better (ratio 2.32 instead of 2.15), it still processes 26.0 GB/s, or 208 Gbps of input traffic compressed to 89.7 Gbps, and a single core runs at 491 MB/s (650 MB/s with two threads).

Decompression is not SLZ's main purpose, but the zdec utility, based on the built-in uslz decompressor, is 3 times faster than zcat on streams produced by zenc, and remains 2 times faster on streams produced by gzip. These tests were run on a single core of a 3rd gen AMD EPYC at 3.7 GHz, decompressing the Silesia corpus compressed in gzip format (the output size is the 211947520 bytes of the original data):

Increasing compression ratio in HTTP gateways

Note: the measurements in this section were performed with version 1.2 and will need to be performed again with the current version.

Since some products automatically throttle the compression level based on the CPU usage or the available memory, on high speed networks, zlib can end up showing a much lower average compression ratio in order to fill an uplink. With SLZ, no extra memory usage is needed for compression, and the CPU usage is reduced by 2.5-3.5 times, meaning that the throttling will happen at much higher data rates. The net effect is a significant improvement on the compression ratio compared to zlib at a given resource usage.

HAProxy running on a single core of a core i5-3320M, with 500 concurrent users, the default 16kB buffers, and accessing www.haproxy.org (76 kB) shows the following measures with traffic leaving the machine via a gigabit Ethernet link (hence the 976 Mbps of IP traffic) after being compressed with zlib and SLZ, respectively :

At 100% CPU (no CPU usage limit) :

At 85% CPU (limited) :

In the CPU-limited test, once CPU usage limit was reached, blocks were sent uncompressed. This explains how the gigabit link was always saturated, and it is then the input bandwidth which varies according to the average compression ratio. In all situations, SLZ resulted in bandwidth savings 3 times higher than zlib.

No magics inside!

There are 6 key points having a large impact on compression speed in any LZ- based compressor :

  1. dictionary lookup : looking up a sequence of bytes in a recent window is not necessarily fast. SLZ uses a hash-based technique similar to LZO or LZ4, focusing on keeping track of only the most recent occurrence of a given sequence. The hash table contains the absolute position in the stream of the last occurrence of the hashed entry. This table is initialized to impossible values prior to compressing so that unused entries are not even evaluated, as it was found to be faster than not initializing them. During tests, it was found that hashing 4 bytes to one value among 4096-65536 resulted in the best compression ratio, and that 8192 entries was the best tradeoff between speed and compression ratio. Smaller hash tables cause more collisions hurting the compression ratio, and larger ones experience a significant slowdown on certain datasets due to the hashtable having more difficulties to fit entirely in the CPU cache. The size remains configurable at build time using SLZ_HASH_BITS.
  2. indexing : if sequences of less than 3 bytes are considered for a lookup, since we only keep the last occurrence, there's a high risk of not keeping the most useful sequence which has large chances of matching a similar word. For example in HTML a lot of occurrences of "><", "</", "/>" can be found so the probability that the one from "</a>" is picked instead of the one from "</td>" will make it hard to find long matches. Conversely, sequences larger than 4 bytes will only match once in a while and will prevent shorter series of characters from being matched. Experimentation has shown that hashing 3 bytes only provided the same compression ratio as hashing 4 bytes, with up to 10% faster input processing thanks to the reduced cost of hashing. Indexing all the repeated data caused a significant performance drop (about 5%) for less than 1% compression ratio improvement, so only the beginning of each match is indexed. When a match is found, SLZ also checks whether it extends backwards into the pending literals, which recovers bytes that were missed without having to index more.
  3. longest match : the longest match measurement is very similar to an strcmp() call except that we count similar characters. In order to avoid this slow operation most of the time, we store in the hash table a copy of the 4 bytes that were hashed. This makes it easy to perform a comparison and to skip those which do not match, then to only start the longest match measurement for parts which already match on the first 4 bytes. With a 13-bits hash on 32-bit character sequence, there's a theorical 1 chance over 524288 that a matching hash implies a matching sequence. But in practice on selected text this chance is significantly higher since not all 32 bits are used, it's more like 24 bits (6 bits per char), so it's one chance over 2048 that an entry will match. That's still 2047/2048 useless calls to the memmatch function that are saved this way. The match itself is then measured one machine word at a time, the first differing byte being located by counting the trailing zeroes of the XOR of the two words.
  4. encoding the output : deflate is often criticized for its bit-oriented stream which is expensive to manipulate as it requires bit shifts. Moreover, on top of this the deflate stream supports several encoding modes, the most common one being dynamic huffman trees, which requires some significant maintenance to keep a valid tree up to date and which SLZ doesn't use. Instead, SLZ uses static huffman trees, and when too many binary entries are found to keep this encoding efficient, it switches to the plain literal mode. Proceeding like this comes with a significant benefit : all code sequences, all distances and all lengths are stored as their respective huffman output bits. This avoids an encoding phase, sequences of bits are immediately sent to the destination buffer without any further operation, and remain reasonably cheap compared to the more common usages. On 64-bit machines, the bits are accumulated in a machine word and a whole match (length and distance) is written with a single unaligned store. Long runs of literals that do not match anything are skipped and sent as plain literals without being looked up (at levels 1 to 3). The text content type uses the same principle with a precomputed tree trained on web contents. It is declared as a dynamic tree in the stream, so that any decoder accepts it, but it never changes, and is still used through its precomputed output bits.
  5. checksumming : 3 checksum methods exist depending on the encoding format. RFC1951 (raw deflate) doesn't involve any checksum. RFC1952 (gzip) uses a CRC-32 which is somewhat expensive but can be optimized in various ways. On x86 CPUs with the carry-less multiplication instruction (PCLMULQDQ), detected at boot time, SLZ computes it by folding 16 bytes at a time, which makes it almost free. On ARMv8, the CPU's CRC-32 instructions are used when available. Other machines use pre-computed tables processing 4 bytes at a time. RFC1950 (zlib format) uses an Adler-32 checksum which theorically involves modulus (divide) but which can be optimized as it's done with a number very close to a power of two. This format matters more than it seems, because it is what browsers call "deflate". Its two sums form a long dependency chain which is hard to speed up, but on machines with SIMD units (SSE2 on x86, NEON on ARM), SLZ splits the input into 32-byte blocks and sums each byte offset of the blocks independently. This is written in plain C that the compiler vectorizes, and made the zlib format 12 to 23% faster depending on the CPU. It is now about as fast as gzip on x86 and faster than gzip on ARM. The checksum is computed over all the input at once before compressing, which also contributes to preloading the input data into the caches.
  6. implementation-specific optimizations : modern CPUs can load unaligned words from memory with minimal to no overhead. On architectures that support this (ix86, x86_64, armv7, armv8), this is used to limit the number of operations and memory accesses, both to compare data and to write the output. The bit reversal needed by deflate's huffman codes uses the dedicated instruction on ARM. Some CPUs have a smaller cache and the direct mapping between distances and huffman sequences can make it thrash a lot. Some experimentations were made using a direct mapping only for shortest distances (the most common ones), but results were not encouraging for now as a cache miss is not completely offset by the amount of extra operations.

These three factors have a significant impact on the compression ratio :

  1. collisions in the dictionary, caused by suboptimal hash distribution. Here with 8k entries we have what looks like the best tradeoff between speed and compression ratio for most workloads tested, and it can easily be changed using a macro. This impact is dampened a bit by the backward lookup upon a match, based on the estimate that if a collision happens on the first byte, it will not necessarily happen on the next one, so that a match of 5 bytes or more can still be found.
  2. output encoding : the dynamic huffman trees would definitely shorten the output stream but at an important performance cost. But even with fixed trees it is possible to waste a lot of space. First, input bytes 144 to 255 are encoded on 9 bits per byte. SLZ constantly monitors the output stream to decide when it's preferable to switch to plain literals which cost exactly 52 bits to switch back and forth and will not inflate the output stream any further. Second, referencing a match can be longer than the data it replaces. This is true for short sequences, but it can be true even for a long sequence in the middle of a series of plain literals. By monitoring the output stream and verifying the encoding cost before switching, it is possible to bound the overhead for non-compressible data to the base 5 bytes per 64 kB plus the headers and less than 50 bytes per call (published as SLZ_MAX_OVERHEAD() in slz.h). The text tree costs about 80 bytes each time a block is opened, so a block is only opened once the savings are known to repay it.
  3. content type : most of the contents compressed by HTTP servers and gateways is text (HTML, CSS, JS, JSON, SVG...), on which zlib's default fixed huffman tree is far from optimal, as it encodes most letters on 8 bits and match lengths on 7 bits or more. Since 1.4, an application knowing that it compresses such contents can call slz_set_content_type(strm, SLZ_CT_TEXT) after initializing the stream. Instead of the fixed tree, SLZ then uses a static tree that was trained on web contents, declared at the beginning of each block in about 80 bytes. It remains as cheap to use as the fixed one, at the expense of a few percent of speed, and shrinks the output of such contents by 12 to 18%. It is not suited to binary contents nor to contents of less than 1-2 kB.

Get it!

SLZ is provided as a library with a few extra tools (eg: zenc, a compressor emitting the various formats, and zdec, a decompressor based on uslz which can decompress any gzip, zlib or raw deflate stream, not only those produced by SLZ, and detects the gzip and zlib formats by itself; see the reference tests above for its performance). It is distributed under the X11 license, meaning that you can do a lot of things with it, such as merge it into GPL or BSD-licensed programs, as well as use it in proprietary software. Contributions are welcome and should be sent as Git patches (see "git format-patch") and will be made under the same license exclusively.

Implementations

External links


Created 2015-04-06, last updated 2026-11-08 - Willy Tarreau