Skip to content

Truly independent web browser

LibWeb: Only look for rejected promises after one was rejected

Andreas Klingc717d81c7c28
diff --git a/Libraries/LibWeb/Bindings/MainThreadVM.cpp b/Libraries/LibWeb/Bindings/MainThreadVM.cpp
index 013b014492f..df233d1ce5b 100644
--- a/Libraries/LibWeb/Bindings/MainThreadVM.cpp
+++ b/Libraries/LibWeb/Bindings/MainThreadVM.cpp
@@ -218,6 +218,7 @@ void initialize_main_thread_vm(HTML::AgentType type)
case JS::Promise::RejectionOperation::Reject:
// 1. Append promise to global's about-to-be-notified rejected promises list.
global_mixin.push_onto_about_to_be_notified_rejected_promises_list(promise);
+ HTML::main_thread_event_loop().did_reject_promise_without_handler();
break;
// 7. If operation is "handle",
case JS::Promise::RejectionOperation::Handle: {
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
index ea85c3742da..99168340681 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
@@ -1761,16 +1761,19 @@ void EventLoop::perform_a_microtask_checkpoint()
}
// 4. For each environment settings object settingsObject whose responsible event loop is this event loop, notify about rejected promises given settingsObject's global object.
- GC::RootVector<GC::Ref<EnvironmentSettingsObject>> environments;
- for (auto const& entry : m_related_environment_settings_objects)
- environments.append(*entry.value);
- for (auto& environment_settings_object : environments) {
- auto& global_object = environment_settings_object->global_object();
- if (auto* worklet_global_scope = Bindings::impl_from<WorkletGlobalScope>(&global_object)) {
- worklet_global_scope->notify_about_rejected_promises({});
- continue;
+ // OPTIMIZATION: This runs for every callback, and only a promise rejected since the last time gives it anything to do.
+ if (exchange(m_has_rejected_promises_to_notify_about, false)) {
+ GC::RootVector<GC::Ref<EnvironmentSettingsObject>> environments;
+ for (auto const& entry : m_related_environment_settings_objects)
+ environments.append(*entry.value);
+ for (auto& environment_settings_object : environments) {
+ auto& global_object = environment_settings_object->global_object();
+ if (auto* worklet_global_scope = Bindings::impl_from<WorkletGlobalScope>(&global_object)) {
+ worklet_global_scope->notify_about_rejected_promises({});
+ continue;
+ }
+ relevant_window_or_worker_global_scope(global_object).notify_about_rejected_promises({});
}
- relevant_window_or_worker_global_scope(global_object).notify_about_rejected_promises({});
}
// 5. Cleanup Indexed Database transactions.
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h b/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
index 220ae00d53c..2539e07c8a2 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
@@ -174,6 +174,8 @@ public:
double compute_deadline() const;
+ void did_reject_promise_without_handler() { m_has_rejected_promises_to_notify_about = true; }
+
void did_run_idle_callback() { m_task_generation_of_last_idle_callback = m_task_generation; }
void did_request_idle_callback();
@@ -283,6 +285,7 @@ private:
// https://html.spec.whatwg.org/multipage/webappapis.html#performing-a-microtask-checkpoint
bool m_performing_a_microtask_checkpoint { false };
+ bool m_has_rejected_promises_to_notify_about { false };
u32 m_microtask_checkpoint_deferrals { 0 };
mutable Vector<GC::Weak<DOM::Document>> m_documents;