$ uname -a A_BLOG_ABOUT_UNIX_TOOLS_AND_THE_BEAUTY_OF_SIMPLE_SYSTEMS
SYS.YEAR  2026
← Back to articles
2026-09-05ARTICLE

Pkg Compress

Compressing FreeBSD Packages a Little Further

FreeBSD packages already use a good format: a tar archive compressed with Zstandard. The practical way to reduce their size without introducing a new package format is to increase the zstd compression level:

pkg create -f tzst -l 22 -T 0 package-name

-T 0 uses all detected CPUs for compression. The usual pkg default corresponds to zstd level 19, so level 22 is the interesting comparison—not zstd's command-line default of 3.

A small binary test

The following stripped and unstripped FreeBSD binaries were placed in tar archives and compressed with zstd 1.5.7:

Category Programs Tar Level 3 Level 19 Level 22
C jq, curl, sqlite3 2.43 MB 1.32 MB 1.14 MB 1.14 MB
Rust ripgrep, procs 22.00 MB 6.23 MB 5.00 MB 4.77 MB
Go fzf, dive 14.43 MB 5.74 MB 4.42 MB 4.11 MB

Level 22 provided essentially nothing for the small dynamically linked C programs, about 5% beyond level 19 for Rust, and about 7% for Go. Level-19 decompression remained around 0.8–1.15 GB/s on this system. These are binary-only microbenchmarks, not complete packages, so real results will depend on documentation, metadata, and pre-compressed assets.

Before tuning compression, also check whether binaries are stripped. The tested ripgrep contained debug information; splitting or removing debug symbols may save more space than changing compression levels.

What about a zstd dictionary?

Zstd supports trained custom dictionaries:

zstd --train samples/* -o binaries.dict
zstd -19 -D binaries.dict package.tar
zstd -d -D binaries.dict package.tar.zst

However, the decoder must have the identical dictionary. Current pkg and libarchive package handling do not provide a compatible way to distribute and select one. A repository would need to version, authenticate, retain, and bootstrap its dictionaries.

Dictionaries are most effective for many small, similar inputs. A solid package tar stream already lets zstd learn from earlier content, making a dictionary less valuable for large packages. They may still be worth testing for package metadata or homogeneous families of small Go or Rust programs. Use many representative samples and evaluate on different versions than those used for training.

Multithreaded extraction

Zstd supports multithreaded compression, but a normal zstd frame is principally decompressed by one thread. Parallel decompression requires independent frames and installer support. Libarchive can create multiple zstd frames, including frame-per-file archives, but this sacrifices some cross-file compression and does not automatically make pkg extraction parallel.

Since ordinary zstd decompression is already very fast, package installation may instead be limited by filesystem writes, hashing, scripts, or package-database updates.

A base.txz experiment

FreeBSD 15.1-RELEASE's amd64 base.txz is a different workload from an individual package: it expands to a 715,376,640-byte tar archive. Its shipped XZ stream is 164,624,792 bytes and uses 29 independent 24 MiB blocks with an 8 MiB LZMA2 dictionary.

Custom zstd dictionaries were trained on files from 15.0 and tested only on the held-out 15.1 tar. Broad 32, 110, and 256 KiB dictionaries and a 110 KiB binary/library dictionary all made zstd-19 larger. A raw 1 MiB prefix from the earlier tar saved 353 KiB from the stream, but lost about 695 KiB after including the dictionary. At zstd-22 it also made compression worse.

zstd-19 dictionary Stream size Net change vs. no dictionary
None 172,700,251 —
Broad, 32 KiB 173,054,766 +354,515
Broad, 110 KiB 173,011,648 +311,397
Broad, 256 KiB 172,758,328 +58,077
Binaries/libraries, 110 KiB 173,011,668 +311,417
Raw previous-release prefix, 256 KiB 172,680,573 +242,466 including dictionary
Raw previous-release prefix, 1 MiB 172,347,024 +695,349 including dictionary

An optimistic same-system test did not change the result. Dictionaries trained from the installed 15.1 executables and libraries—all close matches for the target—made the stream larger before dictionary overhead:

110 KiB training corpus Stream size Net penalty including dictionary
/usr/bin + /usr/sbin 173,011,752 +424,141 bytes
/lib 173,012,406 +424,795 bytes
/usr/lib 173,013,700 +426,089 bytes
/lib + /usr/lib 173,013,391 +425,780 bytes

Requesting 256 KiB, 1 MiB, and 4 MiB executable dictionaries did not help either. FastCover selected effective outputs of only 86,160, 33,315, and 58,110 bytes, respectively, and every compressed stream remained larger than the no-dictionary result. The trainer warned that the 4 MiB request was oversized for the 18.4 MiB it could use after its per-file sample cap.

Encoding Size Change from shipped XZ Cached decode
Shipped XZ, 24 MiB blocks 164,624,792 — 0.87 s
zstd 19 172,700,251 +4.91% —
zstd 22 163,463,671 −0.71% 0.68 s
XZ -9e, 64 MiB blocks 155,785,216 −5.37% —
XZ -9e, 128 MiB blocks 154,382,440 −6.22% 1.38 s
XZ -9e, 256 MiB blocks 154,146,656 −6.37% 2.37 s
XZ x86 + -9e, 128 MiB blocks 153,096,020 −7.00% 1.41 s

The best balanced command on this 12-core amd64 host was:

xz -T0 --block-size=128MiB --x86 --lzma2=preset=9e \
  -c base.tar > base.txz

It retains six blocks that xz -T0 can decode in parallel and saves 11,528,772 bytes over the release archive. The x86 branch filter is reversible for arbitrary input but is architecture-specific as an optimization; other architectures need their matching BCJ filter tested. All decode timings used the XZ 5.8.3 command-line decoder with automatic threading and warm filesystem cache. Stock bsdinstall/libarchive extraction may remain single-threaded unless it enables threaded liblzma decoding or pipelines through xz -T0. The winning archive was decompressed and verified byte-for-byte against the original tar.

Recommendation

For ordinary packages, benchmark levels 19 and 22, including cold-cache installation time. For an amd64 installer base.txz, tuned multi-block XZ currently beats zstd and custom dictionaries on size while retaining parallel decoding. Treat external dictionaries as a poor fit for one large solid tar, and investigate debug symbols before adding format complexity.

Further reading: FreeBSD pkg format, pkg-create(8), zstd dictionary training, and libarchive's zstd options.