LibWeb+AK: Use AK::Queue for the microtask queue

The microtask queue is a pure FIFO (enqueue at back, dequeue from
front) but was using a Vector, making every dequeue O(n) due to
element shifting.

Replace it with AK::Queue which has O(1) dequeue. This makes a huge
difference when processing large numbers of microtasks, e.g. during
async-heavy JavaScript workloads where each `await` generates a
microtask.

Also add a for_each() method to AK::Queue so the GC can visit the
queued tasks.
This commit is contained in:
Andreas Kling
2026-03-15 12:02:25 -05:00
committed by Shannon Booth
parent 0e2d8ff43a
commit a141c2c492
Notes: github-actions[bot] 2026-03-16 08:39:17 +00:00
3 changed files with 26 additions and 12 deletions

View File

@@ -8,6 +8,7 @@
#include <AK/Function.h>
#include <AK/Noncopyable.h>
#include <AK/Queue.h>
#include <LibCore/Forward.h>
#include <LibGC/Ptr.h>
#include <LibGC/Weak.h>
@@ -53,8 +54,9 @@ public:
TaskQueue& task_queue() { return *m_task_queue; }
TaskQueue const& task_queue() const { return *m_task_queue; }
TaskQueue& microtask_queue() { return *m_microtask_queue; }
TaskQueue const& microtask_queue() const { return *m_microtask_queue; }
bool microtask_queue_empty() const { return m_microtask_queue.is_empty(); }
void enqueue_microtask(GC::Ref<HTML::Task> task) { m_microtask_queue.enqueue(task); }
GC::Ref<HTML::Task> dequeue_microtask() { return m_microtask_queue.dequeue(); }
void spin_until(GC::Ref<GC::Function<bool()>> goal_condition);
void spin_processing_tasks_with_source_until(Task::Source, GC::Ref<GC::Function<bool()>> goal_condition);
@@ -108,7 +110,7 @@ private:
Vector<GC::Ref<GC::Function<void()>>> m_reached_step_1_tasks;
GC::Ptr<TaskQueue> m_task_queue;
GC::Ptr<TaskQueue> m_microtask_queue;
Queue<GC::Ref<HTML::Task>> m_microtask_queue;
// https://html.spec.whatwg.org/multipage/webappapis.html#currently-running-task
GC::Ptr<Task> m_currently_running_task { nullptr };