13 votes

Bun has been rewritten in Rust

22 comments

  1. [9]
    omid
    Link
    Wow that’s a huge PR! GitHub comments (just GitHub in general I guess) are really struggling to load right now, but judging from the tags here it seems the rewrite is heavily AI-assisted? Perhaps...

    Wow that’s a huge PR! GitHub comments (just GitHub in general I guess) are really struggling to load right now, but judging from the tags here it seems the rewrite is heavily AI-assisted? Perhaps entirely done by AI agents?

    4 votes
    1. [8]
      largepanda
      Link Parent
      Not assisted, done entirely by Anthropic's Claude.

      Not assisted, done entirely by Anthropic's Claude.

      7 votes
      1. [6]
        Eji1700
        Link Parent
        This is not wrong, but I do want to be clear that a huge reason this was so successful is because of a very well written and detailed test suite. AI works wonderfully when it can determine its...

        This is not wrong, but I do want to be clear that a huge reason this was so successful is because of a very well written and detailed test suite. AI works wonderfully when it can determine its wrong, and it can't flub the test.

        14 votes
        1. [5]
          ali
          Link Parent
          Notably: Don’t let Claude write the tests itself. It likes to pretend to write tests but then just build trivial tests

          Notably:

          Don’t let Claude write the tests itself. It likes to pretend to write tests but then just build trivial tests

          7 votes
          1. [2]
            artvandelay
            Link Parent
            It's fun going through my company's codebase and seeing tests that amount to: int var1 = some_default_value; int var2 = some_default_value; assertEquals(var1, var2); Glad we use up valuable CI...

            It's fun going through my company's codebase and seeing tests that amount to:

            int var1 = some_default_value;
            int var2 = some_default_value;
            
            assertEquals(var1, var2);
            

            Glad we use up valuable CI resources for these tests.

            10 votes
            1. teaearlgraycold
              Link Parent
              Some people turn on the AI and turn off their brain.

              Some people turn on the AI and turn off their brain.

              1 vote
          2. skybrian
            Link Parent
            The test I'm seeing it write for my projects aren't all that rigorous but I haven't seen it write complete crap either. I think it's okay but you have to keep an eye on it. Back when I was working...

            The test I'm seeing it write for my projects aren't all that rigorous but I haven't seen it write complete crap either. I think it's okay but you have to keep an eye on it.

            Back when I was working I would see tests that don't actually do anything written by people, too. Usually due to heavy use of mocks.

            4 votes
          3. Eji1700
            (edited )
            Link Parent
            Ah yeah I wasn't clear on that but yes, an extensive HUMAN WRITTEN test suite was why this was "easy". AI works well in the cases where the known output is already confirmed and testable. If you...

            Ah yeah I wasn't clear on that but yes, an extensive HUMAN WRITTEN test suite was why this was "easy". AI works well in the cases where the known output is already confirmed and testable. If you said "build an app like bun in rust and make tests" you'll get a disaster.

            2 votes
      2. Weldawadyathink
        Link Parent
        Not just Claude, but most likely Claude Mythos. Bun was bought by anthropic because it's used for Claude code, so they almost certainly have free access to Mythos.

        Not just Claude, but most likely Claude Mythos. Bun was bought by anthropic because it's used for Claude code, so they almost certainly have free access to Mythos.

        1 vote
  2. [5]
    devalexwhite
    Link
    Man what a waste of electricity. Now there's a PR filled with so much crap nobody can review it, which means bugs will absolutely be introduced. For what?

    Man what a waste of electricity. Now there's a PR filled with so much crap nobody can review it, which means bugs will absolutely be introduced. For what?

    4 votes
    1. [4]
      Macil
      Link Parent
      They're doing the rewrite because they frequently have bugs caused by memory safety issues, which Zig, like C and C++, does not have good protections against. It's true that this rewrite has a lot...

      They're doing the rewrite because they frequently have bugs caused by memory safety issues, which Zig, like C and C++, does not have good protections against. It's true that this rewrite has a lot more usages of unsafe blocks than most Rust projects, but it's a huge improvement from all the code effectively being inside an unsafe block as before, they're manually cutting down on many unnecessary unsafe blocks, and any new code that's written as idiomatic Rust from the start should be free of new memory safety issues.

      9 votes
      1. [3]
        devalexwhite
        Link Parent
        They're swapping memory bugs for slop bugs.

        They're swapping memory bugs for slop bugs.

        6 votes
        1. [2]
          Wes
          Link Parent
          It's a file-for-file conversion. The new codebase will be very similar in structure and control flow to the old codebase. It's likely not very idiomatic Rust, but it's not as if they vibe coded a...

          It's a file-for-file conversion. The new codebase will be very similar in structure and control flow to the old codebase. It's likely not very idiomatic Rust, but it's not as if they vibe coded a million lines without any review. It should be familiar for any existing contributors.

          They also have a very clear picture now of where the unsafe keyword is used, and can target those areas for improvement. Rust offers a lot of assurances that Zig did not. There's likely some edge cases still to shake out, but I expect it's no buggier than the code that came before.

          1 vote
          1. post_below
            Link Parent
            You make some good points. Definitely using Rust was a good call. But I have to add, it will definitely be buggier, less idiomatic, less elegant and more bloated than before. That's just where LLM...

            You make some good points. Definitely using Rust was a good call. But I have to add, it will definitely be buggier, less idiomatic, less elegant and more bloated than before. That's just where LLM coding agent technology is right now. It's not as if being under the Anthropic umbrella gives them magical powers to make agents perform better. Look at how buggy, verbose and convoluted Claude Code is. It would be different if it was human led with AI assistance rather than just letting the agents churn... but it doesn't sound like that's how they did it given the short timeframe.

            Caveat: if they used Mythos and it's lightyears ahead of Opus, maybe it's better than I'm imagining. I'd say that's somewhat unlikely.

            Whatever the case, Claude Code and Codex, despite their issues, have proven that you can ship messy, vibe coded software and people will use it. Vibecoded doesn't mean the new Bun won't be successful.

            1 vote
  3. [4]
    arqalite
    Link
    It doesn't load on my end - but like, what's the point of this rewrite? Is Bun so slow that Rust was needed here? Did it provide a measurable performance improvement? I had the impression that Bun...

    It doesn't load on my end - but like, what's the point of this rewrite?

    Is Bun so slow that Rust was needed here? Did it provide a measurable performance improvement? I had the impression that Bun is the faster of the JS tools.

    I am genuinely in love with Rust, so the more big projects in it the better, but I'm also a practical person, and rewrites are not always the best idea, unless the architecture was flawed or the stack was just so awful.

    2 votes
    1. [3]
      Eji1700
      Link Parent
      Memory safety. Rust has stronger rules for this, and the lead dev straight up cited tons of time dealing with segfaults as the reason for the jump. There are other arguments that it's because...

      It doesn't load on my end - but like, what's the point of this rewrite?

      Memory safety. Rust has stronger rules for this, and the lead dev straight up cited tons of time dealing with segfaults as the reason for the jump.

      There are other arguments that it's because since anthropic acquired bun and Zig is heavily anti AI anything in their code base that this was another reason because they couldn't contribute to Zig development, but i haven't looked into that side so grain of salt. I have no idea how much of that is true or not.

      Edit:

      Last comment I read from them on HN:

      Still writing the blog post about this. Will share more details.

      For where this is coming from, skim the bugfixes in the Bun v1.3.14 and earlier release notes. Rust won’t catch all of these - leaks from holding references too long and anything that re-enters across the JS boundary are still on us. But a large % of that list is use-after-free, double-free, and forgot-to-free-on-error-path, which become compile errors or automatic cleanup.

      7 votes
      1. [2]
        arqalite
        Link Parent
        Ah, okay, in retrospect that makes sense for a JS runtime. If Zig & C++ cause that much trouble for them then it's a worthwhile effort. That said I do believe they could have done an incremental...

        Ah, okay, in retrospect that makes sense for a JS runtime. If Zig & C++ cause that much trouble for them then it's a worthwhile effort.

        That said I do believe they could have done an incremental rewrite instead of just converting it all at once, but if the test suite is as robust as they claim (haven't checked it myself) then maybe it is fine, alongside a proper beta test phase.

        1 vote
        1. Eji1700
          Link Parent
          The original take was “fuck it why not” with the expectation they’d probably throw it out. It apparently performed better than they expected hence the full rewrite

          The original take was “fuck it why not” with the expectation they’d probably throw it out.

          It apparently performed better than they expected hence the full rewrite

          4 votes
  4. [4]
    shu
    Link
    Effectively Bun is now 'open source' in name only since no sane human being will review or work on a million lines of AI generated code. The whole project can now only be worked on with LLMs. In...

    Effectively Bun is now 'open source' in name only since no sane human being will review or work on a million lines of AI generated code. The whole project can now only be worked on with LLMs.

    In some sense it's the open source version of "Flood the zone with shit".

    1 vote
    1. [3]
      teaearlgraycold
      (edited )
      Link Parent
      I think it's hard to say at this point how this will pan out. Here's the current output of cloc on the repo: ---------------------------------------------------------------------------------------...

      I think it's hard to say at this point how this will pan out. Here's the current output of cloc on the repo:

      ---------------------------------------------------------------------------------------
      Language                             files          blank        comment           code
      ---------------------------------------------------------------------------------------
      Rust                                  1442          83527         180903         719553
      Zig                                   1297          79606          60408         571091
      TypeScript                            2618          67710         116976         475079
      JavaScript                            4344          36950          37701         290958
      C++                                    583          27169          19372         215919
      C                                      111          21580          83948         199595
      Text                                    95           4276              0         135769
      JSON                                   589             63              0         118671
      C/C++ Header                           772          14197          29055          57693
      ...
      

      The AI-written rust has way more comments (not surprising, AI loves to write comments). I could see the increase in LoC from the zig and rust versions as due to inherent verboseness of rust code. You're tracking more things using language features, so you need a few more LoC to do the same work.

      The most worrying number is the increased file count. Rust has 11% more files than zig. I suppose that means (very hand-wavey) it's 11% more complicated in architecture see below.

      Edit: So says my LLM, the extra LoC mostly come from rust RAII and trait boilerplate. The code seems to be a pretty clean 1:1 port so I expect people familiar with the original code architecture could easily get up to speed with the new code.

      Edit 2: The extra rust files are mainly lib.rs, mod.rs and build.rs files. The rust code also puts FFI boilerplate into separate files while zig does not.

      4 votes
      1. [2]
        shu
        Link Parent
        Thanks for the info! I just think if I had worked diligently on efficient Zig code for this project as a contributor before, I'd feel completely deflated after this move. Jarred Sumner allegedly...

        Thanks for the info!

        I just think if I had worked diligently on efficient Zig code for this project as a contributor before, I'd feel completely deflated after this move. Jarred Sumner allegedly llm'd all this in a few days, and if I had been a (non-AI) contributor to this project, I'd be absolutely demotivated to work on this now.

        Also this can't be good Rust code, right? A 1:1 port from Zig has to have thousands of unsafe sections, circumventing the borrow checker and Rusts philosophy. I can't imagine that Rust devs would want to work on a project like this.

        But yeah, it's going to be interesting to see how it pans out.

        2 votes
        1. teaearlgraycold
          Link Parent
          Supposedly the port has already fixed a few memory safety bugs. Look for yourself if you want to see how the code looks. If you were a contributor I think you’d find your old code in there just...

          Supposedly the port has already fixed a few memory safety bugs. Look for yourself if you want to see how the code looks. If you were a contributor I think you’d find your old code in there just with a different coat of paint.

          3 votes