LibWeb: Don't allocate a new HeapFunction 60 times per second

We can reuse the same HeapFunction when queueing up a rendering task
on the HTML event loop. No need to create extra work for the garbage
collector like this.
This commit is contained in:
Andreas Kling 2024-10-25 09:56:48 +02:00 committed by Andreas Kling
commit 5c6b879715
Notes: github-actions[bot] 2024-10-25 08:22:14 +00:00
2 changed files with 146 additions and 134 deletions

View file

@ -32,6 +32,10 @@ EventLoop::EventLoop(Type type)
{
m_task_queue = heap().allocate_without_realm<TaskQueue>(*this);
m_microtask_queue = heap().allocate_without_realm<TaskQueue>(*this);
m_rendering_task_function = JS::create_heap_function(heap(), [this] {
update_the_rendering();
});
}
EventLoop::~EventLoop() = default;
@ -43,6 +47,7 @@ void EventLoop::visit_edges(Visitor& visitor)
visitor.visit(m_microtask_queue);
visitor.visit(m_currently_running_task);
visitor.visit(m_backup_incumbent_settings_object_stack);
visitor.visit(m_rendering_task_function);
}
void EventLoop::schedule()
@ -239,7 +244,12 @@ void EventLoop::queue_task_to_update_the_rendering()
if (document->is_decoded_svg())
continue;
queue_global_task(Task::Source::Rendering, *navigable->active_window(), JS::create_heap_function(navigable->heap(), [this] mutable {
queue_global_task(Task::Source::Rendering, *navigable->active_window(), *m_rendering_task_function);
}
}
void EventLoop::update_the_rendering()
{
VERIFY(!m_is_running_rendering_task);
m_is_running_rendering_task = true;
ScopeGuard const guard = [this] {
@ -390,8 +400,6 @@ void EventLoop::queue_task_to_update_the_rendering()
document->fonts()->resolve_ready_promise();
}
}
}));
}
}
// https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-task

View file

@ -81,6 +81,8 @@ private:
virtual void visit_edges(Visitor&) override;
void update_the_rendering();
Type m_type { Type::Window };
JS::GCPtr<TaskQueue> m_task_queue;
@ -116,6 +118,8 @@ private:
bool m_skip_event_loop_processing_steps { false };
bool m_is_running_rendering_task { false };
JS::GCPtr<JS::HeapFunction<void()>> m_rendering_task_function;
};
EventLoop& main_thread_event_loop();