Skip to main content
coreydaley.dev

Making 7z, xz, and ZIP use more than one core

At the end of May, a benchmark showed something embarrassing: creating a 7z archive in Purrchiver took about 57 seconds on a 170 MB folder that 7-Zip compressed in 7. Same output, eight times slower. Over May 28 and 29 that gap — and the matching ones for xz and ZIP — closed.

xz

libarchive can already hand xz compression to liblzma’s multithreaded encoder; it just has to be told to. Purrchiver now does, with a thread count chosen by memory, not just by cores: each thread needs a buffer about three times the compression dictionary, so higher compression levels get fewer threads. At the default setting that’s up to eight threads, leaving one core free so a long compress doesn’t take over your Mac.

7z

7z was harder. liblzma has no multithreaded encoder for the raw LZMA2 data that goes inside a 7z, which is why libarchive’s 7z writer ran on one core.

The fix was a bridge to a different encoder: the multithreaded LZMA2 encoder in the LZMA SDK, the library 7-Zip itself is built on. The result at the time measured about 6.5 seconds against 7-Zip’s 7.0 on the same 170 MB, with the archive within 0.01% of 7-Zip’s size. The cost is memory — comparable to 7-Zip’s own multithreaded mode — and a few percent of compression ratio, because blocks compressed in parallel can’t share a dictionary.

ZIP

libarchive’s ZIP writer works through files one at a time, start to finish. Rather than rewrite the ZIP format, Purrchiver splits the files into balanced groups, compresses each group as a separate archive at the same time, then stitches the pieces together by joining their data and building one combined directory at the end.

A folder of 3,000 small files went from 0.92 seconds to 0.14. It works for AES-256 encrypted ZIPs too. ZIPs of 4 GiB or more still use the one-at-a-time path for now.


The benchmarks page, published the same day, has the full numbers for every format against the standard command-line tools.

Related posts