Top
Best
New

Posted by gr4vityWall 4 days ago

Bun has an open PR adding shared-memory threads to JavaScriptCore(github.com)
145 points | 310 commentspage 3
applfanboysbgon 4 days ago|
Imagine somebody doing a drive-by on your repo and dropping a 270k loc PR expecting you to merge it. Bonus points if they can't even put in the 0.001% smidgen of effort to write why they think the PR is useful or necessary in their own words. Oh, but we don't have to imagine it, because there are people who actually do that!
Retr0id 4 days ago|
The PR is against bun's fork of WebKit, not upstream.
fg137 4 days ago|||
The title is of this post is definitely confusing if not misleading.
talldeck 3 days ago||
Besides, the title should probably have referred to it as "a giant open PR" or something, because I don't think what the submission is trying to focus on is the feature itself.
applfanboysbgon 4 days ago|||
Oh, my mistake, I thought they were doing the zig thing again.
bakkoting 4 days ago||
They didn't open a PR against zig either, the compiler concurrency work they did was also only ever in their own fork.
curtisblaine 4 days ago||
To all the people saying "it's dangerous to add concurrency to javascript", javascript has already workers, shared array buffers and atomics. It's entirely possible today to start two or more workers, pass a shared array buffer via a message and then write concurrently on the same buffer forfeiting message passing and synchronizing only using atomics. You can even do lock less data structures, see for example https://greenvitriol.com/posts/lockless-allocator. That's what you do when you write high performance Web apps. This proposal only adds lightweight threads sharing memory by default, but it's by no means the first and only way to do low level concurrency with javascript.
kikimora 4 days ago|
This is from PR: v1 collects synchronous and stop-the-world

Go build performant web app that has stop-the-world all the time.

curtisblaine 4 days ago||
You bypass GC by allocating pools of array buffers at the cost of low-level memory management.
kikimora 3 days ago||
So you add threads to allow a shared objects graph with a side effect of more frequent stop the world. Then you fight stop the world by resorting to low level memory management. But if you go level why not allocate a shared array buffer and share it across workers? Something you can do today without threads.
gavinray 4 days ago||
One of the biggest things preventing software like SQL DB's from being written in TypeScript is the lack of proper threading.

I genuinely think you could write a competitively-performant multi-threaded DB in Bun + TS if you had shared-heap threads and fast atomics/locking primitives.

jerf 4 days ago||
"I genuinely think you could write a competitively-performant multi-threaded DB in Bun + TS if you had shared-heap threads and fast atomics/locking primitives."

Not likely. Databases that attain any significant use in the field end up getting optimized to the n'th degree because they're the bottleneck of the entire system of every system they get put into. Javascript runs on the "5-10x slower than C" language tier. Personally I think even picking Go, in the "2x slower than C" tier, is a huge mistake, though a few people seem to be doing OK with it. I don't think you can call it "competitive" when your C++ or Rust competition is consuming a factor of magnitude less resources.

WASM DBs, maybe, especially as it continues to mature. Not Javascript.

hedgehog 4 days ago|||
Something compiled to WASM still gives a fair amount of control over memory layout, something that AFAIK is not possible in JS without building effectively a new embedded language on top of an array (Emscripten being an existence proof that you can do more or less anything that way).

One place where an interpreter + JIT language could be interesting is if it were sufficiently safe to allow user code into the query execution engine, such that the JIT could optimize it all together.

nasretdinov 3 days ago|||
Yeah I kinda agree about Go. CockroachDB didn't catch on nearly as much as it should, and I wonder how much it has to do with the fact that optimising code in Go (both in terms of memory usage and performance) is just much harder than C
n_e 4 days ago|||
You have web workers, and for shared memory and synchronisation respectively SharedArrayBuffer and the Atomics namespace.
quotemstr 4 days ago||
Exactly. Nothing stops your writing a high-performance parallel database in TypeScript today. Given that runtimes and tooling are actually pretty good, I think TypeScript is actually a fine choice of language for the task.

The only thing you can't do with JS today is share a heap across threads. You have SharedArrayBuffer. You have atomics. You don't need a shared address space.

There's a high performance database called "PostgreSQL" you may have heard about. It doesn't use threads. It uses separate processes and shared memory: just like standard JavaScript, with its service workers and SharedArrayBuffer.

If not sharing an address space is good enough for PostgreSQL, it's good enough for your TypeScript database.

The problem with shared-everything, unmarked, preemptive-parallel concurrency is that 90% of the time it gets used by people who don't know they shouldn't.

jitl 4 days ago||
SharedArrayBuffer gives you a byte array, and asks you to re-implement the entire JavaScript universe on top of that. String -> use TextEncoder to copy in / out of a byte buffer. Object/class? Sure, devise your own protocol to serialize + copy instances in/out of a byte array. Even basic arithmetic operations on a TypedArray over a SharedArrayBuffer is slow AF in v8 compared to a "native" JS Array<number> for small-medium integers, because our current JIT compilers can jit the fuck out of regular arrays but struggle with TypedArray stuff. It's so sad.
quotemstr 3 days ago||
> Even basic arithmetic operations on a TypedArray over a SharedArrayBuffer is slow AF in v8 compared to a "native" JS Array<number> for small-medium integers, because our current JIT compilers can jit the fuck out of regular arrays but struggle with TypedArray stuff. It's so sad.

Yeah, that sucks, but at least it's just an implementation problem. Java's analogous ByteBuffer methods have been fast compiler intrinsics for a long time, so there's an existence proof of this stuff done right.

ozgrakkurt 4 days ago|||
This sounds pretty insane if you wrote any database code that needs to care about memory management at all
Groxx 4 days ago|||
Are you hoping to, like, run postgres in nodejs or something?

You can get parallelism with web workers and shove sqlite over there if you like, e.g. for running more intensive queries. Beyond that I kinda don't see much of a reason to use JS for databases, except maybe for isolation (e.g. via wasm).

forrestthewoods 4 days ago|||
…but why? JS/TS does not seem like the right tool for the job?
nesarkvechnep 4 days ago||
It's probably what they know so not anything new should be learned.
piterrro 4 days ago||
I honestly should print that comment and hang it on a wall.

> …competitively-performant… Care to explain competitively to what?

asxndu 4 days ago||
I am shocked by how good and comprehensive the bun docs & ecosystem is.

Its so well contained I never need to look outside its ecosystem for basic components. It's a true "Batteries Included" runtime.

Retr0id 4 days ago||
Last time I read the bun docs I spotted an off-by-one bug in sample code, so I opened a github issue. An AI bot responded, confirming the issue, and opened a PR to fix it - A simple "+ 1" added in the right place. Two other AI bots reviewed the PR, which went on for several rounds of "improvements". Last time I checked, neither the issue nor the PR received any human attention (actually I just checked again, and the PR has been closed by stalebot).
Aurornis 4 days ago|||
> (actually I just checked again, and the PR has been closed by stalebot).

Can you provide the link?

Delgan 4 days ago|||
I, too, was curious to see it in practice.

Here is the ticket opened by @retr0id: https://github.com/oven-sh/bun/issues/28030

And here is the swarm of bots / LLMs / agents that open, review and bikeshed the PR before it's closed by the stalebot: https://github.com/oven-sh/bun/pull/28031

It's hilarious. But also a little sad.

Retr0id 4 days ago|||
Yup, that's the one.
Phelinofist 4 days ago||
That's pure comedy
dgellow 4 days ago||||
Ok, you didn’t lie, it’s extremely funny, the stalebot at the end is such a good punchline!
skeledrew 4 days ago|||
Why sad?
Retr0id 4 days ago||
It's a bizarre simulation of what would normally be a social interaction, and the (minor) effort I put in on my end just went into a black hole.
skeledrew 3 days ago||
I see. I'm kinda wondering, did something break in the process? Or was it missing a human to do the final check and merge?
tomjakubowski 4 days ago|||
Here's a trivial docs issue I opened, where I had a similar experience:

https://github.com/oven-sh/bun/issues/31233

The difference is that the PRs to fix that problem were already open when I created the issue. I was unaware of them (I only searched for duplicate issues, not PRs addressing the problem). The robobun comment implies there are 5 open PRs addressing it, but I could only find two. They still haven't been merged, a month later.

https://github.com/oven-sh/bun/pull/30677 <-- later rolled up into:

https://github.com/oven-sh/bun/pull/30747

anematode 4 days ago||
Utter dysfunction. I ripped out bun from my projects after the vibe Rust rewrite, but seems like the problem has existed much longer...
asxndu 4 days ago|||
Was the bug actualy soved?
masklinn 4 days ago||
No, it was closed as stale.
jvidalv 4 days ago|||
Bun is so good that can’t be used as server and only as local script runner.

https://discord.com/channels/876711213126520882/148058965798...

Leaks memory left and right. And the core team seems unable to fix it.

fg137 4 days ago||
Yet I rarely hear about it being used in production systems and replacing Node.js.
tomjakubowski 4 days ago|||
From what I've heard there are two main use cases:

- People use bun as an all-in-one frontend web bundler. Personally, I just use esbuild (and webpack, if I'm working on a system using its module federation, like Jupyterlab). My understanding is bun has a machine-translated port of esbuild (ported to Zig, then to Rust) built into it.

- Claude Code runs on bun.

The second point has to be why Anthropic acquired them.

cozzyd 4 days ago||
Somehow claude-cli left open completely nerfs my laptop's battery life. Is this a bun feature?
doodlesdev 4 days ago||||
It famously is extremely memory leaky, with the core team having no idea how to fix it. With the new AI-automated unsafe Rust migration, this piece of slop may never actually become production-ready.
egorfine 4 days ago|||
I run it in production for multiple systems.

Ready to migrate back to node once the slop version is out.

dgellow 4 days ago||
Why not deno?
egorfine 4 days ago||
I'm not sure deno has a future.
Decabytes 4 days ago||
I'm curious to hear from people if anyone who uses Bun after this changed has noticed any day to day differences with the rust version over the zig version
gkfasdfasdf 4 days ago|
The latest version of Claude Code is the compiled with the Rust version of Bun, I've been using it and haven't noticed any regressions.
l11r 4 days ago||
tbh, that looks like AI psychosis caused by getting access to unlimited tokens
Yoric 4 days ago||
Eh, Firefox/Thunderbird had multi-threaded JS in SpiderMonkey in the late 90s.

Then it was removed it because it made garbage-collection a real mess (the JavaScript gc needs to walk through lots of C++ data, some of it may have specific requirements for destruction/finalization).

I hope it's better this time :)

pjmlp 4 days ago|
The JS / interoperability is why V8 eventually added a C++ GC.
Yoric 3 days ago||
But last time I checked (which was admittedly a while ago), JavaScriptCore didn't have one, no?
pjmlp 3 days ago||
No idea, I never used it, nor looked into its internals.
lmc 4 days ago||
Great, this will be the savior of software devs - as the last memory safety bugs are being patched, a whole new world of javascript multithreading issues to fix opens up.
skeledrew 4 days ago||
This has me thinking of Python's NoGIL movement.
richardbarosky 4 days ago|
Don't have much to say on the topic but recalled this excerpt from the book Coders at Work in the chapter interviewing Douglas Crockford.

``` In my experience, the worst bugs are the real-time bugs, which have to do with interactions with multiple threads. My approach to those bugs is to avoid making them. So I don't like threads. I think threads are an atrocious programming model. They're an occasionally necessarily evil, but they're not necessary for most of the things we use threads for.

One of the things I like about the browser model is that we only get one thread. Some people complain about that—if you lock up that thread, then the browser's locked up. So you just don't do that. There are constantly calls for putting threads into JavaScript and so far we've resisted that. I'm really glad we have.

The event-based model, which is what we're using in the browser, works really well. The only place where it breaks down is if you have some process that takes too long. I really like the approach that Google has taken in Gears to solving that, where they have a separate process which is completely isolated that you can send a program to and it'll run there. When it's finished, it'll tell you the result and the result comes back as an event. That's a brilliant model. ```

RealityVoid 4 days ago||
Soo... Essentially, still threads, but no shared state between threads, and they talk through this message interface?
masklinn 4 days ago||
Threads which can’t share state are called processes.
RealityVoid 4 days ago||
Fair-ish point, if a bit cheeky. But threads can be still threads, i.e. in the same address space, but without actually having shared state between them. And then they're still threads, not processes.

You could also have stuff like message ques going between them, the cost of passing data around is small then, you don't have do leave user space to put stuff in the mq's. But can you still call it "not having shared state"? I'd say yes, even though you do share memory and you do have the mutually accessible que. But I can see why you could argue otherwise.

masklinn 3 days ago||
> Fair-ish point, if a bit cheeky. But threads can be still threads, i.e. in the same address space, but without actually having shared state between them. And then they're still threads, not processes.

If they don’t share state implicitly, then by all relevant features they are processes. That they share an address space is not really relevant except as an opportunity for optimisation. See BEAM.

Hell, older consumer OS used to not have memory isolation, even after multitasking was introduced.

jitl 4 days ago||
this is why operating systems have forbidden threads since 1999 and no one has ever used them since.
More comments...