The Great Bun Rewrite
426 segments
What is Bun moving from Zig to Rust? May
4th. Look, a community note. Bun's
creator states it's a fun experiment,
not a decision to rewrite. Don't worry,
we're not rewriting Bun from Zig to
Rust, right? That I mean, that would be
silly, right? And 9 days later, the plan
for tomorrow's dog food Bun's Rust port
on Claude code internally and start
writing a blog post. I thought it was
just for fun. 2 months later, rewriting
Bun in Rust, the blog post of why and
how they did it. That escalated quickly.
Okay, that went from fun Z to production
in almost no time. Of course, this
rewrite took 11 days with the help of
Claude Fable pre-release before all of
us plebs had the chance to use it. Jared
was out there just crushing rewrites in
11 days. And of course, that rewrite
took 690 million output tokens, 5.9
billion uncashed input token reads, and
72 billion cached input token reads for
around $165,000
of API pricing. Hey, brother.
That's a steal of a deal, okay? So yes,
obviously after reading all of this, I I
just have to we we have to talk about
this, okay? So I I just want you to sit
down and I I
I'm just going to tell you something
that honestly, I don't think this is the
worst use of tokens ever. I actually
think this is a pretty good idea. I
don't Okay, hold hold on. Put put down
your pitchfork, okay? Hey, hey, we're
just sitting down. Let's just sit down.
Let's just talk about it for a little
bit, okay? And while you center yourself
and calm yourself down, I'd like to say
thank you to my sponsor. One of the more
terrifying things you can do as a
company is to contract out software, and
I know a lot of companies are
contracting out their mobile
applications. The thing that makes this
so dangerous is that it's a black box
experience. You hope you get back
software that is complete, that is going
to be well tested, that ultimately will
be extensible as future needs require.
The last thing I wanted someone else to
say to Claude, "Hey, mobile app, no
mistakes." It does make mistakes. This
is why I trust the guys over at Infinite
Red. I have had many a long talks with
the CEO about software quality and the
way to really build robust and good
systems, and I love everything he has to
say. And everything that I have seen
with my own eyes, the quality of
software that they build is very, very
good. I hope we're feeling better now.
Are we? Okay, good. What I'm going to do
is I'm going to tell you why they did
this kind of rewrite, and we're going to
go over the blog on that. And then I'm
going to tell you kind of my thoughts on
the cost, the $165,000
bill. And then lastly, we're going to go
over some what I would consider actually
pretty cool techniques that you can use
in your day-to-day. So, let's get
started. So, I think the best place to
start in this blog post is to start with
why did they do this? And at the very
tippity top, he talks about how, "Hey,
Zig was super instrumental for them
getting started. He's very happy that he
used it." But he said, "Hey, Bun scope
has been a challenge for stability.
Here's a small sample of bugs we fixed
in 1.3.14.
Use after free crash, use after free,
use after free, use after free, crash,
out of bounds, heap out of bounds,
memory leak, memory leak, memory leak,
something to do with SSL wrapper and
knit, double free crash, race
conditions, this thing was never freed."
As you can see, memory problem after
memory problem after memory problem.
Now, Jared attributes a lot of the
difficulty with handling memory and all
these problems due to the just the
unusual set of software he is writing.
Other users of Zig don't have the bugs
we had. And mixing GC with manually
managed memory is an uncommon enough
thing for software to need that no
language really designs for it. He goes
on and continues to praise Zig. "We
wouldn't have gotten this far if not for
Zig. I've always been grateful. Until
very recently, programming language
choice was a one-way decision for a
project like Bun." He goes on and and
gives kind of his justification for why
defer is a bit more tricky and why
something like Ray or drop in Rust is
just going to be a superior way for him
to be able to handle managed memory
inside of this garbage collected
universe. And the core of his argument
is right here. Many projects opt to
answer these kind of questions through a
style guide. Tiger Beetle's Tiger style
is an example in Zig and Google's 31,000
word C++ style guide is another. The
challenge with style guides is
enforcement. How do you make sure the
style guide is followed? Historically,
code review was the answer with the best
effort enforcement via linters and
static analyzers. What he's trying to
say is that there's not a language
enforcement, instead there is a
community enforcement around here like,
"Hey, we've all agreed to write code in
a very specific way. Anybody who breaks
this pact, if not caught in review,
could lead to bugs." And he says, "Hey,
this is getting really difficult for us
to maintain." Then he even specifically
calls out C++. About 20% of Bun's code
is written in C++. Bun embeds several
C++ libraries. It would have been really
easy to use C++, but then at the end of
the day, you're still doing style guide
enforcement is his general argument. And
so, that's why he looked towards Rust
because Rust, it's not a style guide
enforcement. It's not like, "Hey, did
you make sure you used a shared pointer
there or a unique pointer there?" No,
it's like, "Hey, you can't You don't
have an option. This is a language that
enforces it at the language at the
compiler level." Now, I'm positive
there's a lot of arguments that are
going to be like, "Well, actually, this
is just a skill issue on his behalf."
And just, you know, Rust Sure, Rust will
save you, but at the end of the day, you
could have just done this with Zig.
Sure, I do agree with all those general
sentiments, and I think that that is a
real thing. But I also think it's a very
hard project. I'm not the one paint
maintaining it. I'm not the one in
charge of it. So, if he wants to go robo
rewrite something via the help of an LLM
to get rid of this class of bugs, you
know what? More power to the guy. All
right, so I think it's best to talk
about the $165,000
thing now, okay? I think a lot of people
out there when they hear that, they just
hear that money up front, and they go,
"Oh, this was a terrible idea. How could
you pay $165,000?" Okay, Claude token
rich man, not everybody uh one of us has
that. But, I've been a part of a
rewrite. The rewrite I did was for an
like an internal data consistency thing
for Netflix. And when I did that
rewrite, it took a three of us
approximately 1 year to get everything
back into place pretty well. Now, that 1
year effectively cost that company over
a million dollars when you consider all
the management, all the interacting with
other teams, plus my time, plus somebody
else's time, plus another person's time.
One of those people was a staff
engineer. You can just imagine that all
those costs come together, plus, you
know, medical insurance, all that stuff.
When you think about the actual cost of
an engineer, like Netflix paid a lot of
money for us three to go on effectively
a mental adventure on rewriting a piece
of software that already worked
previously. Granted, the new thing was
better, but nonetheless, it took a long
time, a lot of money, and features
froze. So, now when you think of it in
that kind of perspective, 11 days to
rewrite $160,000?
To me, that actually doesn't seem bad.
And the reason why I don't say it seems
bad, I think it seems actually awesome.
Because let's just pretend that given
enough kind of style guides and and
assisting, you could get software that
looks maybe 50% the way you actually
wanted it to look at the end. Well, for
the next month, engineers could sit
there and help kind of reshape it and
get it into something they actually
really like, and you're still leaps and
bounds cheaper and significantly faster.
So, to me, this rewrite is not absurd. I
think a lot of people just immediately
go, "Oh, this is just so stupid." They
don't actually think about it from maybe
a more holistic perspective, which is,
"Okay, let's pretend it took three
engineers three months to get the code
base back into shape after this kind of
robo rewrite." Well, guess what? That's
still way faster, and it's likely way
cheaper than it would have been some
other direction. Like, it's literally
the cost, quality, and speed thing all
put together at the exact same time.
It's one of the rare three-point
triangle W's. So, when I see like posts
or tweets talking about how ridiculous
it was to spend 160 grand, I look at
that like, "Damn, that's a steal. That
is an absolute steal." And then
afterwards, you get I get to do
refactorbation, my favorite activity.
Bro, let me in. All right, so let's
actually go over what he did
effectively. He kind of gives this nice
little piece of pseudo code that says,
"Hey, go do the task. Then after doing
the task, I want you to go get two
reviews. And then I want you to apply
the review feedback to each one of
those." And how he does the reviews is
he actually spawns a completely separate
agent with no context, then says, "Hey,
here's the change set. Here's what I
wanted it to do. Tell me, did it do the
thing? Can you find any problems? Can
you write about any of the bugs you see?
Can you write about like, you know,
gives it some basic things." I don't
know why he does two reviews. I wonder
if there's like maybe two different
things they had the reviewers do. I
don't know, but you could imagine that
each of the reviews brought a different
kind of goal or orientation to it, and
this allowed good feedback. And
honestly, I've been doing effectively
this approach for the last eight months,
and this has been one of the greatest
uses of AI I have had so far. In fact, I
have this guy right here where when I
go, "Hey, prime agent review," it's
going to say, "Hey, is this from like a
cursor cloud agent run, or do you just
want me to go straight to a GitHub PR?"
And when I do that, it's going to go
say, "Okay, go give me the URL," which I
can go give it a URL. Then it's going to
ask me, "What context files do you want
to include into it? Hey, what's the
prompt? What's like the way you want me
to review it?" And then it'll go off and
churn out a review, and actually give
good feedback leaving on. And often what
I find is that about 50% of the feedback
is actually good. Like, "Hey, that's an
actually good thing to go catch. Don't
forget to go do that." And I so I'm a
huge fan of this. So, this thing right
here is honestly super cool. To spare
you the long reading, I'll just kind of
give you the fast track. Effectively,
what the LLM did it would like whole
translate a one Zig file into one Rust
file and then do those two reviews. And
then after going through all of the Zig
code and just literally mechanically
translating it, at the end of the day,
there was 16,000 errors. And so, as you
can watch this thing go, this is the
replaying of those errors being fixed.
Effectively, what happened is that they
had a whole bunch of agents running in
different work trees just taking one
error, fixing the error, then having the
two reviewers review, then fixing the
feedback, and then just keep on going
through this process all the way until
all of the items were fixed, all the
errors were fixed, and now all of a
sudden you have a working version of
Bun. Of course, the moment that he ran
bun dash version, it had immediately
linked linker error. So, it did it
didn't really work, it just immediately
panicked. So, the next step was to
actually start going through and making
every single unit test pass. And so, he
did the exact same thing again, set up
these huge amount of Claude code workers
to all run, and they'd all slice up the
amount of tests and just fix them. And
it looked something like this if
visualized where all these different
shards are off running and making sure
everything is working. And eventually,
he got to the point where after so many
days, effectively, all the tests pass.
And it was at this point where he
decided, "Okay, yeah, this rewrite might
actually work. This actually might be a
thing." So, after 11 days, there were
6,502
commits yielding approximately 1.8
million lines written/rewritten,
$165,000.
And honestly, it's kind of it's kind of
a neat thing. I I mean, I know that I
rag a lot on AI, but I do think that
this is one of its strong suits, which
is translating one thing to the next
thing. Like that, I think that this
probably worked out reasonably well. At
least the Rust code will be,
you know, somewhat in the same shape
that the Zig code was in. And so, their
ability to move through the code base is
probably not, honestly, all that bad.
Now, is this going to be great idiomatic
Rust? You know, I know there was a lot
of porting, there's a lot of style
guides. Hey, there's a lot of work on
the how they're going to be doing
lifetimes and everything up front. So,
it's probably not all that far off. But,
my guess is that they had to put in a
lot of effort afterwards to really start
kind of cleaning up the code base and
making everything work. But, you know,
they also have a lot of engineers. They
have a lot They got a lot of Fable
tokens, okay? They got that They got
that on guard rated Fable, okay? They
got that operator Fable. Not us poors,
okay? We don't even get We don't even
get that kind of Fable at all, okay? We
get Fable at home. I guess the real
takeaway is going to be this. At some
point, Bun 1.4 is going to be released
to the public. And when they release it,
what's going to happen? I don't know.
Will it be awesome? Will everything be
all thumbs up? Or is it going to be
absolutely horrifying? We're going to
find out because there is a non-zero
chance that Bun 1.4 gets released. And
guess what? Hundreds of thousands of
people just have their stuff broken on
their computer cuz they update Bun,
breaks CI that gets the latest Bun,
breaks.
I don't know. It's going to be really
interesting. Or, nothing happens. And
then everybody goes, "Huh.
I guess that's pretty cool." By the way,
if I'm sweating, if I'm looking shiny, I
think I am looking shiny in here. Oh my
gosh, I It's 100° out in my horse barn
is not, in fact, keeping me cool. Now,
you're probably wondering, why am I not
having just like a "Ah, this is such a
sucky thing" kind of response to all
this. I just kind of want to throw this
out there. One of my favorite things in
the universe to do is to build tools.
And when I looked at this, especially
when I looked at how he kind of
organized changes, the feedback, and the
fixes, like when I look at this, I'm
like, "Oh man, I love this. This is dev
tools. I love dev tools stuff." And this
is kind of why I built actually this
Prime Agent review. I've been doing this
more manually or using other tools and I
was like, "Dude, I should just start
really integrating a lot of this stuff
into my environment and start building
it out such that I actually have a
pretty excellent integration with cloud
agents, my local Neovim, and like how I
like to develop." And so for me, I just
love good tools. I love seeing good
tools. I love the fact that he was
actually able to get this all done. Now,
whether or not it's going to be a
success, I guess will be just gauged by
the reception of 1.4. But for me, I
honestly thought it was pretty cool. I
know. It's kind of a It's kind of a weak
take from me. I get it. You guys You
guys probably wanted something more, but
I don't really have something more on
this. I You know, I just love
technology. I like seeing cool things.
And honestly, being a part of multiple
rewrites, this is pretty awesome. Like
the last time I was a part of a rewrite,
people got fired. And this one is just
like I don't think anyone's getting
fired. That's awesome. The name
is hey. The You know, I'm all right.
I get excited, okay? You know what?
Honestly, I have strong beginner energy,
okay? That's all it is. A gen.
Ask follow-up questions or revisit key timestamps.
This video examines Bun's rapid decision to rewrite its codebase from Zig to Rust using AI agents. The creator discusses the motivations behind the move, specifically addressing the challenges of managing memory and stability in Zig, as well as the significant $165,000 cost associated with the AI-assisted rewrite. The video evaluates the project as a successful demonstration of utilizing AI agents for massive refactoring and testing tasks, despite initial skepticism from the community.
Videos recently processed by our community