HomeVideos

The Great Bun Rewrite

Now Playing

The Great Bun Rewrite

Transcript

426 segments

0:00

What is Bun moving from Zig to Rust? May

0:03

4th. Look, a community note. Bun's

0:06

creator states it's a fun experiment,

0:09

not a decision to rewrite. Don't worry,

0:11

we're not rewriting Bun from Zig to

0:14

Rust, right? That I mean, that would be

0:15

silly, right? And 9 days later, the plan

0:18

for tomorrow's dog food Bun's Rust port

0:20

on Claude code internally and start

0:21

writing a blog post. I thought it was

0:23

just for fun. 2 months later, rewriting

0:25

Bun in Rust, the blog post of why and

0:28

how they did it. That escalated quickly.

0:31

Okay, that went from fun Z to production

0:33

in almost no time. Of course, this

0:36

rewrite took 11 days with the help of

0:38

Claude Fable pre-release before all of

0:40

us plebs had the chance to use it. Jared

0:42

was out there just crushing rewrites in

0:45

11 days. And of course, that rewrite

0:47

took 690 million output tokens, 5.9

0:51

billion uncashed input token reads, and

0:54

72 billion cached input token reads for

0:57

around $165,000

1:00

of API pricing. Hey, brother.

1:03

That's a steal of a deal, okay? So yes,

1:06

obviously after reading all of this, I I

1:08

just have to we we have to talk about

1:10

this, okay? So I I just want you to sit

1:12

down and I I

1:13

I'm just going to tell you something

1:14

that honestly, I don't think this is the

1:16

worst use of tokens ever. I actually

1:18

think this is a pretty good idea. I

1:19

don't Okay, hold hold on. Put put down

1:21

your pitchfork, okay? Hey, hey, we're

1:23

just sitting down. Let's just sit down.

1:24

Let's just talk about it for a little

1:26

bit, okay? And while you center yourself

1:27

and calm yourself down, I'd like to say

1:29

thank you to my sponsor. One of the more

1:30

terrifying things you can do as a

1:32

company is to contract out software, and

1:34

I know a lot of companies are

1:35

contracting out their mobile

1:37

applications. The thing that makes this

1:39

so dangerous is that it's a black box

1:41

experience. You hope you get back

1:43

software that is complete, that is going

1:45

to be well tested, that ultimately will

1:47

be extensible as future needs require.

1:49

The last thing I wanted someone else to

1:50

say to Claude, "Hey, mobile app, no

1:52

mistakes." It does make mistakes. This

1:54

is why I trust the guys over at Infinite

1:56

Red. I have had many a long talks with

1:58

the CEO about software quality and the

2:01

way to really build robust and good

2:03

systems, and I love everything he has to

2:05

say. And everything that I have seen

2:07

with my own eyes, the quality of

2:08

software that they build is very, very

2:11

good. I hope we're feeling better now.

2:12

Are we? Okay, good. What I'm going to do

2:14

is I'm going to tell you why they did

2:16

this kind of rewrite, and we're going to

2:17

go over the blog on that. And then I'm

2:19

going to tell you kind of my thoughts on

2:20

the cost, the $165,000

2:23

bill. And then lastly, we're going to go

2:25

over some what I would consider actually

2:27

pretty cool techniques that you can use

2:30

in your day-to-day. So, let's get

2:31

started. So, I think the best place to

2:33

start in this blog post is to start with

2:35

why did they do this? And at the very

2:38

tippity top, he talks about how, "Hey,

2:40

Zig was super instrumental for them

2:42

getting started. He's very happy that he

2:43

used it." But he said, "Hey, Bun scope

2:46

has been a challenge for stability.

2:48

Here's a small sample of bugs we fixed

2:50

in 1.3.14.

2:52

Use after free crash, use after free,

2:54

use after free, use after free, crash,

2:56

out of bounds, heap out of bounds,

2:58

memory leak, memory leak, memory leak,

3:01

something to do with SSL wrapper and

3:02

knit, double free crash, race

3:04

conditions, this thing was never freed."

3:06

As you can see, memory problem after

3:09

memory problem after memory problem.

3:11

Now, Jared attributes a lot of the

3:13

difficulty with handling memory and all

3:15

these problems due to the just the

3:17

unusual set of software he is writing.

3:19

Other users of Zig don't have the bugs

3:21

we had. And mixing GC with manually

3:24

managed memory is an uncommon enough

3:26

thing for software to need that no

3:28

language really designs for it. He goes

3:30

on and continues to praise Zig. "We

3:32

wouldn't have gotten this far if not for

3:34

Zig. I've always been grateful. Until

3:37

very recently, programming language

3:39

choice was a one-way decision for a

3:41

project like Bun." He goes on and and

3:43

gives kind of his justification for why

3:45

defer is a bit more tricky and why

3:48

something like Ray or drop in Rust is

3:50

just going to be a superior way for him

3:52

to be able to handle managed memory

3:55

inside of this garbage collected

3:57

universe. And the core of his argument

3:59

is right here. Many projects opt to

4:01

answer these kind of questions through a

4:03

style guide. Tiger Beetle's Tiger style

4:06

is an example in Zig and Google's 31,000

4:09

word C++ style guide is another. The

4:11

challenge with style guides is

4:12

enforcement. How do you make sure the

4:14

style guide is followed? Historically,

4:16

code review was the answer with the best

4:18

effort enforcement via linters and

4:20

static analyzers. What he's trying to

4:21

say is that there's not a language

4:23

enforcement, instead there is a

4:24

community enforcement around here like,

4:27

"Hey, we've all agreed to write code in

4:29

a very specific way. Anybody who breaks

4:31

this pact, if not caught in review,

4:34

could lead to bugs." And he says, "Hey,

4:35

this is getting really difficult for us

4:37

to maintain." Then he even specifically

4:39

calls out C++. About 20% of Bun's code

4:42

is written in C++. Bun embeds several

4:44

C++ libraries. It would have been really

4:47

easy to use C++, but then at the end of

4:49

the day, you're still doing style guide

4:51

enforcement is his general argument. And

4:53

so, that's why he looked towards Rust

4:56

because Rust, it's not a style guide

4:58

enforcement. It's not like, "Hey, did

4:59

you make sure you used a shared pointer

5:01

there or a unique pointer there?" No,

5:02

it's like, "Hey, you can't You don't

5:04

have an option. This is a language that

5:06

enforces it at the language at the

5:08

compiler level." Now, I'm positive

5:10

there's a lot of arguments that are

5:11

going to be like, "Well, actually, this

5:12

is just a skill issue on his behalf."

5:14

And just, you know, Rust Sure, Rust will

5:16

save you, but at the end of the day, you

5:18

could have just done this with Zig.

5:19

Sure, I do agree with all those general

5:21

sentiments, and I think that that is a

5:23

real thing. But I also think it's a very

5:25

hard project. I'm not the one paint

5:26

maintaining it. I'm not the one in

5:28

charge of it. So, if he wants to go robo

5:30

rewrite something via the help of an LLM

5:32

to get rid of this class of bugs, you

5:34

know what? More power to the guy. All

5:36

right, so I think it's best to talk

5:37

about the $165,000

5:40

thing now, okay? I think a lot of people

5:42

out there when they hear that, they just

5:44

hear that money up front, and they go,

5:46

"Oh, this was a terrible idea. How could

5:48

you pay $165,000?" Okay, Claude token

5:51

rich man, not everybody uh one of us has

5:53

that. But, I've been a part of a

5:55

rewrite. The rewrite I did was for an

5:58

like an internal data consistency thing

6:00

for Netflix. And when I did that

6:02

rewrite, it took a three of us

6:03

approximately 1 year to get everything

6:05

back into place pretty well. Now, that 1

6:07

year effectively cost that company over

6:10

a million dollars when you consider all

6:11

the management, all the interacting with

6:13

other teams, plus my time, plus somebody

6:15

else's time, plus another person's time.

6:17

One of those people was a staff

6:18

engineer. You can just imagine that all

6:20

those costs come together, plus, you

6:22

know, medical insurance, all that stuff.

6:23

When you think about the actual cost of

6:25

an engineer, like Netflix paid a lot of

6:28

money for us three to go on effectively

6:30

a mental adventure on rewriting a piece

6:33

of software that already worked

6:34

previously. Granted, the new thing was

6:36

better, but nonetheless, it took a long

6:38

time, a lot of money, and features

6:40

froze. So, now when you think of it in

6:42

that kind of perspective, 11 days to

6:45

rewrite $160,000?

6:48

To me, that actually doesn't seem bad.

6:49

And the reason why I don't say it seems

6:51

bad, I think it seems actually awesome.

6:53

Because let's just pretend that given

6:55

enough kind of style guides and and

6:58

assisting, you could get software that

7:00

looks maybe 50% the way you actually

7:02

wanted it to look at the end. Well, for

7:04

the next month, engineers could sit

7:06

there and help kind of reshape it and

7:08

get it into something they actually

7:10

really like, and you're still leaps and

7:13

bounds cheaper and significantly faster.

7:16

So, to me, this rewrite is not absurd. I

7:18

think a lot of people just immediately

7:20

go, "Oh, this is just so stupid." They

7:21

don't actually think about it from maybe

7:23

a more holistic perspective, which is,

7:25

"Okay, let's pretend it took three

7:26

engineers three months to get the code

7:29

base back into shape after this kind of

7:32

robo rewrite." Well, guess what? That's

7:34

still way faster, and it's likely way

7:37

cheaper than it would have been some

7:39

other direction. Like, it's literally

7:40

the cost, quality, and speed thing all

7:43

put together at the exact same time.

7:45

It's one of the rare three-point

7:46

triangle W's. So, when I see like posts

7:48

or tweets talking about how ridiculous

7:50

it was to spend 160 grand, I look at

7:52

that like, "Damn, that's a steal. That

7:56

is an absolute steal." And then

7:59

afterwards, you get I get to do

8:01

refactorbation, my favorite activity.

8:03

Bro, let me in. All right, so let's

8:05

actually go over what he did

8:07

effectively. He kind of gives this nice

8:09

little piece of pseudo code that says,

8:10

"Hey, go do the task. Then after doing

8:12

the task, I want you to go get two

8:14

reviews. And then I want you to apply

8:17

the review feedback to each one of

8:18

those." And how he does the reviews is

8:20

he actually spawns a completely separate

8:22

agent with no context, then says, "Hey,

8:24

here's the change set. Here's what I

8:26

wanted it to do. Tell me, did it do the

8:29

thing? Can you find any problems? Can

8:31

you write about any of the bugs you see?

8:32

Can you write about like, you know,

8:34

gives it some basic things." I don't

8:35

know why he does two reviews. I wonder

8:37

if there's like maybe two different

8:38

things they had the reviewers do. I

8:40

don't know, but you could imagine that

8:42

each of the reviews brought a different

8:44

kind of goal or orientation to it, and

8:46

this allowed good feedback. And

8:48

honestly, I've been doing effectively

8:50

this approach for the last eight months,

8:52

and this has been one of the greatest

8:54

uses of AI I have had so far. In fact, I

8:58

have this guy right here where when I

9:00

go, "Hey, prime agent review," it's

9:01

going to say, "Hey, is this from like a

9:03

cursor cloud agent run, or do you just

9:05

want me to go straight to a GitHub PR?"

9:07

And when I do that, it's going to go

9:08

say, "Okay, go give me the URL," which I

9:10

can go give it a URL. Then it's going to

9:12

ask me, "What context files do you want

9:13

to include into it? Hey, what's the

9:15

prompt? What's like the way you want me

9:16

to review it?" And then it'll go off and

9:18

churn out a review, and actually give

9:20

good feedback leaving on. And often what

9:22

I find is that about 50% of the feedback

9:25

is actually good. Like, "Hey, that's an

9:28

actually good thing to go catch. Don't

9:30

forget to go do that." And I so I'm a

9:32

huge fan of this. So, this thing right

9:34

here is honestly super cool. To spare

9:37

you the long reading, I'll just kind of

9:38

give you the fast track. Effectively,

9:40

what the LLM did it would like whole

9:43

translate a one Zig file into one Rust

9:46

file and then do those two reviews. And

9:48

then after going through all of the Zig

9:51

code and just literally mechanically

9:53

translating it, at the end of the day,

9:55

there was 16,000 errors. And so, as you

9:59

can watch this thing go, this is the

10:00

replaying of those errors being fixed.

10:03

Effectively, what happened is that they

10:04

had a whole bunch of agents running in

10:07

different work trees just taking one

10:09

error, fixing the error, then having the

10:11

two reviewers review, then fixing the

10:13

feedback, and then just keep on going

10:15

through this process all the way until

10:17

all of the items were fixed, all the

10:19

errors were fixed, and now all of a

10:20

sudden you have a working version of

10:22

Bun. Of course, the moment that he ran

10:24

bun dash version, it had immediately

10:27

linked linker error. So, it did it

10:28

didn't really work, it just immediately

10:30

panicked. So, the next step was to

10:31

actually start going through and making

10:33

every single unit test pass. And so, he

10:36

did the exact same thing again, set up

10:38

these huge amount of Claude code workers

10:40

to all run, and they'd all slice up the

10:43

amount of tests and just fix them. And

10:46

it looked something like this if

10:47

visualized where all these different

10:49

shards are off running and making sure

10:51

everything is working. And eventually,

10:53

he got to the point where after so many

10:54

days, effectively, all the tests pass.

10:58

And it was at this point where he

10:59

decided, "Okay, yeah, this rewrite might

11:02

actually work. This actually might be a

11:03

thing." So, after 11 days, there were

11:05

6,502

11:07

commits yielding approximately 1.8

11:10

million lines written/rewritten,

11:13

$165,000.

11:15

And honestly, it's kind of it's kind of

11:17

a neat thing. I I mean, I know that I

11:18

rag a lot on AI, but I do think that

11:20

this is one of its strong suits, which

11:22

is translating one thing to the next

11:24

thing. Like that, I think that this

11:26

probably worked out reasonably well. At

11:28

least the Rust code will be,

11:30

you know, somewhat in the same shape

11:32

that the Zig code was in. And so, their

11:34

ability to move through the code base is

11:36

probably not, honestly, all that bad.

11:38

Now, is this going to be great idiomatic

11:40

Rust? You know, I know there was a lot

11:42

of porting, there's a lot of style

11:44

guides. Hey, there's a lot of work on

11:45

the how they're going to be doing

11:47

lifetimes and everything up front. So,

11:49

it's probably not all that far off. But,

11:51

my guess is that they had to put in a

11:53

lot of effort afterwards to really start

11:56

kind of cleaning up the code base and

11:58

making everything work. But, you know,

11:59

they also have a lot of engineers. They

12:01

have a lot They got a lot of Fable

12:03

tokens, okay? They got that They got

12:05

that on guard rated Fable, okay? They

12:07

got that operator Fable. Not us poors,

12:10

okay? We don't even get We don't even

12:11

get that kind of Fable at all, okay? We

12:13

get Fable at home. I guess the real

12:15

takeaway is going to be this. At some

12:16

point, Bun 1.4 is going to be released

12:18

to the public. And when they release it,

12:21

what's going to happen? I don't know.

12:23

Will it be awesome? Will everything be

12:25

all thumbs up? Or is it going to be

12:26

absolutely horrifying? We're going to

12:28

find out because there is a non-zero

12:30

chance that Bun 1.4 gets released. And

12:33

guess what? Hundreds of thousands of

12:35

people just have their stuff broken on

12:37

their computer cuz they update Bun,

12:39

breaks CI that gets the latest Bun,

12:41

breaks.

12:42

I don't know. It's going to be really

12:43

interesting. Or, nothing happens. And

12:46

then everybody goes, "Huh.

12:48

I guess that's pretty cool." By the way,

12:50

if I'm sweating, if I'm looking shiny, I

12:51

think I am looking shiny in here. Oh my

12:54

gosh, I It's 100° out in my horse barn

12:57

is not, in fact, keeping me cool. Now,

13:00

you're probably wondering, why am I not

13:01

having just like a "Ah, this is such a

13:03

sucky thing" kind of response to all

13:05

this. I just kind of want to throw this

13:06

out there. One of my favorite things in

13:08

the universe to do is to build tools.

13:11

And when I looked at this, especially

13:12

when I looked at how he kind of

13:14

organized changes, the feedback, and the

13:16

fixes, like when I look at this, I'm

13:18

like, "Oh man, I love this. This is dev

13:20

tools. I love dev tools stuff." And this

13:22

is kind of why I built actually this

13:24

Prime Agent review. I've been doing this

13:26

more manually or using other tools and I

13:28

was like, "Dude, I should just start

13:29

really integrating a lot of this stuff

13:31

into my environment and start building

13:33

it out such that I actually have a

13:34

pretty excellent integration with cloud

13:36

agents, my local Neovim, and like how I

13:39

like to develop." And so for me, I just

13:42

love good tools. I love seeing good

13:43

tools. I love the fact that he was

13:45

actually able to get this all done. Now,

13:47

whether or not it's going to be a

13:48

success, I guess will be just gauged by

13:50

the reception of 1.4. But for me, I

13:53

honestly thought it was pretty cool. I

13:55

know. It's kind of a It's kind of a weak

13:57

take from me. I get it. You guys You

13:59

guys probably wanted something more, but

14:01

I don't really have something more on

14:02

this. I You know, I just love

14:04

technology. I like seeing cool things.

14:06

And honestly, being a part of multiple

14:08

rewrites, this is pretty awesome. Like

14:10

the last time I was a part of a rewrite,

14:11

people got fired. And this one is just

14:14

like I don't think anyone's getting

14:15

fired. That's awesome. The name

14:17

is hey. The You know, I'm all right.

14:20

I get excited, okay? You know what?

14:22

Honestly, I have strong beginner energy,

14:24

okay? That's all it is. A gen.

Interactive Summary

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.

Suggested questions

3 ready-made prompts