Introduction
In the tech world, code rewrites are not uncommon, but they often come with considerable challenges. Today, we dive into the journey of the Roc team's rewrite of their compiler from Rust to Zig, an endeavor that took a year and a half of hard work.
Why Move from Rust to Zig?
The decision to rewrite a project as vast as the Roc compiler, boasting 300,000 lines of code, is not taken lightly. Rust is renowned for its memory safety and performance, so why switch? The answer lies in the specific needs of the Roc project. Zig offers more explicit memory management and finer control, crucial for some low-level projects. Additionally, Zig is designed to be simple and readable, facilitating long-term code maintenance.
Achieving Feature Parity
Recently, the team reached a major milestone: feature parity with the former Rust compiler. This means that every feature present in the old compiler is now available in the Zig version. A concrete example is the update of the Rocci Bird game, written in Roc, which was successfully recompiled using the new compiler. The new WebAssembly binary is just 31KB, compared to more than double that size previously.
Challenges Faced
Rewriting a project of this magnitude is not without its hurdles. Collaboration was key, with notable contributions from Anthony Bullard and Sam Mohr on the new parser, and Jared Ramirez on the type-checker. Fuzzer errors and other bugs were methodically addressed, thanks to members like JRI98.
Zig offers a different approach which, though beneficial in the long run, required rethinking some fundamental parts of the compiler. The team had to manage deep architectural differences between Rust and Zig, particularly in terms of memory management and static typing.
The Future of Roc
With version 0.1.0 of the new compiler slated for later this year, the team is confident in Zig's potential to enhance the project's performance and maintainability. User feedback, such as that from Niclas Åhdén, continues to guide future improvements.
Conclusion
The transition from Rust to Zig for the Roc compiler is an inspiring example of the boldness and innovation required in modern software development. If you're curious about how such a rewrite could benefit your project, let's discuss your project in 15 minutes.