tc39 / proposal-atomics-microwait

Micro waits in JS
http://tc39.es/proposal-atomics-microwait/
Other
37 stars 0 forks source link

Why have microwait take an integer? #3

Closed bakkot closed 5 months ago

bakkot commented 5 months ago

The non-negative integer argument iterationNumber is a hint to implement a backoff algorithm if the microwait itself is in a loop. It defaults to 0. Atomics.microwait(n) waits at most as long as Atomics.microwait(n+1).

Why not just have engines count how many times microwait has been called since the last microtask or turn of the event loop, so users don't have to remember to track it themselves?

syg commented 5 months ago

Because my intuition is there's no correlation between microtask checkpoints and event loop turns and how many times a busy loop is performed. The point of a busy loop with a microwait in order to acquire micro-contended locks quickly without sleeping. It seems reasonable that sometimes you'd acquire (and release) multiple such locks in a single run-to-completion, no?

bakkot commented 5 months ago

Well, there is a correlation, which is that the number of times the busy loop is performed is upper bounded by the number of times microwait is called in a given turn. But yes, it's true that you might reasonably have multiple such loops within a single turn. I guess since this is designed for low-level stuff it's reasonable to expect the developer to handle this sort of bookkeeping explicitly.