perf: reduce useless branching at pool info building
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 28 Aug 2024 10:29:39 +0000 (12:29 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 28 Aug 2024 10:29:39 +0000 (12:29 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts

index 35ac4a0f337d0859216f6082cec3deb2776d5a71..a0c779c2aac7d933888a542cda46c09d157fc8ab 100644 (file)
@@ -2044,9 +2044,6 @@ export abstract class AbstractPool<
       minSize: this.minimumNumberOfWorkers,
       ready: this.ready,
       started: this.started,
-      ...(this.opts.enableTasksQueue === true && {
-        backPressure: this.backPressure,
-      }),
       strategyRetries: this.workerChoiceStrategiesContext?.retriesCount ?? 0,
       type: this.type,
       version,
@@ -2062,6 +2059,21 @@ export abstract class AbstractPool<
           this.isWorkerNodeBusy(workerNodeKey) ? accumulator + 1 : accumulator,
         0
       ),
+      executedTasks: this.workerNodes.reduce(
+        (accumulator, workerNode) =>
+          accumulator + workerNode.usage.tasks.executed,
+        0
+      ),
+      executingTasks: this.workerNodes.reduce(
+        (accumulator, workerNode) =>
+          accumulator + workerNode.usage.tasks.executing,
+        0
+      ),
+      failedTasks: this.workerNodes.reduce(
+        (accumulator, workerNode) =>
+          accumulator + workerNode.usage.tasks.failed,
+        0
+      ),
       idleWorkerNodes: this.workerNodes.reduce(
         (accumulator, _, workerNodeKey) =>
           this.isWorkerNodeIdle(workerNodeKey) ? accumulator + 1 : accumulator,
@@ -2069,16 +2081,7 @@ export abstract class AbstractPool<
       ),
       workerNodes: this.workerNodes.length,
       ...(this.opts.enableTasksQueue === true && {
-        stealingWorkerNodes: this.workerNodes.reduce(
-          (accumulator, workerNode) =>
-            workerNode.info.continuousStealing ||
-            workerNode.info.backPressureStealing
-              ? accumulator + 1
-              : accumulator,
-          0
-        ),
-      }),
-      ...(this.opts.enableTasksQueue === true && {
+        backPressure: this.backPressure,
         backPressureWorkerNodes: this.workerNodes.reduce(
           (accumulator, _, workerNodeKey) =>
             this.isWorkerNodeBackPressured(workerNodeKey)
@@ -2086,43 +2089,30 @@ export abstract class AbstractPool<
               : accumulator,
           0
         ),
-      }),
-      executedTasks: this.workerNodes.reduce(
-        (accumulator, workerNode) =>
-          accumulator + workerNode.usage.tasks.executed,
-        0
-      ),
-      executingTasks: this.workerNodes.reduce(
-        (accumulator, workerNode) =>
-          accumulator + workerNode.usage.tasks.executing,
-        0
-      ),
-      ...(this.opts.enableTasksQueue === true && {
+        maxQueuedTasks: this.workerNodes.reduce(
+          (accumulator, workerNode) =>
+            accumulator + (workerNode.usage.tasks.maxQueued ?? 0),
+          0
+        ),
         queuedTasks: this.workerNodes.reduce(
           (accumulator, workerNode) =>
             accumulator + workerNode.usage.tasks.queued,
           0
         ),
-      }),
-      ...(this.opts.enableTasksQueue === true && {
-        maxQueuedTasks: this.workerNodes.reduce(
+        stealingWorkerNodes: this.workerNodes.reduce(
           (accumulator, workerNode) =>
-            accumulator + (workerNode.usage.tasks.maxQueued ?? 0),
+            workerNode.info.continuousStealing ||
+            workerNode.info.backPressureStealing
+              ? accumulator + 1
+              : accumulator,
           0
         ),
-      }),
-      ...(this.opts.enableTasksQueue === true && {
         stolenTasks: this.workerNodes.reduce(
           (accumulator, workerNode) =>
             accumulator + workerNode.usage.tasks.stolen,
           0
         ),
       }),
-      failedTasks: this.workerNodes.reduce(
-        (accumulator, workerNode) =>
-          accumulator + workerNode.usage.tasks.failed,
-        0
-      ),
       ...(this.workerChoiceStrategiesContext?.getTaskStatisticsRequirements()
         .runTime.aggregate === true && {
         runTime: {