perf: optimize backpressure task(s) stealing conditions
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 2 Sep 2024 09:35:51 +0000 (11:35 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 2 Sep 2024 09:35:51 +0000 (11:35 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts

index 1a2c8bf3125687b1e7bb834ce1bc2a067936c66c..b883eb9646b71fb9b132063b672abb6fa4a789e6 100644 (file)
@@ -265,16 +265,16 @@ export abstract class AbstractPool<
           workerNodeA.usage.tasks.queued - workerNodeB.usage.tasks.queued
       )
     for (const [workerNodeKey, workerNode] of workerNodes.entries()) {
+      if (sourceWorkerNode.usage.tasks.queued === 0) {
+        break
+      }
       if (
-        sourceWorkerNode.usage.tasks.queued > 0 &&
         workerNode.info.id !== workerId &&
+        !workerNode.info.backPressureStealing &&
         workerNode.usage.tasks.queued <
           // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
           this.opts.tasksQueueOptions!.size! - sizeOffset
       ) {
-        if (workerNode.info.backPressureStealing) {
-          continue
-        }
         workerNode.info.backPressureStealing = true
         this.stealTask(sourceWorkerNode, workerNodeKey)
         workerNode.info.backPressureStealing = false