Bun's Zig => Rust Rewrite Is Impressive!

Show notes

Full Bun course: https://academind.com/courses/bun-the-practical-guide

Official blog post: https://bun.com/blog/bun-in-rust Zig's creator's response: https://andrewkelley.me/post/my-thoughts-bun-rust-rewrite.html

Website: https://maximilian-schwarzmueller.com/

Socials: 👉 Twitch: https://www.twitch.tv/maxedapps 👉 X: https://x.com/maxedapps 👉 Udemy: https://www.udemy.com/user/maximilian-schwarzmuller/ 👉 LinkedIn: https://www.linkedin.com/in/maximilian-schwarzmueller/

Want to become a web developer or expand your web development knowledge? I have multiple bestselling online courses on React, Angular, NodeJS, Docker & much more! 👉 https://academind.com/courses

Show transcript

00:00:00: Bun was ported from Sig to Rust.

00:00:14: This is really interesting, also regarding how AI was leveraged and what that could mean for similar projects in our industry.

00:00:22: Now just as a little side note I personally like BUN quite a bit.

00:00:26: it's my default javascript runtime.

00:00:28: i use on most of the projects A brand new course on Bunn last week.

00:00:36: And it works, of course for the Rust version as does for the SIG version.

00:00:39: So if you want to dive a bit deeper into bun and learn about all the core APIs It has to offer because that's one of the biggest advantages Of bun that is so much stuff built in.

00:00:48: some people don't like it.

00:00:49: I love it.

00:00:50: If You Want To Learn More About That That Course May Be Interesting.

00:00:53: But Let'S Now Take A Closer Look At The Overall Porting Timeline Shall We?

00:00:58: It Actually Happened Two Months ago So the blog post was released last week in July, but the port itself happened in May.

00:01:08: And it started in early May when the Claude-Phaser port branch was discovered on the official BUN GitHub repository.

00:01:15: That branch contained a porting MD file, or markdown file with instructions of how to convert SIG code into Rust code and how to port that code.

00:01:26: Translation tables...and general instructions!

00:01:29: That was discovered, so naturally discussions ensued on Hacker News on X and there were people excited about that.

00:01:43: There were very critical of it but at the point we didn't know a lot.

00:01:47: obviously there is a lot of polarization around Rust.

00:01:54: Like myself, there are people that hate Rust.

00:01:57: There are people who hate everything should be rewritten in Rust because very often you read the People Want program XYZ to be written in Rust and I get this is annoying.

00:02:08: but naturally therefore there was a lot of polarization going on But The port had not yet happened.

00:02:15: That changed later In May And On may fourteenth an official poll request Was opened then also merged that actually migrated the buncode base, all of it from SIG to Rust.

00:02:30: Now as you can see It's a huge pull request.

00:02:32: more than one million lines of code were added and The removal of the SIG code happened in at different stage.

00:02:40: So yeah That why its not a swap But Its A Huge Change As You Can See With Almost Seven Thousand Commits.

00:02:46: Now Naturally... ...That Code Was Not Human Reviewed At Least Not Entirely by AI.

00:02:54: it had passing tests, but not human reviewed.

00:03:02: But that was published or merged.

00:03:06: And of course more discussion ensued.

00:03:08: now, as I just mentioned it was not reviewed by humans.

00:03:10: that's impossible to do in that timeframe but the community dove-in and had a look at analyzed parts…and then there were another blog post about the use.

00:03:32: Because one big criticism that came up after this poll request is, That the rust code in there was not idiomatic Rust.

00:03:41: It wasn't kind of a rust code you would or should write if were building it from scratch.

00:03:46: Instead really like a translation From SIG to RUST.

00:03:50: And that meant Not all those Rust best practices and patterns Were used and especially There was decent amount unsafe usage in there.

00:04:00: Now to understand Unsafe you have to understand how Rust manages memory because that is one of its biggest advantages and it's quite different from other languages, Because In most languages You Have either a garbage collector Which Is A Process That Basically Detects When A Value Is No longer Used Anywhere In The Program And Then Freezes Up the Memory which Is Convenient But Consumes Extra Resources or you have to do it yourself.

00:04:26: In languages like C for example, You have to manually allocate and free up memory And its the same in SIG!

00:04:53: This scope ends essentially.

00:04:55: And that's nice because there are different situations where a value may no longer be needed, but still when you have to clean up memory manually.

00:05:04: There are many situations where you can shoot yourself in the foot.

00:05:07: You have more fine-grained control and it can be very useful Very efficient.

00:05:11: But It is also easy To forget situations In more complex programs Where you then May not Be cleaning Up Memory Hence introducing A memory leak or cleaning up twice, which will also cause an error.

00:05:25: And therefore there is a trade-off and Rust has And it's tied to scope.

00:05:38: So if you have a scope, and you can create scope by using curly braces or function would also have its own scope.

00:05:43: You may know that concept from JavaScript.

00:05:46: If you have scope then when a value is created in there It's owned by that scope but after the scope ends And that of course is very convenient because you don't have to take care about freeing it up.

00:05:58: You also do not have a garbage collector, instead you have the clear rule!

00:06:02: That can lead to complexity in more complex programs where you need to pass values around…you can do this in Rust but it requires different ways of thinking...but ofcourse gives you memory safety unless you use the unsafe keyword.

00:06:19: If you use that and create an unsafe scope And in there, those rules no longer apply and it's then your job to make sure that memory is managed appropriately.

00:06:28: Now why would you do that?

00:06:29: Well for example if you're pulling in some C library which you can do in Rust... You can blend in some c code so-to say or call some C Library of methods & functions.

00:06:40: Then since C is naturally unsafe the code where you reach out to that C Code Is also unsafe.

00:06:48: So you need that feature To interact with our UnsafeCode.

00:06:52: And that is also one thing they noted in their official statement, That all those unsafe usages In the codebase to a decent part were actually related To calls to other libraries C libraries and so on which won't change.

00:07:08: but They also identified areas where?

00:07:18: They did and still are doing that.

00:07:20: So you can think of this initial port as like a starting point, which then got refined over time.

00:07:26: Still worth mentioning That this initial pull request or that huge poll requests already had passing tests.

00:07:32: so it was stable.

00:07:34: the Tests were passing but The code wasn't out-of-the-code quality.

00:07:37: You would maybe expect if it were written in rust from the grown up because that wasn't the goal.

00:07:43: So that was May twenty first.

00:07:45: Then there was silence and it's also worth noting, this version of BUN wasn't released yet but then on July the official blog post was released where we find lots of interesting details about this port because there is a lot to learn here.

00:08:11: This entire port, and that's no secret was done with help of AI!

00:08:15: It's worth keeping in mind that BUNN is owned by Anthropics so they had free access all those tokens AND especially also to Fable V before it was released for the public.

00:08:29: And if you're installing Claude code now btw even though BUNn one point four the Rust version isn't yet.

00:08:36: Claude code is already running on top of an unreleased banner version, so to say that it's the Rust version.

00:08:44: So there's that.

00:08:45: but yeah this port was done with Claude Code based fable five with free tokens, of course essentially since bun is part of anthropic.

00:08:55: And that's important to keep in mind because In that blog post we learn That if you were To Combine all the Tokens or If You were to sum up All The Tokens That Were Spent and Charge API Prices This Entire Port Would Have Cost Around One Hundred and Sixty Thousand Dollars.

00:09:13: That'S a Mind Blowing Number.

00:09:16: but Actually If you think about the scope of this project, and the scope is that bun was five hundred thirty-five thousand lines off sick code.

00:09:26: if You Think About That Scope And How Long It Would Take Humans To Port That Trust.

00:09:32: Then The One Hundred Sixty Thousand Dollars Actually Might Not Sound Too Bad Depending On Where Your Located Now.

00:09:39: Nonetheless It's clear that no open source project would be able to do this and most companies probably wouldn't spend the amount of money on a port.

00:09:50: This is possible because BAN is part of Anthropic, but also a nice marketing stunt for Anthropic which may not have been the main intent.

00:10:03: I don't know that, but of course it's nice marketing.

00:10:06: That is all important to keep in mind.

00:10:08: nonetheless In this blog post we can learn how Jared Did dat port or he?

00:10:16: Yeah made Datport work and that all started with that porting MD file which He created a discussion with Claude in the three-hour discussion as you mentioned in the blog post where he essentially decided together Claude code and the anthropic models, how such a porting MD file would have to look like?

00:10:37: To be able to translate SIG to Rust code.

00:10:40: Then once he iterated on that and was happy with it, He tested it on three files initially And when she was happy With that he unleashed Claude On The entire bun code base.

00:10:53: Now in this blog post he makes it clear that he did not just prompt Claude to rewite, bun and rust.

00:11:00: But instead set up an elaborate system where main agent, spinning up sub-agents too of course doing the port according to the porting MD file and then he had two adversarial review agents that reviewed The work off.

00:11:17: That main Asian once it was done And provided feedback and then a fixer agent for applying that feedback.

00:11:24: and yet they're all running in a loop and of course distributed across multiple Work trees to process the entire code base and work its way through it.

00:11:35: In the blog post he mentioned that he rewrote bun and rust using fifty dynamic workflows in clod code, which are these workflows that spin up a lot of sub-agents over the course.

00:11:46: So that all happened with help of loops in Claude code, which helped a lot of sub agents and the clear process off one main agent.

00:12:08: The review agents at the fixer agent.

00:12:11: And then he also did another separate pass, so to say making all those tests work.

00:12:20: And that brought its own challenges because the test suite is so large and complex that he ran into various infrastructure restrictions because some tests consume a lot of memory.

00:12:31: running many tests in parallel therefore doesn't work but ultimately made it all work also with help from AI rerunning the tests and so on.

00:12:43: So lots of looping, lots of agents and sub-agents involved naturally in lots of tokens burned, one hundred and sixty five thousand dollars in tokens burnt!

00:12:53: Now again you can dive in deeper... And that is what I would recommend doing if you're interested in all the nitty gritty details.

00:12:59: it's a really great blog post documenting the journey to getting there.

00:13:04: but that isn't a nutshell.

00:13:05: how The Port happened over eleven days with all those agents distributed across multiple workflows, fifty such work flows as we learned.

00:13:15: Across or over eleven days one hundred sixty five thousand dollars spent in tokens at API prices.

00:13:24: Now finally once he was done In the blog post He mentioned that bun one point four fixes various bugs.

00:13:33: That last sick version has that it's more memory efficient than its smaller And as we learn by the response of Andrew Kelly, The Creator Of SICK some of these improvements could have probably all achieved with SICK.

00:13:48: But that blog post is pretty interesting because it got edited!

00:13:53: It's less full-of anger now than it was initially... ...the first version was FULL OF PERSONAL ATTACKS just to then say THAT IT WAS NOT MEANT AS A PERSONALE ATTack but it Was Full Of Personal Attacks.

00:14:06: The latest version, which I'll also link below is still pretty spicy.

00:14:11: In the end you can clearly tell from reading this first but also this one that Andrew, the creator of SIG and Jared, they won't become best friends anymore or again now he thanks bun for supporting SIG financially just basically.

00:14:34: yeah rage about this entire port and how it wouldn't have been necessary if the BUN code would've written in proper SIG.

00:14:44: He makes it very clear that he doesn't feel like the BUND repository, the SIG version had a high-code quality... And THAT led to many problems!

00:14:54: That may or may not be true.

00:14:56: I think its absolutely possible that IN A PROJECT OF THE SCALE OF BUN moving at speed in which bun use moves, code quality may not have been up to the standards of The Creator Of SIG.

00:15:12: You could definitely argue that most code projects don't necessarily have the highest code quality though so you can make off that whatever you want.

00:15:21: now I'll leave it at that.

00:15:25: i feel like the overall response blog post is pretty weak because Steaming with anger it has some valid points like for example in the blog post Jared makes.

00:15:40: he mentions that The port from SIG to Rust was validated of course with all these reviewer agents but also by running the test suite and making that work.

00:15:51: And Andrew correctly states, Definitely, these two won't become best friends and I have no opinion on whether SIG or Rust is the better language in general.

00:16:15: Or for BUN.

00:16:34: This entire port is really impressive when it comes to the use of AI.

00:16:41: And sure, It's a way of using AI that most of us won't be able to afford or want me willing to afford in companies.

00:16:48: but its impressive That I was able do that and Its not vibe coding Or just YOLO prompting.

00:16:56: There IS A clear process behind all of this.

00:16:59: A lot Of thought went into this Which i hopefully made clear which Definitely also gets clearer if you dive into the nitty gritty technical details, but with all planning and iteration of this setup.

00:17:13: Clearly not just one prompt thrown at it... This shows what you can do with AI and of course porting a code base from one language to another is pretty good use case for AI.

00:17:27: If you think about it, AI can struggle with writing new code that may not write the code.

00:17:40: Definitely, AI is amazing for building new software too.

00:17:43: But you face a different set of problems there with a port the huge advantages that You have a codebase For dai to just look at and translate which Is something ai can do?

00:17:55: And you Have a test suite There so there's A lot To build up on it.

00:17:59: It's a good use case for Ai as it seems an ass.

00:18:02: this Port clearly Proves and I think That is The most interesting takeaway here Also that you can tackle projects.

00:18:11: That just would have been impossible to tackle before.

00:18:14: again, not for everybody but For certain companies of certain sizes this may be interesting.

00:18:19: modernizing legacy software with help of AI Can or maybe a great use case and This shows and proves can be done.

00:18:30: Of course, BUN is not out yet!

00:18:32: We'll see.

00:18:33: if everything crashes and they have to revert in a month from now... You can't totally rule it but I personally don't think that will happen.

00:18:40: It's being used by some early adopters already like the Claude Code CLI.

00:18:54: But I'm pretty confident that this will work out and i find it a pretty impressive feat.

00:19:00: And also, A Pretty Impressive Usage of AI.

00:19:03: but as always...I am also interested in hearing what your thoughts are about all.

New comment

Your name or nickname, will be shown publicly
At least 10 characters long
By submitting your comment you agree that the content of the field "Name or nickname" will be stored and shown publicly next to your comment. Using your real name is optional.