hack-pad / hackpad

The in-browser IDE for Go
https://hackpad.org
Apache License 2.0
532 stars 37 forks source link

Use Web Workers for true concurrency #11

Open JohnStarich opened 3 years ago

JohnStarich commented 3 years ago

The biggest performance bottleneck for hackpad right now appears to be true parallelization. The Go CLI performs starts compile and link processes, which could benefit greatly with real parallelization and moving off of the main thread (even though they run in the background).

Going further, I think it could make sense to set up the kernel as a single SharedWorker, with each browser tab representing a new abstract "worker" concept that can run processes. Each tab would start by registering itself as a worker and "requesting" an editor be spawned on itself. Using a single kernel would keep overhead much lower, and enable some interesting inter-tab and project behaviors.

Moving from a single main/background thread to multi-worker is a big undertaking, so this could take a while.

gedw99 commented 3 years ago

Have you seen go-app?

https://github.com/maxence-charriere/go-app

it uses web workers

gedw99 commented 3 years ago

https://github.com/realPy/hogosuru/blob/main/broadcastchannel/broadcastchannel.go#L4 might help !

Been using all of this code with gio: https://github.com/realPy/hogosuru/issues/36

JohnStarich commented 3 years ago

FYI I've made some progress with Workers, though it's been slow-going. MessagePorts are great, but they sure are hard to debug when they're inside the runtime.

I think after this is implemented, I might reorganize everything – I can only name so many different objects "worker," "process," and "filesystem" before I start going crazy πŸ™ƒ

JohnStarich commented 3 years ago

Thanks for the contributions @gedw99 You're awesome πŸ˜„

gedw99 commented 3 years ago

looking forward to seeing this.

I am trying to use services workers, and to also have the exact same things running outside WASM. SO match the Service workers spec, to build a golang proxy, just like Service workers.

This will allow the same golang wasm to run inside a browser, as well as on any other device.

gedw99 commented 3 years ago

I think after this is implemented, I might reorganize everything – I can only name so many different objects "worker," "process," and "filesystem" before I start going crazy πŸ™ƒ

If oyu have a branch or code to look at even if its in bad shape, let me know. It will be interesting to me for my work in matching the Browser environment in a non browser environment.

JohnStarich commented 3 years ago

@gedw99 Definitely. Current progress is now pushed up as #18

gedw99 commented 3 years ago

Thanks πŸ™ @JohnStarich for the heads up.

is it ok if I wait until it’s finished as it’s holidays here and I am trying to avoid work stuff as much as possible

JohnStarich commented 3 years ago

No worries! I'm slowly attacking the problem bit by bit, eventually we'll get it done πŸ˜„

paralin commented 2 years ago

Building parallelized programs w/ Go in the browser w/ WebWorkers is one of the goals of ControllerBus: https://github.com/aperturerobotics/controllerbus

There's a lot of time invested towards building modular pluggable programs in that repo, communicating via IPC over MessageChannel. It could be useful for this purpose.

JohnStarich commented 2 years ago

Interesting. Thanks for sharing @paralin

JohnStarich commented 2 years ago

This problem has been haunting me for quite some time πŸ™‚ I'm still working on it, though progress remains slow. A new implementation has been pushed. This one crashes less and the design is slightly improved.

Just need to figure out these silly crashes. The traces are incredibly unhelpful πŸ€”

JohnStarich commented 2 years ago

Made some good progress. Finally found and fixed a callback blocking the event loop which triggered a crash loop.

Now, onto making pipes work across MessageChannels!

JohnStarich commented 2 years ago

Finally have the draft PR working with web workers πŸŽ‰ Still have some issues with stdin/stdout on the terminal, but this refactor is nearly done!

The next major part after web workers is attempting to parallelize the calls to the compile and link tools. The compiler seems to be avoiding them at the moment, though I'm not sure I understand it yet. I might be hitting transaction throughput issues in IndexedDB.

oliverlj commented 1 year ago

In our project, we used postMessage to communicate with a web worker. https://gitlab.com/c1560/cryptofiscafacile/-/blob/develop/wasm/src/wasm-worker-proxy.ts

amlwwalker commented 1 year ago

@JohnStarich any luck on this? This seems like the big refactor that this project needs and last update sounded positive!