Ever wonder why iOS and macOS are so large? A big portion of that is poor data compression. Testing with Crush-LZ and apfscompress shows that Apple could shrink iOS & macOS system installs by up to 12%, while increasing decompression speed by up to 38%.
How Apple Compresses
Apple already uses compression to reduce the size of iOS and macOS installations. The supported algorithms are stored in AppleFSCompressionTypeZlib.kext, which ships a subset of the algorithms available for apps in Compression.framework.
Of the 5 algorithms APFS supports, Apple never uses LZFSE, which they introduced in 2015 as a faster alternative to zlib. Today, macOS uses LZVN and LZBITMAP, and iOS uses zlib and LZBITMAP:
There are two primary problems:
- Apple’s choice of algorithms is suboptimal
Compression.frameworkprioritizes speed over ratio
Each of these gaps costs storage space and performance.
Picking the Right Algorithm
For building an operating system, compression time is a one-time cost - a few minutes of build time per release. Ratio and decompression speed are what matter, as they are paid on every software update and every system startup for billions of users. Below is a graph of the algorithms on a ratio/decompression speed chart:
Remember how macOS uses LZVN? LZFSE is better in both decompression speed and ratio, so swapping LZVN for LZFSE is an obvious win for macOS.
Battle Royale
It turns out we can do better than just swapping the primary algorithms. APFS allows us to pick the best format on a per-file basis - this slightly boosts both ratio and speed, as LZBITMAP wins a small portion of files outright.
Using Apple’s own encoders, that makes the compressed iOS files about 3% smaller, with 16% faster decompression, and the compressed macOS files about 8% smaller, with 23% faster decompression:
This is the limit with Apple’s compressors - but we can do better. For a one-time operating system build, there’s no reason to use a compressor built for high-speed compression rather than optimized data packing.
Optimizing the Compressor
Crush-LZ spends additional CPU time during compression to represent the same data in less space. This results in substantial ratio gains for LZFSE and LZBITMAP, with small gains for zlib. LZVN and LZ4 are not competitive even when optimized.
In this chart, no file is ever switched to a slower algorithm, and both configurations decompress faster than Apple’s current shipping config. Crush-LZ’s maximum ratio configuration for LZFSE is slightly slower to decompress than Apple’s LZFSE, but speed can be restored by giving back some of the ratio gains using the encoder’s tuple_tax parameter.
LZFSE gains substantially from additional compute, replacing most zlib-compressed files on iOS. LZVN is not competitive on either platform once LZBITMAP is optimized.
Mapping the Frontier
The Pareto frontier includes a variety of other space/performance tradeoffs. Notably, Apple is not remotely close to this frontier:
Crush-LZ offers material gains over Apple’s current configurations:
- iOS takes 600 MB less space, and decompresses 38% faster (smallest)
- macOS takes 1.825 GB less space, and decompresses 18% faster (balanced)
After adding back in uncompressed files, this cuts about 4% off the current size of an iOS install, and 12% off the size of a macOS install.
Potential Surprises
Apple may be planning to replace LZVN / LZFSE entirely with LZMesh, a new compression algorithm that arrived in Compression.framework recently, but has not yet reached APFS. LZMesh is a better algorithm overall, so this would be an excellent outcome.
Crush-LZ includes a compressor for LZMesh, offering ratio gains of nearly 2% over Apple’s LZMesh implementation. It is not as heavily optimized as the LZFSE & LZBITMAP compressors, but I plan to optimize it further if LZMesh reaches APFS.
Final Notes
I have a tendency to run out of storage space, so I’m really hoping to see compression improvements for macOS and iOS in the future. Alas, except for custom-kernel Macs and VMs, denser system images are on Apple’s timeline.
However Crush-LZ and apfscompress can do far more than just compress system files. Shrink Wizard uses apfscompress to save disk space by compressing applications on your device, and apfscompress can shrink DMGs so you can deliver smaller precompressed app installs to users.
May your files be ever-shrinking, and your storage remain plentiful.