perf: optimize task(s) stealing implementation
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 3 Sep 2023 19:31:58 +0000 (21:31 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 3 Sep 2023 19:31:58 +0000 (21:31 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts
src/pools/selection-strategies/fair-share-worker-choice-strategy.ts

index 6668361f9491a7cf927fba8734a11c37781a3b87..aae083953c526c16f7bcaa92bddd3bf042423d3e 100644 (file)
@@ -1201,20 +1201,18 @@ export abstract class AbstractPool<
 
   private redistributeQueuedTasks (workerNodeKey: number): void {
     while (this.tasksQueueSize(workerNodeKey) > 0) {
-      let destinationWorkerNodeKey!: number
-      let minQueuedTasks = Infinity
-      for (const [workerNodeId, workerNode] of this.workerNodes.entries()) {
-        if (workerNode.info.ready && workerNodeId !== workerNodeKey) {
-          if (workerNode.usage.tasks.queued === 0) {
-            destinationWorkerNodeKey = workerNodeId
-            break
+      const destinationWorkerNodeKey = this.workerNodes.reduce(
+        (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => {
+          if (!workerNode.info.ready) {
+            return minWorkerNodeKey
           }
-          if (workerNode.usage.tasks.queued < minQueuedTasks) {
-            minQueuedTasks = workerNode.usage.tasks.queued
-            destinationWorkerNodeKey = workerNodeId
-          }
-        }
-      }
+          return workerNode.usage.tasks.queued <
+            workerNodes[minWorkerNodeKey].usage.tasks.queued
+            ? workerNodeKey
+            : minWorkerNodeKey
+        },
+        0
+      )
       if (destinationWorkerNodeKey != null) {
         const destinationWorkerNode = this.workerNodes[destinationWorkerNodeKey]
         const task = {
@@ -1258,30 +1256,26 @@ export abstract class AbstractPool<
         (workerNodeA, workerNodeB) =>
           workerNodeB.usage.tasks.queued - workerNodeA.usage.tasks.queued
       )
-    for (const sourceWorkerNode of workerNodes) {
-      if (sourceWorkerNode.usage.tasks.queued === 0) {
-        break
+    const sourceWorkerNode = workerNodes.find(
+      (workerNode) =>
+        workerNode.info.ready &&
+        workerNode.info.id !== workerId &&
+        workerNode.usage.tasks.queued > 0
+    )
+    if (sourceWorkerNode != null) {
+      const task = {
+        ...(sourceWorkerNode.popTask() as Task<Data>),
+        workerId: destinationWorkerNode.info.id as number
       }
-      if (
-        sourceWorkerNode.info.ready &&
-        sourceWorkerNode.info.id !== workerId &&
-        sourceWorkerNode.usage.tasks.queued > 0
-      ) {
-        const task = {
-          ...(sourceWorkerNode.popTask() as Task<Data>),
-          workerId: destinationWorkerNode.info.id as number
-        }
-        if (this.shallExecuteTask(destinationWorkerNodeKey)) {
-          this.executeTask(destinationWorkerNodeKey, task)
-        } else {
-          this.enqueueTask(destinationWorkerNodeKey, task)
-        }
-        this.updateTaskStolenStatisticsWorkerUsage(
-          destinationWorkerNodeKey,
-          task.name as string
-        )
-        break
+      if (this.shallExecuteTask(destinationWorkerNodeKey)) {
+        this.executeTask(destinationWorkerNodeKey, task)
+      } else {
+        this.enqueueTask(destinationWorkerNodeKey, task)
       }
+      this.updateTaskStolenStatisticsWorkerUsage(
+        destinationWorkerNodeKey,
+        task.name as string
+      )
     }
   }
 
index af3fcfa00ce06fdb369f7372e8154016a504efce..a337278ba9ce75a074c1b32d751d8f928ec86496 100644 (file)
@@ -128,12 +128,12 @@ export class FairShareWorkerChoiceStrategy<
   private getWorkerNodeVirtualTaskStartTimestamp (
     workerNodeKey: number
   ): number {
+    const virtualTaskEndTimestamp =
+      this.pool.workerNodes[workerNodeKey]?.strategyData
+        ?.virtualTaskEndTimestamp
     const now = performance.now()
-    return now <
-      (this.pool.workerNodes[workerNodeKey]?.strategyData
-        ?.virtualTaskEndTimestamp ?? -Infinity)
-      ? (this.pool.workerNodes[workerNodeKey]?.strategyData
-          ?.virtualTaskEndTimestamp as number)
+    return now < (virtualTaskEndTimestamp ?? -Infinity)
+      ? (virtualTaskEndTimestamp as number)
       : now
   }
 }