perf: improve node eligibility branching on worker choice strategies
[poolifier.git] / src / pools / selection-strategies / least-elu-worker-choice-strategy.ts
index 0f992cfaf82d17794def8c9a129dacf91075f2ec..938d2072e998eed7ac26deeb225662ad884217b8 100644 (file)
@@ -57,8 +57,7 @@ export class LeastEluWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public choose (): number | undefined {
-    const chosenWorkerNodeKey = this.leastEluNextWorkerNodeKey()
-    this.assignChosenWorkerNodeKey(chosenWorkerNodeKey)
+    this.nextWorkerNodeKey = this.leastEluNextWorkerNodeKey()
     return this.nextWorkerNodeKey
   }
 
@@ -71,15 +70,15 @@ export class LeastEluWorkerChoiceStrategy<
     let minWorkerElu = Infinity
     let chosenWorkerNodeKey: number | undefined
     for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
+      if (!this.isWorkerNodeEligible(workerNodeKey)) {
+        continue
+      }
       const workerUsage = workerNode.usage
       const workerElu = workerUsage.elu?.active?.aggregate ?? 0
-      if (this.isWorkerNodeEligible(workerNodeKey) && workerElu === 0) {
+      if (workerElu === 0) {
         chosenWorkerNodeKey = workerNodeKey
         break
-      } else if (
-        this.isWorkerNodeEligible(workerNodeKey) &&
-        workerElu < minWorkerElu
-      ) {
+      } else if (workerElu < minWorkerElu) {
         minWorkerElu = workerElu
         chosenWorkerNodeKey = workerNodeKey
       }