Background
I am using Atomics
daily as full time job to enable WASM FFI and whatnot and it's gorgeous ... but ... it requires me a double orchestration on Atomics.wait
in order to grab the SAB desired length for a result first, then wait on the new SAB buffer population to retrieve results.
Today I've just thought that if a Worker can be paused, or put in idle yet blocking state for the worker itself, maybe we could have a "soft idle" mechanism that wouldn't block entirely the event loop so that postMessage
dance can be allowed while still blocking the rest of the code execution up to that waitUntil
call?
Proposal
The waitUntil
method would be available and work exactly like wait
but its argument (one of its arguments) is a promise that would either resolve or reject at some point in time. If it resolves it returns ok
if it rejects it returns aborted
(a new entry for the current state of affairs)
All other values and behavior around timeout, delay, or count would be the same and the only special thing here is that listeners, or at least the very next message
event received in that worker, would be possible to trigger from the main
thread, or the foreign thread that was handling such SAB (which is SharedArrayBuffer in case it wasn't clear).
Thoughts?
What does it solve?
Currently I need to invoke elsewhere a function, serialize its result, notify the length of such serialized result, wait again over a new SAB with enough length to fill all chars from that result, notify it's done, unserialize at the target realm and move on.
Because structuredClone doesn't offer a way to serialize in a buffer friendly way (and deserialize at the target), this is unnecessarily slow due me needing to use a library to serialize recursive + complex data and deserialize it ... it all works already, but it's unnecessarily slower than me asking to invoke something elsewhere, return the result via postMessage
and get rid of the middle home-made serializer logic ... it will save ms after all, but when many worker <-> main operations happen, those ms will be seconds or more in the long run.
Previous work
We have event.waitUntil
in ServiceWorkers handlers so that naming it similarly was kinda natural to me but I don't have any strong opionion about the naming here, it's the ability to improve the worker <-> main dance that matters.