Bun Rewrites Entire Codebase from Zig to Rust in 11 Days
Bun, the all-in-one JavaScript toolkit, recently completed a significant refactoring of its entire codebase from Zig to Rust. This ambitious project, which cost the equivalent of $165,000 and utilized 64 parallel AI agents, was finished in just 11 days. The rewrite, involving a million lines of code, reportedly fixed 128 old bugs, reduced the binary size by 20%, and improved performance by a few percentage points. The Rust-based code has been powering Claude since June without any noticeable issues.
The Controversy
Despite Bun's positive announcement, Andrew Kelly, the creator of Zig, expressed strong criticism. Kelly claimed that Bun's benchmarks were misleading and that the Zig team was actually relieved to see Bun transition away from their language. He stated that Bun's Zig codebase was internally used by the Zig team as an example of how not to write Zig. Kelly also made personal remarks about Jared, Bun's founder, suggesting he was producing "SLOP" even before the advent of large language models (LLMs).
The Motivation for the Rewrite
The decision to rewrite Bun's codebase stemmed from fundamental issues related to memory management and the future integration of AI-generated code.
Memory Management Challenges in Zig
Bun was originally written in Zig, a language that requires manual memory management. This presented a significant challenge because Bun embeds JavaScriptCore, Safari's garbage-collected JavaScript engine. This created a hybrid environment where half of Bun's objects were managed by the garbage collector, while the other half resided in manually managed Zig memory. The constant need for these two systems to manage pointers to each other led to a complex and error-prone codebase.
Bun's change logs frequently documented bugs arising from this memory management paradigm, including:
- Reading memory that had already been freed.
- Freeing the same memory twice.
- Failing to free memory at all.
One notable example was a development server memory leak, where three megabytes were leaked with every rebuild due to a single error path neglecting cleanup. While memory bugs alone might not have necessitated a full rewrite, they were a significant contributing factor.
The AI Factor and Zig's Stance
A more pressing issue arose after Anthropic acquired Bun: the majority of Bun's future code was intended to be written by Claude, an AI. Zig, however, is known for its anti-AI stance. The Zig community reportedly refuses LLM-generated pull requests and may even close security reports if the bug was discovered by AI.
Furthermore, AI models are not proficient at writing Zig code due to several reasons:
- Limited Training Data: There is very little Zig code available on the internet for AI models to train on.
- Language Instability: Zig has not yet reached version 1.0, meaning it frequently introduces breaking changes, making it difficult for AI models to keep up.
These factors made it clear that Zig was not a sustainable language for Bun's AI-driven future.
The Rewrite Process
In early May, Jared decided to address both memory management and AI compatibility by porting all 535,000 lines of Zig to Rust. Rust's borrow checker would move memory management into the type system, transforming most memory mismanagement issues into compile-time errors rather than runtime errors.
The rewrite process itself was innovative and heavily relied on AI:
- Porting Guide Generation: Claude spent hours studying Bun's existing codebase to produce a comprehensive porting guide.
- Lifetime Tracing: A workflow was implemented to trace the lifetime of every struct field, generating a massive spreadsheet that documented years of tribal knowledge regarding memory allocation and deallocation.
- Parallel AI Agents: 64 parallel Claude agents were deployed across four Git worktrees to perform the actual code conversion. At one point, these agents were generating 1,300 lines of Rust code per minute.
- Adversarial Review: To ensure code quality, each implementer agent was paired with two adversarial reviewer Claude agents. These reviewers operated in separate context windows, with their sole purpose being to assume the code was incorrect and identify flaws.
The Results and Aftermath
After 11 days and 6,52 commits, Bun's entire test suite passed on every platform. The port successfully fixed 128 long-standing bugs, resolved the development server's memory leak issues, and reduced binary sizes by 20%.
Andrew Kelly's response to Bun's announcement was highly critical. He suggested that the "divorce" between Bun and Zig had been brewing for years, with the Zig team privately using Bun's codebase as an example of poor Zig programming. Kelly also made personal attacks against Jared, attributing his "beginner energy" to his decision to skip college and pursue startup ventures. He also relayed secondhand reports of Jared being a difficult manager.
Beyond the personal attacks, Kelly raised several technical objections:
- He argued that most of the performance gains were due to link-time optimization, a feature Zig has always supported.
- He claimed that the binary size reduction was unrelated to Rust.
- He pointed out that Jared conveniently omitted any mention of compile times, where Zig would almost certainly outperform Rust.
Conclusion
The Bun rewrite has sparked a debate about the efficacy of AI-powered code rewrites and the merits of different programming languages. While Bun touts significant improvements, Andrew Kelly's counter-arguments highlight potential biases and alternative explanations for the observed benefits. The situation has resulted in Zig losing a prominent user, Andrew Kelly expressing strong emotions, and Jared receiving public criticism regarding his technical approach.
Code Rabbit: A Solution for AI-Generated Code Review
The video also features a sponsorship by Code Rabbit, a tool designed to streamline the review of AI-generated code. Code Rabbit Review transforms pull requests into guided walkthroughs by:
- Grouping related changes into cohorts.
- Ordering them into layers.
- Adding AI summaries for quick navigation.
This approach aims to make reviewing large, AI-generated codebases more manageable, akin to an IDE experience for pull requests. Code Rabbit integrates natively with GitHub for comments and approvals and can generate inline diagrams for call flows, state changes, and schema updates. It is used by over 100,000 open-source projects, including Bun and Next.js.
Takeaways
- Bun completed a full rewrite of its million‑line JavaScript toolkit from Zig to Rust in just 11 days, using 64 parallel AI agents and costing about $165,000.
- The Rust rewrite fixed 128 long‑standing bugs, eliminated a development‑server memory leak, and reduced the binary size by roughly 20% while delivering modest performance gains.
- The primary motivations were Zig’s manual memory management complexities and its anti‑AI stance, which conflicted with Bun’s plan to generate most future code with Claude.
- The conversion relied on AI‑driven processes: Claude created a porting guide, traced struct lifetimes, and 64 agents produced up to 1,300 lines of Rust per minute, each reviewed by adversarial Claude agents.
- Zig creator Andrew Kelly criticized the rewrite, claiming the performance and size improvements stem from link‑time optimizations and that Zig would still outperform Rust in compile time, while also making personal attacks on Bun’s founder.
Frequently Asked Questions
Why did Bun switch from Zig to Rust for its codebase?
Bun switched because Zig’s manual memory management created frequent bugs and conflicted with Bun’s AI‑driven development strategy, as Zig’s community resists LLM‑generated code and lacks stable training data. Rust’s borrow checker moves memory safety into the type system and is more compatible with Claude‑generated code, addressing both stability and future AI integration.
How did the 64 parallel AI agents accelerate Bun’s rewrite from Zig to Rust?
The 64 Claude agents were split across four Git worktrees, each generating up to 1,300 lines of Rust per minute while following a detailed porting guide and lifetime‑tracing spreadsheet; every piece of code was paired with two adversarial reviewer agents that deliberately searched for mistakes, allowing rapid, automated conversion and continuous quality checks that compressed a million‑line rewrite into 11 days.
Who is Fireship on YouTube?
Fireship is a YouTube channel that publishes videos on a range of topics. Browse more summaries from this channel below.
Does this page include the full transcript of the video?
Yes, the full transcript for this video is available on this page. Click 'Show transcript' in the sidebar to read it.
Helpful resources related to this video
If you want to practice or explore the concepts discussed in the video, these commonly used tools may help.
Links may be affiliate links. We only include resources that are genuinely relevant to the topic.