Rewriting Bun in Rust

159 points | by afturner 2 hours ago

61 comments

  • YuechenLi 27 minutes ago

    >Combined with the Rust rewrite, ICU changes, and identical code folding, Bun's binary size shrinks by ~20% on Linux & Windows.

    People who are surprised by this probably has not seen what Zig code actually looks like. Zig's explicitness and lack of abstraction have a real cost that it is basically one of the most verbose programming languages I've ever seen, it's somehow even more verbose than Go. Basic features of modern languages like pattern matching and generics, and as you can see, having to manually clean up everything means that if you forget once, it's a memory leak. Having SOME abstraction is actually good if it prevents you from making mistakes.

    Ironically, Zig is a programming language that's probably best written by LLMs, since they can actually tolerate the verbosity.

      benced 21 minutes ago

      Not a compiler expert - shouldn't language verbosity and binary size be, at best, very loosely related?

        steveklabnik 14 minutes ago

        I don't think you can draw that conclusion. For example, in Rust:

            #[derive(Copy, Clone)]
            enum Expr {
                Int(i32),
                Add(i32, i32),
                Neg(i32),
            }
            
            fn eval(expr: Expr) -> i32 {
                match expr {
                    Expr::Int(x) => x,
                    Expr::Add(a, b) => a + b,
                    Expr::Neg(x) => -x,
                }
            }
        
        Rust's enums can carry data. You can write the same thing in C, but because it does not have the enum feature, you have to do it yourself. They're sometimes called "tagged unions" for a reason, you use a union + a tag when doing it by hand:

            #include <stdint.h>
            
            typedef enum {
                EXPR_INT,
                EXPR_ADD,
                EXPR_NEG,
            } ExprTag;
            
            typedef struct {
                ExprTag tag;
                union {
                    struct {
                        int32_t value;
                    } Int;
            
                    struct {
                        int32_t left;
                        int32_t right;
                    } Add;
            
                    struct {
                        int32_t value;
                    } Neg;
                };
            } Expr;
            
            int32_t eval(Expr expr) {
                switch (expr.tag) {
                    case EXPR_INT:
                        return expr.Int.value;
            
                    case EXPR_ADD:
                        return expr.Add.left + expr.Add.right;
            
                    case EXPR_NEG:
                        return -expr.Neg.value;
                }
            
                __builtin_unreachable();
            }
        
        I haven't actually compiled this, but it should compile to almost the exact same, if not literally the exact same, machine code. Yet one is way more verbose than the other.
          pavon 10 minutes ago

          I think you are saying the same thing as benced - just because Zig source code is verbose is no reason to assume the binary should be larger.

            steveklabnik 7 minutes ago

            I read my parent ask asking a question: is there a correlation, or not?

            I am saying that I do not believe there is a correlation between source code length and binary length.

            I think it's more "disagreeing with YuechenLi" than "agreeing with benced", unless benced was implying an opinion with the question.

      giancarlostoro 26 minutes ago

      > Ironically, Zig is a programming language that's probably best written by LLMs, since they can tolerate actually tolerate the verbosity.

      Rust in my opinion feels the same.

        afavour 6 minutes ago

        I don’t feel the verbosity with Rust. Haven’t written it in a while but now in the LLM era I’m looking forward to saying “sort out the lifetime errors for me”.

          i_am_a_peasant 4 minutes ago

          I trust a lot more Rust code generated by an LLM than anything else ngl.

        sroussey 23 minutes ago

        Agree. But just because it feels the same doesn’t mean it compiles the same.

        honeycrispy 15 minutes ago

        That can often depend on how you write it.

  • Philpax an hour ago

    Without commenting on Bun itself as a project, or the nature of the rewrite, it can't be good for Zig that a naive rewrite away from it fixed memory leaks, improved stability, shrunk binary size by 20%, and improved performance by 5%.

      pyrolistical 42 minutes ago

      Yeah but they turned it into something unreadable. Call it a skill issue if you wish.

      I just haven’t found another language that just makes sense. Zig doesn’t hide anything from you

        kgeist 16 minutes ago

        >they turned it into something unreadable

        Did you compare the code before/after? It's a mechanical line-by-line port, and most of the code is identical to the old version, just with Rust syntax. They have an example in the blog post.

        lifthrasiir 30 minutes ago

        The article explicitly mentions the maintainability as a foremost concern.

      bielok an hour ago

      I would guess that people looking to use Zig understand that those are project concerns and not language concerns.

        Zakis1 an hour ago

        The stability gains are a direct language concern as mentioned throughout the article.

      lifthrasiir 34 minutes ago

      The same concern applies to every GC language, so it's not necessarily bad for Zig. Bun can have been grown too large for Zig to be effective, while moderately sized projects may still greatly benefit from Zig.

        saghm a few seconds ago

        I thought Zig was supposed to be a C replacement (as in, it doesn't actually provide full safety in the way that Rust or a GC language would)?

      throwaway27448 38 minutes ago

      True, but rewrites often allow for this sort of benefit in themselves. It's possible rewriting it in zig would have yielded some of the same improvements.

        pdpi 32 minutes ago

        A sophisticated rewrite? Sure. This was a naive like-for-like rewrite, though.

      geon 34 minutes ago

      Wouldn't the same improvements have been made in zig if they instructed the agents to improve instead of rewrite?

        Zakis1 16 minutes ago

        But how would you verify that the agents have written memory safe code? Rust's borrowchecker is a lot faster and actually verifiably safe compared to asking an LLM to fix the safety issues that the Zig version had.

      rq1 37 minutes ago

      From a PL Theory perspective, Zig is vibe-coded.

      Not sure why people use it.

  • hansvm 15 minutes ago

    Every time I've rewritten a major project I've made it smaller and faster while fixing all the major bugs and most of the minor ones. My current team has had similar experiences. I'd be curious to see what a Zig -> Zig rewrite of the same magnitude would have done for quality.

  • bel8 an hour ago

    > Claude Code v2.1.181 (released June 17th) and later use the Rust port of Bun.

    It seems the reports of Bun's death have been greatly exaggerated.

  • duhhhhh1212 38 minutes ago

    Where is the cost breakdown? I feel like this would be the easiest number to determine and write in this post. It's hard to believe that there have been no problems/downsides since the port.

      gpm 36 minutes ago

      > Where is the cost breakdown?

      From the article

      > Pre-merge, this took 5.9 billion uncached input tokens, 690 million output tokens, and 72 billion cached input token reads — around $165,000 at API pricing

      > It's hard to believe that there have been no problems/downsides since the port.

      A significant portion of the article was dedicated to the 19 regressions they've found. Starting here: https://bun.com/blog/bun-in-rust#porting-mistakes

        BearOso 7 minutes ago

        [delayed]

        duhhhhh1212 22 minutes ago

        Thanks!! Those are solid numbers but confusing. He reported input, output, and cached input token reads but not cache writes/cached creation input tokens? Maybe cache writes aren't a thing internally?

  • theLiminator 29 minutes ago

    That's the power of a strong test suite. LLMs excel when you have verifiable rewards. I imagine we'll get a lot more rewritten in rust projects in the future. Rust is also an ideal target for such rewrites as it offers a lot of verification (via its type system) and is low overhead with zero-gc. There's less and less reason to use GC'd languages in the agentic coding era.

    I think Rust is a locally optimal target for LLM coding, we might see a better language in the future, but I think Rust will dominate for quite some time.

      lifthrasiir 27 minutes ago

      > There's less and less reason to use GC'd languages in the agentic coding era.

      Faster iteration, maybe? Rust's safety guarantee isn't exactly free (while still being very excellent) and does affect iteration time. I have a private project (>300K LoC) that has been translated from Python to TypeScript and the reason we couldn't use Rust was definitely the iteration time.

        gpm 20 minutes ago

        Eh... rust's safety isn't free, but not having it and wasting time on "oh I forgot to change this call site" also isn't free. On the whole I'd say the safety assists in iteration time.

        What costs rust in iteration time in my opinion is the low level (by default) nature of it. There's a faster-to-iterate language that has yet to be created which is rust but we sacrifice performance (and memory fiddling ergonomics for the odd person who does that) so we don't have to worry about things like whether a variable is stack or heap allocated. Which is in the direction of a GCed language but retains the mutable-xor-aliasable semantics.

        Between rust and current GCed languages though... I guess I agree with "maybe" in both directions.

          theLiminator 15 minutes ago

          Maybe something like Hylo? But personally I don't see anything displacing rust for the next few years, as I think there's enough rust in the training data for it to be the best "serious" language for agentic systems-level development.

          It's really the only systems language in its exact niche.

  • thevinter 2 hours ago

    One thing that I found interesting is that most of the discourse surrounding the topic happened with the assumption that the rewrite was happening with an Opus-like model, and not with Fable. Those assumptions, at least partially, were used as arguments against the fact that the rewrite was feasible and/or a good idea.

    Clearly the model itself doesn't completely change the narrative, but at least as a note to myself, I would like to be more careful with assuming the capabilities of the models used internally by Anthropic and affiliated orgs.

  • ianm218 21 minutes ago

    Inspired by this project I ported most of Valkey to Rust here valdr.dev .

    The coolest outcome was being able to run a redis comparible store on an a cloudflare durable object so you do I.e. rate limiting for free with little infra.

  • giancarlostoro 20 minutes ago

    So I kept hearing that the author did this purely because Anthropic wanted a PR story, but reading this entire very well written post, with meticulous detail, what say you now? I never thought it made any sense for him to do this just because Anthropic asked him to. Sometimes you find yourself fighting the stack you're currently using, and another stack (or programming language) looks like it would alleviate a lot. LLM was just another tool in his toolbelt. I had already ported projects that were old and abandoned before using Claude Code, so I knew it was possible.

      nozzlegear 17 minutes ago

      > what say you now?

      I think that when you have a $165,000 hammer, all of your problems begin to look a lot like nails.

        giancarlostoro 14 minutes ago

        I've done rewrites like this, maybe it wasn't Zig to Rust, but I have been able to rewrite sizable projects, from C# to Rust before. I incorporated a similar strategy, have Claude Opus review the codebase, write a spec, then have Claude implement it, while reviewing the spec, and using the codebase as fallback and gospel over the spec. That said, it's not the entire story here as I said, there was a lot of thought put into it, it it had not been done with Claude, I have a feeling he might have started an "experimental" version of Bun in Rust instead, as many developers have done in the past before LLMs.

        benced 7 minutes ago

        I would guess the cost to do this with humans would be _at least_ $1.5M in compensation alone (I'm thinking three 500k/year Bay Area engineers) so this is already an order of magnitude cheaper.

        Is it worth $165K? I'm less sure of that but it's honestly a moot point - this will get to 5 then 4 digits of cost pretty fast.

  • minimaxir 2 hours ago

    Adding bespoke animations via Claude Code to the blog post is definitely thematic. It's unclear if they're useful data visualizations as they take a bit of time to parse, but they're neat.

  • SergeAx 11 minutes ago

    I still think that generating a Zig-Rust transpiler would be a better approach, given all the LLM quirks, including the ability to just /goal the model with binary-identical LLVM bytecode.

    However, an open-sourced tool like that would've greatly harmed the Zig ecosystem and community.

      ivanjermakov 3 minutes ago

      > would've greatly harmed the Zig ecosystem and community

      People looking to abandon the ship first chance are unlikely to contribute much to the ecosystem and community.

  • rvz an hour ago

    As expected [0] [1], this was a clear advertisement / marketing opportunity of Anthropic's Fable model on rewriting Bun (which powers Claude Code) from Zig into Rust.

    Something that would have taken hundreds of developers now took 1 developer with Fable.

    Now Claude, rewrite Claude Code from TypeScript to Rust. Make absolutely zero mistakes.

    [0] https://news.ycombinator.com/item?id=48073893

    [1] https://news.ycombinator.com/item?id=48240829

      steveklabnik an hour ago

      EDIT: the parent has effectively deleted their original comment

      > There are a lot of ways to do a terrible job of this. For example, prompting Claude "Rewrite Bun in Rust. Don't make any mistakes." and then praying it would work is not what I did.

        rvz an hour ago

        *whoosh* goes the joke.

          steveklabnik an hour ago

          Hacker News does not have a meme-y culture, and this post takes this topic pretty seriously and is technically interesting.

          It's not so much that I missed that it was a joke, I just don't think that it really added to the discussion.

          What you've edited it to is a much better comment.

            rvz an hour ago

            It's ok to admit that you did not get the joke. That's fine.

              QuaternionsBhop 17 minutes ago

              Jokes require mutual context. You failed to create a joke because you did not ensure the prerequisites were met.

      merb 14 minutes ago

      1 Developer with a 200k budget for tools.

  • classicposter 38 minutes ago

    This slop rewrite introduced new vulnerabilities and regressions.

  • dfabulich 29 minutes ago

    This blog post further undermines my trust in Jarred.

    He makes it sound like Claude did a fantastic Rust rewrite, and "the work continues."

    But when the Rust port merged to main, the state of the code was very, very bad. There were 13,000 instances of `unsafe`, no Miri tests at all, and, sure enough, it exposed UB in safe Rust. https://github.com/oven-sh/bun/issues/30719

    Observers could see this coming from a mile away, objected strongly to using AI to RIIR before the code merged. Rather than incorporate feedback and get the code ready for production, Jarred gaslit us all, right here on HN. https://news.ycombinator.com/item?id=48019226

    Just 9 days before he merged the Rust rewrite to the main branch, Jarred wrote:

    > This whole thread is an overreaction. 302 comments about code that does not work. We haven’t committed to rewriting. There’s a very high chance all this code gets thrown out completely.

    It's plausible that Bun's Rust rewrite is now in much better shape than it was in May. But a blog post like this would have been a place to apologize, to accept that it was a very bumpy rollout, to acknowledge that public messaging was extremely poor, and to earn back our trust.

    As it stands, I guess I'll have to run my own tests to try to evaluate whether Bun 1.4 is ready for prime time, because I just can't trust Jarred to give us a straight answer.

      Georgelemental 21 minutes ago

      Pre-release code had bugs that were fixed before the release? Why is that a problem? That's the point of having a testing and release process

        pier25 9 minutes ago

        what about new bugs introduced after the rewrite?

      benced 18 minutes ago

      > We haven’t committed to rewriting. There’s a very high chance all this code gets thrown out completely.

      God forbid an engineer express uncertainty.

        Atotalnoob 17 minutes ago

        Uncertainty is one thing, but a high chance means it’s 51% or higher to me.

        Based on that, the bun rewrite messaging was fairly misleading.

          wccrawford 6 minutes ago

          That was their estimate at the time, based off the information they had. You can't ask more of someone than that.

          Either they estimated poorly, or it ended up the lesser portion of their estimate after all. After all, unless the estimate is 100%, there's always a chance it'll fall into the other portion.

          jen20 a few seconds ago

          To understand your error, consider that in the month leading up to the 2016 US presidential election, the widely-accepted probabilities were between 70% (Five-Thirty-Eight) and 90% (Reuters) in favour of Clinton.

  • tangenter 12 minutes ago

    Should we brace for another front page Zig donation announcement? A fast follow with a “Why Zig?” penance piece, replete with anecdotes about how it is the only true way to express oneself?