From 6fe6166607fc933f61838cc3710a17f008c2dd7b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 28 Dec 2023 10:24:00 +0100 Subject: [PATCH] LibWeb: Put a cap on how many image loads we'll batch up before flushing The BatchingDispatcher mechanism is used by HTMLImageElement to avoid decoding one image at a time, since interleaving decode/layout/repaint over and over takes way more time than doing many decodes followed by a single layout/repaint pair. Before this change, we didn't have a limit on how many batched loads we'd allow ourselves to queue up, which could lead to situations where more and more images kept being added to the queue, and never getting processed. This fixes the issue by putting an arbitrary limit (16) on the number of batched image loads, and then allowing the flush to happen after that instead of re-deferring processing. --- Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp index 444c72571b4..99684abcb57 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp @@ -299,8 +299,13 @@ public: void enqueue(JS::SafeFunction callback) { + // NOTE: We don't want to flush the queue on every image load, since that would be slow. + // However, we don't want to keep growing the batch forever either. + static constexpr size_t max_loads_to_batch_before_flushing = 16; + m_queue.append(move(callback)); - m_timer->restart(); + if (m_queue.size() < max_loads_to_batch_before_flushing) + m_timer->restart(); } private: