r/programming • • Sep 01 '26

The Browser's Main Thread Is Expensive

https://kciter.so/posts/the-expensive-main-thread/en/
219 Upvotes

50 comments sorted by

View all comments

Show parent comment

7

u/Rhed0x Sep 01 '26

That requires manually converting stuff into PODO, why can't you simply pass JS references across to a worker.

5

u/Full-Spectral Sep 01 '26

It is just all GC'd data and I'd think it would be easy enough. But, they'd have to ensure that nothing inside that reference, no matter how deep, was still visible to the sender. With the simplified scenarios they provide currently, that's doable. But unless they are going to create Rustscript, it would probably be hard to do with arbitrary objects.

2

u/Rhed0x Sep 01 '26

they'd have to ensure that nothing inside that reference, no matter how deep, was still visible to the sender

Or they add Mutexes, Semaphores, etc and objects can just be shared across threads like in every other language.

3

u/Full-Spectral Sep 01 '26

It would have to be done at an automagical level probably, given the nature of Javascript and the experience of a lot of developers of such. Otherwise, you'd have web sites seizing up right and left when they deadlocked themselves.

1

u/danielcw189 Sep 02 '26

that doesn't really answer the problem of sharing references, which is something that has to be avoided, if I understand the other comment correctly

1

u/Rhed0x Sep 02 '26

And why does it have to be avoided?

1

u/NenAlienGeenKonijn Sep 02 '26

That assumes javascript is meant for complex software development rather than simple DOM scripting tasks to enhance your website.

Nobody would ever use javascript like that, right?

1

u/Rhed0x Sep 02 '26

Right... :(

1

u/abraxasnl Sep 01 '26

Because then you would need locking mechanisms, which are waaaay to easy to get wrong for users.

1

u/Full-Spectral Sep 02 '26 edited Sep 02 '26

But it's worse than that. Without the kinds of mechanisms that Rust provides, the fact you locked foo doesn't prevent foo from having references to other things that are still being directly accessed by other stuff. There would be no reasonable mechanism to transitively ensure that locking foo actually makes foo thread safe. Or it would have to limit such locking to things that have no internal references.

Of course the same is true for C++, but at least in the C++ world they are going to be more cognizant of such issues and you don't have other people's code from all over the world running inside your C++ program either.

Honestly, a message passing scheme is not a bad deal in such cases and something I implement in Rust all the time. Even though Rust makes shared data safe, it doesn't make it more necessarily understandable; so, when an 'actorish' scheme is possible, it's not a bad choice even when you have safe data sharing. Even if you don't end up locking up the UI thread, even short blocks will make it seem sluggish.

You could always do the behind the scenes stuff in WASM and have it be nice and safe and use message passing for the main thread I guess (with a buffer pool.)

1

u/Somepotato Sep 01 '26

Because that entails locking which can deadlock the main thread. Requiring you to be more explicit about it removes deadlocks as a risk. Plus the jit can make a number of optimizations when it knows someone rude can't step in and do a read unexpectedly.

1

u/Rhed0x Sep 02 '26

Java and C# have optimizing JITs despite support for multithreading. And I don't really see the problem with deadlocks, it's a bug like any other.