The most controversial rewrite in history just shipped...
This video details how Bun, the all-in-one JavaScript toolkit, performed a complete rewrite of its 535,000 lines of Zig code into Rust using Anthropic's Claude AI agents. The rewrite, which took only 11 days, cost $165,000 in token usage and resolved 128 bugs, shrunk the binary size by 20%, and resulted in a 2-5% speed increase. The creator of Zig, Andrew Kelley, publicly criticized Bun's code quality and their use of AI, highlighting the differing philosophies on memory management between Zig (manual) and Rust (borrow checker), and the challenges of training AI models on less established languages like Zig. The video also discusses CodeRabbit, an AI-powered code review tool, as an alternative for improving code quality.
read more
The video highlights Bun's ambitious rewrite of its core codebase from Zig to Rust, orchestrated by Anthropic's Claude AI. This decision stemmed from significant challenges with memory management in the original Zig implementation. Zig, a low-level language, requires manual memory management without compiler assistance, which led to numerous bugs in Bun's codebase, including memory leaks, use-after-free errors, and double-frees. The integration of JavaScriptCore, Safari's garbage-collected JavaScript engine, with Zig's manual memory management created a complex hybrid environment. Half of Bun’s objects were managed by JavaScriptCore’s garbage collector, while the other half resided in manually managed Zig memory. This dual approach necessitated constant pointer management between the two systems, resulting in a codebase plagued with stability issues. The official blog post mentions fixing 128 bugs, a 20% reduction in binary size, and a 2-5% performance improvement post-rewrite, attributing these gains to Rust's borrow checker and compiler-assisted tools like LeakSanitizer and Miri.
The rewrite process itself was a fascinating AI-powered endeavor. First, Claude spent three hours analyzing Bun's Zig codebase to create a porting guide, outlining patterns to map Zig constructs to Rust. Then, a dynamic workflow was implemented to trace the lifetimes of every struct field in the Zig codebase, documenting years of tribal knowledge about memory ownership and deallocation. This data was serialized into a giant spreadsheet, providing a comprehensive map for the translation. With this groundwork, 64 parallel Claude agents were deployed across four Git worktrees. These agents translated 1,448 files, generating approximately 1,300 lines of Rust code per minute at peak. To ensure code quality and prevent biases, adversarial review was incorporated, where each implementing Claude agent was paired with two reviewing Claude agents. The reviewers' sole job was to assume the code was wrong and identify bugs, operating in separate context windows to minimize bias. The entire process, which would typically take a team of engineers a year, was completed in just 11 days, costing an estimated $165,000 in API token usage, a cost that Anthropic, as Bun's acquirer, effectively absorbed.
Andrew Kelley, the creator of Zig, offered a sharp critique of Bun's decision and methodology. He argued that the claimed performance gains from the Rust rewrite were primarily due to Link-Time Optimization (LTO), a feature Zig has always supported. He also suggested that the binary size reduction was unrelated to Rust and that Bun's blog post conveniently omitted compile-time comparisons, where Zig likely outperforms Rust. Kelley expressed relief at Bun's departure from the Zig ecosystem, describing Bun's original codebase as a "prime example of How Not To Write Zig Code" due to its "hacks on top of hacks" and "abuse of assertions." He attributed this to what he termed Bun founder Jarred Sumner's "beginner energy" – a tendency to move fast and jump into problems without deep consideration. Kelley also highlighted the inherent challenges of using LLMs for code generation in less mature languages like Zig (which is pre-1.0), as limited training data and frequent breaking changes make it difficult for AI models to produce reliable code compared to more established languages like Rust.
In essence, the Bun rewrite represents a significant milestone in AI-assisted code migration, demonstrating the potential of LLMs to tackle large-scale refactoring challenges. However, it also sparked a debate about code quality, language philosophy, and the role of AI in software development, with the creator of Zig pointing out the nuances and potential pitfalls of such a rapid, AI-driven transition.