tc39 / proposal-atomics-microwait

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

reducing heuristics #12

Closed michaelficarra closed 1 month ago

michaelficarra commented 3 months ago

In its current design, this proposal needs to use a heuristic to

  1. link multiple calls within the same spin-wait loop
  2. guess the backoff strategy being attempted

And assuming it is able to do both of those accurately, it doesn't need the programmer to provide an iteration count. So why not just ask the programmer to provide those things instead? Something like this:

Atomics.pause("linear decreasing", LOOP_INSTANCE_NONCE);

The parameters would be

  1. one of a predefined set of strings that identify a preferred backoff strategy
  2. a unique number that is consistent throughout each instance of a spin-wait loop

With this design, we've completely removed the heuristics, and we allow the engine to manage the number of pauses.

syg commented 3 months ago

I don't think a string argument would work because the comparison would take too long and dwarf the pause time.

If the string argument value were interned, then it's just a pointer comparison and that's fine, but interned strings are an implementation detail. So if the user were to pass a non-interned string they would get very different timing behavior, which is pretty undesirable.

I'm not against the general idea here, but strings in particular I'm skeptical about.

syg commented 3 months ago

With this design, we've completely removed the heuristics, and we allow the engine to manage the number of pauses.

Strong disagree with this characterization. You've made the heuristics to be more opaque and decoupled them from an integer, but the heuristics are definitely still there, if not more so.

Take the backoff strategy argument -- are we to enumerate a set of backoff strategies and prescribe exact behaviors for them? That's pretty bad, as the engine must be able to decide to not do anything for any argument if it decides the underlying CPU doesn't benefit from pausing.

Take the nonce -- an engine isn't going to use unlimited memory to track all possible nonces. Maybe an engine only tracks the most previous nonce (a cache with 1 entry), or some fixed N previous seen nonces for small N. More importantly, we shouldn't prescribe what to do.

syg commented 1 month ago

Closing this per off-line conversations with @michaelficarra who found the response reasonable. Please reopen if incorrect.