From 438cad9f017a7de9c3108c6812ccd1c9eaed7265 Mon Sep 17 00:00:00 2001 From: Laimonas Turauskas Date: Thu, 16 May 2024 13:46:53 -0400 Subject: [PATCH] Improve queue processing efficiency. (#369) --- .../formula/internal/SynchronizedUpdateQueue.kt | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/formula/src/main/java/com/instacart/formula/internal/SynchronizedUpdateQueue.kt b/formula/src/main/java/com/instacart/formula/internal/SynchronizedUpdateQueue.kt index 93aef0d2..6ce90225 100644 --- a/formula/src/main/java/com/instacart/formula/internal/SynchronizedUpdateQueue.kt +++ b/formula/src/main/java/com/instacart/formula/internal/SynchronizedUpdateQueue.kt @@ -84,8 +84,8 @@ class SynchronizedUpdateQueue( val peekUpdate = updateQueue.peek() if (peekUpdate != null) { // Since there is a pending update, we try to process it. - val updateExecuted = takeOver(this::pollAndExecute) - if (!updateExecuted) { + val executed = takeOver(this::executeQueue) + if (!executed) { return } } else { @@ -95,10 +95,12 @@ class SynchronizedUpdateQueue( } } - private fun pollAndExecute() { - // We remove first update from the queue and execute if it exists. - val actualUpdate = updateQueue.poll() - actualUpdate?.invoke() + private fun executeQueue() { + var update = updateQueue.poll() + while (update != null) { + update() + update = updateQueue.poll() + } } /**