Repository navigation
JavaScriptEventLoop: Add opt-in yieldInterval for host responsiveness - #821
Conversation
|
TBH, I'm not sure if we should bring this to the global executor because:
Having said that, the option is opt-in, so it's fine if this is a quick big win for you 👍 |
|
@kateinoigakukun On 2: agreed, our case is probably unusual: a big SQLite write in the browser, spread across concurrent tasks in shared storage code. I tried local yields first, but If you'd rather not carry this in upstream, I'm fine keeping it in our fork 🙏🏻 |
|
Interesting, thanks for the data point! Then I think it could be a useful valid option even for other applications 👍 |
|
@kateinoigakukun should I include changes from linked draft PR? |
|
The heuristic looks simple enough so worth including it |
Overview
JobQueue.runAllJobs()runs every queued job in a single microtask, including jobs enqueued along the way. Long-running async work blocks rendering, input, timers, and messages until it completes, and a Web Worker can't respond to messages in the meantime.Task.yield()doesn't help because the yielded task goes back into the same drain. Yielding from app code withTask.sleeponly works if every ready task does it, and their timers then expire together.This PR adds an opt-in
yieldInterval. When set, a drain stops once the interval has passed and the remaining jobs continue in a new JavaScript task. The default (nil) keeps the current behavior, so latency-sensitive apps are unaffected.1. Drain loop. After each job,
runAllJobs()checks the elapsed time. Once the interval has passed and jobs remain, it schedules a continuation and returns withisSpinningstill set, so new jobs wait for the continuation instead of starting another drain. Jobs are never interrupted.2. Continuation.
setImmediate(Node.js, Bun), elseMessageChannel(browsers, Workers, Deno), elsesetTimeout(0).MessageChannelavoids the 4 ms clamp on nested timers; withsetTimeout, the workload below was about 15% slower.3. Clock.
ContinuousClock, orperformance.now()in Embedded Swift. A clock read goes through the WASI shim and costs more than a small job, so the drain reads it every N jobs. N starts at 1 and doubles (up to 256) while less than a quarter of the interval has passed, so slow jobs are still checked after every job and long runs of small jobs pay for a read only now and then. N goes back to 1 at the start of every drain. Trade-off: if a drain switches from many small jobs to slow ones, up to N - 1 slow jobs can run before the next read, so the yield can come later than the interval.Measurements
A Swift/Wasm app that writes a 255k-node graph to SQLite, Chrome, medians:
Task.sleepbefore each batchyieldInterval = 16 msThe remaining 0.6 s is a single synchronous job in the app, which no scheduler can split.
Scheduling overhead, 1M chained
Task.yield()hops, Node.js, release:nilyieldIntervalsetTesting
JavaScriptEventLoopYieldIntervalTests(XCTest on Node.js):yieldIntervalisnilby default;make unittestJavaScriptEventLoopbuilds with the Embedded SDK, and a small Embedded client that sets the interval runs in Node.js and lets a JS timer run between jobs (without the interval, it does not).Task.yield()hops, 3 interleaved runs per build, median of 5 rounds each../Utilities/format.swiftapplied.Docs: a doc comment on
yieldIntervaland one bullet in the JavaScript Interop Cheat Sheet.