Skip to content

Truly independent web browser

LibWeb: Queue the task of a timer without a delay right away

Andreas Klingc723d38bff13
diff --git a/Libraries/LibWeb/HTML/Timer.cpp b/Libraries/LibWeb/HTML/Timer.cpp
index cc481687adb..8f7498961a4 100644
--- a/Libraries/LibWeb/HTML/Timer.cpp
+++ b/Libraries/LibWeb/HTML/Timer.cpp
@@ -51,6 +51,11 @@ void Timer::stop()
m_timer->stop();
}
+void Timer::fire_now()
+{
+ m_timer->on_timeout();
+}
+
void Timer::restart(i32 milliseconds)
{
m_earliest_firing = MonotonicTime::now() + AK::Duration::from_milliseconds(milliseconds);
diff --git a/Libraries/LibWeb/HTML/Timer.h b/Libraries/LibWeb/HTML/Timer.h
index 481596a87cc..f6e1c53d100 100644
--- a/Libraries/LibWeb/HTML/Timer.h
+++ b/Libraries/LibWeb/HTML/Timer.h
@@ -44,6 +44,9 @@ public:
void restart(i32 milliseconds);
bool is_active() const;
+ // Runs what the timer runs when it fires, without waiting for the clock.
+ void fire_now();
+
void set_callback(Function<void()>);
void set_interval(i32 milliseconds);
@@ -59,6 +62,7 @@ public:
// neither queues a task. Running the task releases the claim.
[[nodiscard]] bool claim_firing();
void release_firing() { m_has_queued_firing = false; }
+ bool has_queued_firing() const { return m_has_queued_firing; }
private:
Timer(i32 milliseconds, Function<void()> callback, i32 id, Repeating, TimerThrottlingClass, double deadline);
diff --git a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
index ca65cf962a2..dbb7d628214 100644
--- a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
+++ b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
@@ -1525,6 +1525,8 @@ void WindowOrWorkerGlobalScopeMixin::run_steps_after_a_timeout_impl(i32 timeout,
// throttled_timer_delay()), so the delay the timer is armed with can differ from the one it was asked for.
auto deadline = HighResolutionTime::unsafe_shared_current_time() + timeout;
auto throttled_delay = throttled_timer_delay(throttling_class, deadline);
+ if (throttling_class != TimerThrottlingClass::Immediate || repeat == Repeat::Yes)
+ m_earliest_timer_deadline = min(m_earliest_timer_deadline, deadline);
// NB: The intensive tier paces chained timers by their wake-ups (see throttled_timer_delay()), so note when one
// runs while the document is hidden. The step itself goes into a GC::Function rather than into the wrapper's
@@ -1584,8 +1586,31 @@ void WindowOrWorkerGlobalScopeMixin::run_steps_after_a_timeout_impl(i32 timeout,
// NB: Don't restart an already-active repeating timer. It's already firing on schedule and
// restarting it would cause drift (next fire = now + interval instead of previous fire + interval).
- if (!existing_timer)
- timer->start();
+ if (!existing_timer) {
+ // OPTIMIZATION: A timer with no delay has nothing to wait for. Unless a timer that expired before this one started
+ // still waits for the platform event loop, it needs no pass through that loop, so its task comes
+ // before the tasks queued after this. One that does not repeat needs no clock at all.
+ bool can_fire_now = throttling_class == TimerThrottlingClass::Immediate && !has_expired_timer_yet_to_fire(deadline);
+ if (!can_fire_now || repeating == Timer::Repeating::Yes)
+ timer->start();
+ if (can_fire_now)
+ timer->fire_now();
+ }
+}
+
+// NB: An expired timer's firing comes from the platform event loop, and its task goes ahead of those of the timers that
+// started after it expired. m_earliest_timer_deadline never comes after the real earliest deadline, so this only
+// looks through the timers after that deadline has passed.
+bool WindowOrWorkerGlobalScopeMixin::has_expired_timer_yet_to_fire(double now)
+{
+ if (now < m_earliest_timer_deadline)
+ return false;
+ m_earliest_timer_deadline = NumericLimits<double>::max();
+ for (auto& it : m_timers) {
+ if (it.value->is_active() && !it.value->has_queued_firing())
+ m_earliest_timer_deadline = min(m_earliest_timer_deadline, it.value->deadline());
+ }
+ return now >= m_earliest_timer_deadline;
}
// A delay for Core::Timer: whole milliseconds, never negative, and no more than it can hold.
diff --git a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h
index 02e665e0b94..c19b8f41cd2 100644
--- a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h
+++ b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h
@@ -162,6 +162,7 @@ private:
No,
};
WebIDL::ExceptionOr<i32> run_timer_initialization_steps(TimerHandler handler, i32 timeout, GC::RootVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id = {});
+ bool has_expired_timer_yet_to_fire(double now);
void run_steps_after_a_timeout_impl(i32 timeout, TimerThrottlingClass, Function<void()> completion_step, Optional<i32> timer_key, Repeat repeat = Repeat::No);
bool document_is_hidden() const;
bool timers_are_throttled() const;
@@ -177,6 +178,7 @@ private:
IDAllocator m_timer_id_allocator;
HashMap<int, GC::Ref<Timer>> m_timers;
+ double m_earliest_timer_deadline { NumericLimits<double>::max() };
// The interval between the wake-ups a hidden document holds its delayed timers back to; a test can shorten it.
double m_hidden_document_timer_wake_up_interval { 1000 };
diff --git a/Tests/LibWeb/Text/expected/HTML/timer-without-delay-after-expired-timer.txt b/Tests/LibWeb/Text/expected/HTML/timer-without-delay-after-expired-timer.txt
new file mode 100644
index 00000000000..3ebf6b813aa
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/timer-without-delay-after-expired-timer.txt
@@ -0,0 +1,2 @@
+expired timer
+timer without a delay
diff --git a/Tests/LibWeb/Text/expected/HTML/timer-without-delay-runs-before-later-tasks.txt b/Tests/LibWeb/Text/expected/HTML/timer-without-delay-runs-before-later-tasks.txt
index 6d2029fe892..a84597d259c 100644
--- a/Tests/LibWeb/Text/expected/HTML/timer-without-delay-runs-before-later-tasks.txt
+++ b/Tests/LibWeb/Text/expected/HTML/timer-without-delay-runs-before-later-tasks.txt
@@ -1 +1 @@
-microtask message port window message timeout 1 timeout 2
+microtask timeout 1 message port window message timeout 2
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/syntax/parsing/DOMContentLoaded-defer.txt b/Tests/LibWeb/Text/expected/wpt-import/html/syntax/parsing/DOMContentLoaded-defer.txt
index 2e5370ca6a8..0ce83f0f649 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/syntax/parsing/DOMContentLoaded-defer.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/syntax/parsing/DOMContentLoaded-defer.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail The end: DOMContentLoaded and defer scripts
\ No newline at end of file
+1 Pass
+Pass The end: DOMContentLoaded and defer scripts
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/HTML/timer-without-delay-after-expired-timer.html b/Tests/LibWeb/Text/input/HTML/timer-without-delay-after-expired-timer.html
new file mode 100644
index 00000000000..a6c85ce0ade
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/timer-without-delay-after-expired-timer.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(done => {
+ setTimeout(() => println("expired timer"), 1);
+
+ const start = performance.now();
+ while (performance.now() - start < 5) { }
+
+ setTimeout(() => {
+ println("timer without a delay");
+ done();
+ }, 0);
+ });
+</script>