From f87739ecd7eda7a0242ddac9ce81092711b231ff Mon Sep 17 00:00:00 2001 From: Noam Rosenthal Date: Wed, 6 Oct 2021 08:02:19 +0300 Subject: [PATCH] Explicit algorithm for idle callback "deadline" The algorithm replaces the algorithm that is currently defined in prose here: https://w3c.github.io/requestidlecallback/#the-idledeadline-interface). It works as such: - Timers can calculate their estimated next callback timestamps, based on their start time and document-inactive time so far. - The deadline for idle tasks for an event loop is the earliest between: - The time until the next timer callback in this event loop is estimated to fire. - The time until the next render, estimated at16ms after the start of the previous task that had a rended opportunity, if there are pending requestAnimationFrame callbacks. - 50ms TODO: the deadline is currently checked between windows, it needs to be re-checked between callbacks in the same window. See https://github.com/w3c/requestidlecallback/issues/71 --- source | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/source b/source index a17e9648185..bc0c85ff0a5 100644 --- a/source +++ b/source @@ -93525,6 +93525,8 @@ import "https://example.com/foo/../module2.mjs"; which is initially false. It is used to prevent reentrant invocation of the perform a microtask checkpoint algorithm.

+

Each window event loop has a DOMHighResTimeStamp + estimated time of next render, initially set to zero.

Queuing tasks
@@ -93781,7 +93783,8 @@ import "https://example.com/foo/../module2.mjs";
  • If docs is not empty, then set hasARenderingOpportunity to - true.

  • + true and set this's estimated time of next render to taskStartTime + plus 16.

  • Unnecessary rendering: Remove from docs all Document objects @@ -93895,7 +93898,7 @@ import "https://example.com/foo/../module2.mjs";

  • -

    If all of the following are true +

    If all of the following are true:

    -

    then for each Window object whose relevant agent's - event loop is this event loop, run the - start an idle period algorithm, passing the Window.

    +

    then:

    + +
      +
    1. Let maxDeadline be now plus 50.

    2. + +
    3. Let windows be all Window objects whose + relevant agent's event loop is + this event loop.

    4. + +
    5. +

      For each Window win in windows:

      + +
        +
      1. Let deadline be maxDeadline. + +

      2. +

        For each Window windowToCheck in windows:

        + +
          +
        1. If the result of calling windowToCheck's + estimate next callback time is between now and + deadline, set deadline to that value.

        2. + +
        3. If windowToCheck's map of animation frame callbacks is not + empty and windowToCheck's estimated time of next render is + between now and deadline, set deadline to that + value.

        4. +
        + +

        We need to run this step for every window to make sure any timeouts added + during the idle period algorithm are taken into account. A non-critical task + (requestIdleCallback) might schedule a critical task + (setTimeout / requetAnimationFrame).

        +
      3. + +
      4. If the current high resolution time is greater than deadline, + abort this loop.

      5. + +
      6. Start an idle period algorithm for win with + deadline.

      7. +
      +
    6. +
  • @@ -96328,7 +96370,8 @@ enum DOMParserSupportedType {

    Objects that implement the WindowOrWorkerGlobalScope mixin have a list of active timers. Each entry in this lists is identified by a number, which must be unique within the list for the lifetime of the object that implements the - WindowOrWorkerGlobalScope mixin.

    + WindowOrWorkerGlobalScope mixin, and has an estimate next callback time, + an algorithm returing a DOMHighResTimestamp.


    @@ -96494,6 +96537,29 @@ enum DOMParserSupportedType {
  • Let task's timer nesting level be nesting level.

  • +
  • Let startTime be the current high resolution time.

  • + +
  • +

    Set the estimate next callback time algorithm for the entry in the + list of active timers associated with handle to the following steps:

    + +
      +
    1. +

      Let estimatedCallbackTime be startTime plus timeout, + plus the duration in milliseconds in which the document has not been + fully active since startTime, plus a number representing an + implementation-defined length of time in milliseconds.

      +
    2. + +
    3. Let now be the current high resolution time.

    4. + +
    5. If estimatedCallbackTime is less than now, return + now.

    6. + +
    7. Return estimatedCallbackTime.

    8. +
    +
  • +
  • Return handle, and then continue running this algorithm in parallel.