Title: [291476] trunk
Revision
291476
Author
[email protected]
Date
2022-03-18 04:58:07 -0700 (Fri, 18 Mar 2022)

Log Message

Remove the 1ms minimum for setTimeout
https://bugs.webkit.org/show_bug.cgi?id=221124
<rdar://problem/73852354>

Reviewed by Sam Weinig.

The HTML spec makes no mention of a 1ms minimum for timers. Removing
the 1ms minimum for setTimeout results in a 0.7-2.1% improvement on
Speedometer, depending on platform and hardware.

The WPT added here demonstrates how this change can affect pages: if a
page schedules a 1ms and then a 0ms timeout in the same turn of the
event loop, then with this patch they will now be fired in the reverse
order. Firefox and Chrome do not impose a 1ms minimum, which reduces
the risk of this being a problem.

Not addressing the setTimeout 1ms minimum here, which should likely also
be removed.

While we're here, settle on "one shot" rather rather than "single
shot" as the term for timers that fire once.

Tests: imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any.html
       imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any.worker.html

* page/DOMTimer.h:
* page/DOMTimer.cpp:
(WebCore::DOMTimer::DOMTimer): We must pass oneShot into
intervalClampedToMinimum, since the way isOneShot determines whether
we are a one shot timer is by checking repeatInterval, which is only
set once startRepeating is called.
(WebCore::DOMTimer::install):
(WebCore::DOMTimer::fired):
(WebCore::DOMTimer::updateTimerIntervalIfNecessary):
(WebCore::DOMTimer::isOneShot const):
(WebCore::DOMTimer::intervalClampedToMinimum const):

Modified Paths

Added Paths

Diff

Added: trunk/LayoutTests/imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any-expected.txt (0 => 291476)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any-expected.txt	2022-03-18 11:58:07 UTC (rev 291476)
@@ -0,0 +1,3 @@
+
+PASS A 0ms timeout should not be clamped to 1ms
+

Added: trunk/LayoutTests/imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any.html (0 => 291476)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any.html	                        (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any.html	2022-03-18 11:58:07 UTC (rev 291476)
@@ -0,0 +1 @@
+<!-- This file is required for WebKit test infrastructure to run the templated test -->
\ No newline at end of file

Added: trunk/LayoutTests/imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any.js (0 => 291476)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any.js	                        (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any.js	2022-03-18 11:58:07 UTC (rev 291476)
@@ -0,0 +1,15 @@
+async_test((t) => {
+  let order = [];
+
+  function append(n) {
+    order.push(`${n}ms`);
+    if (order.length == 3) {
+      assert_array_equals(order, ["0ms", "1ms", "2ms"]);
+      t.done();
+    }
+  }
+
+  setTimeout(t.step_func(() => append(2)), 2);
+  setTimeout(t.step_func(() => append(1)), 1);
+  setTimeout(t.step_func(() => append(0)), 0);
+}, "A 0ms timeout should not be clamped to 1ms");

Added: trunk/LayoutTests/imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any.worker-expected.txt (0 => 291476)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any.worker-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any.worker-expected.txt	2022-03-18 11:58:07 UTC (rev 291476)
@@ -0,0 +1,3 @@
+
+PASS A 0ms timeout should not be clamped to 1ms
+

Added: trunk/LayoutTests/imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any.worker.html (0 => 291476)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any.worker.html	                        (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any.worker.html	2022-03-18 11:58:07 UTC (rev 291476)
@@ -0,0 +1 @@
+<!-- This file is required for WebKit test infrastructure to run the templated test -->
\ No newline at end of file

Modified: trunk/Source/WebCore/ChangeLog (291475 => 291476)


--- trunk/Source/WebCore/ChangeLog	2022-03-18 09:51:14 UTC (rev 291475)
+++ trunk/Source/WebCore/ChangeLog	2022-03-18 11:58:07 UTC (rev 291476)
@@ -1,3 +1,42 @@
+2022-03-18  Cameron McCormack  <[email protected]>
+
+        Remove the 1ms minimum for setTimeout
+        https://bugs.webkit.org/show_bug.cgi?id=221124
+        <rdar://problem/73852354>
+
+        Reviewed by Sam Weinig.
+
+        The HTML spec makes no mention of a 1ms minimum for timers. Removing
+        the 1ms minimum for setTimeout results in a 0.7-2.1% improvement on
+        Speedometer, depending on platform and hardware.
+
+        The WPT added here demonstrates how this change can affect pages: if a
+        page schedules a 1ms and then a 0ms timeout in the same turn of the
+        event loop, then with this patch they will now be fired in the reverse
+        order. Firefox and Chrome do not impose a 1ms minimum, which reduces
+        the risk of this being a problem.
+
+        Not addressing the setTimeout 1ms minimum here, which should likely also
+        be removed.
+
+        While we're here, settle on "one shot" rather rather than "single
+        shot" as the term for timers that fire once.
+
+        Tests: imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any.html
+               imported/w3c/web-platform-tests/html/webappapis/timers/zero-settimeout.any.worker.html
+
+        * page/DOMTimer.h:
+        * page/DOMTimer.cpp:
+        (WebCore::DOMTimer::DOMTimer): We must pass oneShot into
+        intervalClampedToMinimum, since the way isOneShot determines whether
+        we are a one shot timer is by checking repeatInterval, which is only
+        set once startRepeating is called.
+        (WebCore::DOMTimer::install):
+        (WebCore::DOMTimer::fired):
+        (WebCore::DOMTimer::updateTimerIntervalIfNecessary):
+        (WebCore::DOMTimer::isOneShot const):
+        (WebCore::DOMTimer::intervalClampedToMinimum const):
+
 2022-03-18  Antti Koivisto  <[email protected]>
 
         [CSS Container Queries] Basic support for container units

Modified: trunk/Source/WebCore/page/DOMTimer.cpp (291475 => 291476)


--- trunk/Source/WebCore/page/DOMTimer.cpp	2022-03-18 09:51:14 UTC (rev 291475)
+++ trunk/Source/WebCore/page/DOMTimer.cpp	2022-03-18 11:58:07 UTC (rev 291476)
@@ -49,6 +49,8 @@
 namespace WebCore {
 
 static const Seconds minIntervalForNonUserObservableChangeTimers { 1_s }; // Empirically determined to maximize battery life.
+static const Seconds minIntervalForOneShotTimers { 0_ms };
+static const Seconds minIntervalForRepeatingTimers { 1_ms };
 static const int maxTimerNestingLevel = 5;
 
 class DOMTimerFireState {
@@ -155,16 +157,16 @@
 
 bool NestedTimersMap::isTrackingNestedTimers = false;
 
-DOMTimer::DOMTimer(ScriptExecutionContext& context, Function<void(ScriptExecutionContext&)>&& action, Seconds interval, bool singleShot)
+DOMTimer::DOMTimer(ScriptExecutionContext& context, Function<void(ScriptExecutionContext&)>&& action, Seconds interval, bool oneShot)
     : SuspendableTimerBase(&context)
     , m_nestingLevel(context.timerNestingLevel())
     , m_action(WTFMove(action))
     , m_originalInterval(interval)
     , m_throttleState(Undetermined)
-    , m_currentTimerInterval(intervalClampedToMinimum())
+    , m_currentTimerInterval(intervalClampedToMinimum(oneShot))
     , m_userGestureTokenToForward(UserGestureIndicator::currentUserGesture())
 {
-    if (singleShot)
+    if (oneShot)
         startOneShot(m_currentTimerInterval);
     else
         startRepeating(m_currentTimerInterval);
@@ -172,17 +174,17 @@
 
 DOMTimer::~DOMTimer() = default;
 
-int DOMTimer::install(ScriptExecutionContext& context, std::unique_ptr<ScheduledAction> action, Seconds timeout, bool singleShot)
+int DOMTimer::install(ScriptExecutionContext& context, std::unique_ptr<ScheduledAction> action, Seconds timeout, bool oneShot)
 {
     auto actionFunction = [action = "" context) mutable {
         action->execute(context);
     };
-    return DOMTimer::install(context, WTFMove(actionFunction), timeout, singleShot);
+    return DOMTimer::install(context, WTFMove(actionFunction), timeout, oneShot);
 }
 
-int DOMTimer::install(ScriptExecutionContext& context, Function<void(ScriptExecutionContext&)>&& action, Seconds timeout, bool singleShot)
+int DOMTimer::install(ScriptExecutionContext& context, Function<void(ScriptExecutionContext&)>&& action, Seconds timeout, bool oneShot)
 {
-    Ref<DOMTimer> timer = adoptRef(*new DOMTimer(context, WTFMove(action), timeout, singleShot));
+    Ref<DOMTimer> timer = adoptRef(*new DOMTimer(context, WTFMove(action), timeout, oneShot));
     timer->suspendIfNeeded();
 
     // Keep asking for the next id until we're given one that we don't already have.
@@ -190,7 +192,7 @@
         timer->m_timeoutId = context.circularSequentialID();
     } while (!context.addTimeout(timer->m_timeoutId, timer.get()));
 
-    InspectorInstrumentation::didInstallTimer(context, timer->m_timeoutId, timeout, singleShot);
+    InspectorInstrumentation::didInstallTimer(context, timer->m_timeoutId, timeout, oneShot);
 
     // Keep track of nested timer installs.
     if (NestedTimersMap* nestedTimers = NestedTimersMap::instanceForContext(context))
@@ -198,7 +200,7 @@
 #if ENABLE(CONTENT_CHANGE_OBSERVER)
     if (is<Document>(context)) {
         auto& document = downcast<Document>(context);
-        document.contentChangeObserver().didInstallDOMTimer(timer.get(), timeout, singleShot);
+        document.contentChangeObserver().didInstallDOMTimer(timer.get(), timeout, oneShot);
         if (DeferDOMTimersForScope::isDeferring())
             document.domTimerHoldingTank().add(timer.get());
     }
@@ -288,7 +290,7 @@
     // wait unit the end of this function to delete DOMTimer.
     Ref<DOMTimer> protectedThis(*this);
 
-    bool _oneShot_ = !repeatInterval();
+    bool _oneShot_ = isOneShot();
 
     ASSERT(scriptExecutionContext());
     ScriptExecutionContext& context = *scriptExecutionContext();
@@ -350,7 +352,7 @@
     if (nestedTimers) {
         for (auto& idAndTimer : *nestedTimers) {
             auto& timer = idAndTimer.value;
-            if (timer->isActive() && !timer->repeatInterval())
+            if (timer->isActive() && timer->isOneShot())
                 timer->updateThrottlingStateIfNecessary(fireState);
         }
         nestedTimers->stopTracking();
@@ -374,22 +376,32 @@
     if (previousInterval == m_currentTimerInterval)
         return;
 
-    if (repeatInterval()) {
+    if (isOneShot()) {
+        LOG(DOMTimers, "%p - Updating DOMTimer's fire interval from %.2f ms to %.2f ms due to throttling.", this, previousInterval.milliseconds(), m_currentTimerInterval.milliseconds());
+        augmentFireInterval(m_currentTimerInterval - previousInterval);
+    } else {
         ASSERT(repeatInterval() == previousInterval);
         LOG(DOMTimers, "%p - Updating DOMTimer's repeat interval from %.2f ms to %.2f ms due to throttling.", this, previousInterval.milliseconds(), m_currentTimerInterval.milliseconds());
         augmentRepeatInterval(m_currentTimerInterval - previousInterval);
-    } else {
-        LOG(DOMTimers, "%p - Updating DOMTimer's fire interval from %.2f ms to %.2f ms due to throttling.", this, previousInterval.milliseconds(), m_currentTimerInterval.milliseconds());
-        augmentFireInterval(m_currentTimerInterval - previousInterval);
     }
 }
 
+bool DOMTimer::isOneShot() const
+{
+    return !repeatInterval();
+}
+
 Seconds DOMTimer::intervalClampedToMinimum() const
 {
+    return intervalClampedToMinimum(isOneShot());
+}
+
+Seconds DOMTimer::intervalClampedToMinimum(bool oneShot) const
+{
     ASSERT(scriptExecutionContext());
     ASSERT(m_nestingLevel <= maxTimerNestingLevel);
 
-    Seconds interval = std::max(1_ms, m_originalInterval);
+    Seconds interval = std::max(oneShot ? minIntervalForOneShotTimers : minIntervalForRepeatingTimers, m_originalInterval);
 
     // Only apply throttling to repeating timers.
     if (m_nestingLevel < maxTimerNestingLevel)

Modified: trunk/Source/WebCore/page/DOMTimer.h (291475 => 291476)


--- trunk/Source/WebCore/page/DOMTimer.h	2022-03-18 09:51:14 UTC (rev 291475)
+++ trunk/Source/WebCore/page/DOMTimer.h	2022-03-18 11:58:07 UTC (rev 291476)
@@ -69,6 +69,8 @@
     friend class Internals;
 
     WEBCORE_EXPORT Seconds intervalClampedToMinimum() const;
+    Seconds intervalClampedToMinimum(bool oneShot) const;
+    bool isOneShot() const;
 
     bool isDOMTimersThrottlingEnabled(Document&) const;
     void updateThrottlingStateIfNecessary(const DOMTimerFireState&);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to