w3c / requestidlecallback

Cooperative Scheduling of Background Tasks
https://w3c.github.io/requestidlecallback/
Other
50 stars 19 forks source link

Why do we have two lists of callbacks? #27

Closed bzbarsky closed 8 years ago

bzbarsky commented 8 years ago

There is a "list of idle request callbacks" and a "list of runnable idle callbacks" serves. I guess the idea is that if an idle callback is scheduled during an idle period it won't run until the next idle period? But why is that a desirable behavior? If that's not the intent, what is the intent?

rmcilroy commented 8 years ago

The two lists of callbacks are so that an idle callback scheduled during one idle period doesn't run until the next idle period. The reason for this is that it allows code like this:

This allows code patterns like the following:

    function doWork(deadline) {
      if (deadline.timeRemaining() <= 5) {
        // This will take more than 5ms so wait until we 
        // get called back with a long enough deadline.
        requestIdleCallback(doWork);
        return;
      }
      // do work...
    }

If this is called with a deadline of less than 5ms then it won't get run again until the next idle period, at which point the deadline may be large enough to do the work. If the inner requestIdleCallback got to run during the same idle period then it would run immediately with the same (or a slightly smaller) deadline each time until the deadline is finally used up, causing the CPU to burn power unnecessarily.

I'll add some text to the spec to explain this a bit better.

bzbarsky commented 8 years ago

Oh, right, because you can't say how much time you actually want. Definitely worth explaining better, especially because it means we make it impossible to actually schedule some more work for the current idle period.

igrigorik commented 8 years ago

Definitely worth explaining better, especially because it means we make it impossible to actually schedule some more work for the current idle period.

You could.. If you request multiple callbacks you can split your work across them and they'll be scheduled back-to-back. Not the greatest pattern, but it is possible.

bzbarsky commented 8 years ago

That requires you to know ahead of time that you will need to do multiple callbacks, no?

igrigorik commented 8 years ago

Yes it does, which is why I said it's not the greatest of patterns.. To your point in #28, I do agree that it might make sense to provide some finer control over the scheduling (e.g. back-to-back vs wait until next idle period vs I want to wait for a long (50ms) idle block).

rmcilroy commented 8 years ago

Sorry for the delay, I've been busy with other things. I've uploaded #35 to hopefully at least clarify the current idle posting semantics.

If we want to provide finer control over scheduling (i.e., back-to-back or waiting for a long idle period) we should probably open a separate issue to discuss that.

igrigorik commented 8 years ago

35 landed, closing. @bzbarsky please feel free to reopen if we need to do more work here.