Mozilla recently disclosed that since the Firefox 151 version released in May this year, the browser has widely used the zlib-rs library in Gzip compression and decompression processing. This is a set of Zlib implementations written in Rust, aiming to improve memory safety and overall performance while maintaining compatibility.

According to a technical note released today by the Trifecta Tech Foundation, they have been communicating with the Mozilla team about the introduction of zlib-rs for nearly two years before finally pushing the library into the official version of Firefox.

image.webp

zlib-rs is promoted as a "plug-and-play" solution that can directly replace Zlib implemented in C language. However, during the actual implementation process, Mozilla encountered some unexpected complications. Since zlib-rs uses different algorithm implementations at different compression levels, its behavior is not completely consistent with traditional Zlib, which brings certain challenges to compatibility verification and actual deployment. In addition, zlib-rs has also been affected by stability issues on Intel's 13th and 14th generation Core processors. In order to fix these CPU-related faults, Firefox had to introduce a small amount of "unsafe" code in some code paths to bypass specific hardware flaws.

Although the process has not been entirely smooth, Mozilla and the Trifecta Tech Foundation stress that the benefits of moving to zlib-rs are significant. On the one hand, compared with the traditional C version of Zlib, the migration to Rust provides better memory security for the compression and decompression logic, reducing the potential risk of out-of-bounds access or memory corruption. On the other hand, zlib-rs also brings significant improvements in performance, especially on the x86_64 platform, with considerable improvements in compression and decompression throughput.

On the AArch64 platform, the performance gains currently brought by zlib-rs are relatively small, but the Trifecta Tech Foundation points out that there is still room for optimization in the future. Compared with the Zlib implementation provided by Apple in macOS, zlib-rs has the opportunity to further improve efficiency through more refined code tuning in some scenarios, thereby achieving more consistent cross-platform performance on the Arm platform.

The Trifecta Tech Foundation detailed the entire process of introducing zlib-rs into Firefox in a blog post, including years of communication with Mozilla, trade-offs in algorithm selection and compatibility testing, and engineering trade-offs made to address Intel CPU issues. Mozilla hopes that by gradually adopting Rust implementation in key basic components, Firefox can further consolidate its reputation for security while continuing to pursue performance.

More technical details and benchmark results can be found on the Trifecta Tech Foundation blog:

https://trifectatech.org/blog/zlib-rs-in-firefox/

Currently, Firefox users are already transparently benefiting from this underlying library replacement in their daily use.