perf: optimize runtime worker choice strategy change
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 9 Apr 2023 17:02:53 +0000 (19:02 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 9 Apr 2023 17:02:53 +0000 (19:02 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts

index f4f3e8337056a1cb47edba30e0b4982ef339f5e0..41ce6aa019a5269a0a47e338934c093aa2a3ee6d 100644 (file)
@@ -191,21 +191,16 @@ export abstract class AbstractPool<
   ): void {
     this.checkValidWorkerChoiceStrategy(workerChoiceStrategy)
     this.opts.workerChoiceStrategy = workerChoiceStrategy
-    for (const [index, workerNode] of this.workerNodes.entries()) {
-      this.setWorkerNode(
-        index,
-        workerNode.worker,
-        {
-          run: 0,
-          running: 0,
-          runTime: 0,
-          runTimeHistory: new CircularArray(),
-          avgRunTime: 0,
-          medRunTime: 0,
-          error: 0
-        },
-        workerNode.tasksQueue
-      )
+    for (const workerNode of this.workerNodes) {
+      this.setWorkerNodeTasksUsage(workerNode, {
+        run: 0,
+        running: 0,
+        runTime: 0,
+        runTimeHistory: new CircularArray(),
+        avgRunTime: 0,
+        medRunTime: 0,
+        error: 0
+      })
     }
     this.workerChoiceStrategyContext.setWorkerChoiceStrategy(
       workerChoiceStrategy
@@ -482,6 +477,19 @@ export abstract class AbstractPool<
     }
   }
 
+  /**
+   * Sets the given worker node its tasks usage in the pool.
+   *
+   * @param workerNode - The worker node.
+   * @param tasksUsage - The worker node tasks usage.
+   */
+  private setWorkerNodeTasksUsage (
+    workerNode: WorkerNode<Worker, Data>,
+    tasksUsage: TasksUsage
+  ): void {
+    workerNode.tasksUsage = tasksUsage
+  }
+
   /**
    * Gets the given worker its tasks usage in the pool.
    *