perf: improve node eligibility branching on worker choice strategies
[poolifier.git] / src / pools / selection-strategies / least-busy-worker-choice-strategy.ts
index 224ca540130f49c37e6b28d5de471e5237dddb14..338ae379d2663e3777366353691bdc5ab8639592 100644 (file)
@@ -61,8 +61,7 @@ export class LeastBusyWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public choose (): number | undefined {
-    const chosenWorkerNodeKey = this.leastBusyNextWorkerNodeKey()
-    this.assignChosenWorkerNodeKey(chosenWorkerNodeKey)
+    this.nextWorkerNodeKey = this.leastBusyNextWorkerNodeKey()
     return this.nextWorkerNodeKey
   }
 
@@ -75,16 +74,16 @@ export class LeastBusyWorkerChoiceStrategy<
     let minTime = Infinity
     let chosenWorkerNodeKey: number | undefined
     for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
+      if (!this.isWorkerNodeEligible(workerNodeKey)) {
+        continue
+      }
       const workerTime =
         (workerNode.usage.runTime?.aggregate ?? 0) +
         (workerNode.usage.waitTime?.aggregate ?? 0)
-      if (this.isWorkerNodeEligible(workerNodeKey) && workerTime === 0) {
+      if (workerTime === 0) {
         chosenWorkerNodeKey = workerNodeKey
         break
-      } else if (
-        this.isWorkerNodeEligible(workerNodeKey) &&
-        workerTime < minTime
-      ) {
+      } else if (workerTime < minTime) {
         minTime = workerTime
         chosenWorkerNodeKey = workerNodeKey
       }