]> Piment Noir Git Repositories - poolifier.git/commitdiff
perf: reduce pool info property internal usage
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 20 Aug 2025 18:29:19 +0000 (20:29 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 20 Aug 2025 18:29:19 +0000 (20:29 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts
src/pools/cluster/dynamic.ts
src/pools/thread/dynamic.ts

index 65ad5e3a40ec9877b8e1a3bf87fe7fe6032c4cb6..332656a000ba7beae5d087eee5f3763c049adc9c 100644 (file)
@@ -154,18 +154,8 @@ export abstract class AbstractPool<
             accumulator + (workerNode.usage.tasks.maxQueued ?? 0),
           0
         ),
-        queuedTasks: this.workerNodes.reduce(
-          (accumulator, workerNode) =>
-            accumulator + workerNode.usage.tasks.queued,
-          0
-        ),
-        stealingWorkerNodes: this.workerNodes.reduce(
-          (accumulator, _, workerNodeKey) =>
-            this.isWorkerNodeStealing(workerNodeKey)
-              ? accumulator + 1
-              : accumulator,
-          0
-        ),
+        queuedTasks: this.getQueuedTasks(),
+        stealingWorkerNodes: this.getStealingWorkerNodes(),
         stolenTasks: this.workerNodes.reduce(
           (accumulator, workerNode) =>
             accumulator + workerNode.usage.tasks.stolen,
@@ -632,7 +622,8 @@ export abstract class AbstractPool<
       })
     )
     if (this.emitter != null) {
-      this.emitter.emit(PoolEvents.destroy, this.info)
+      this.emitter.listenerCount(PoolEvents.destroy) > 0 &&
+        this.emitter.emit(PoolEvents.destroy, this.info)
       this.emitter.emitDestroy()
       this.readyEventEmitted = false
     }
@@ -1425,13 +1416,14 @@ export abstract class AbstractPool<
       !this.started ||
       this.destroying ||
       this.workerNodes.length <= 1 ||
-      this.info.queuedTasks === 0
+      this.getQueuedTasks() === 0
     )
   }
 
   private checkAndEmitReadyEvent (): void {
     if (this.emitter != null && !this.readyEventEmitted && this.ready) {
-      this.emitter.emit(PoolEvents.ready, this.info)
+      this.emitter.listenerCount(PoolEvents.ready) > 0 &&
+        this.emitter.emit(PoolEvents.ready, this.info)
       this.readyEventEmitted = true
     }
   }
@@ -1442,21 +1434,24 @@ export abstract class AbstractPool<
       this.backPressureEventEmitted &&
       !this.backPressure
     ) {
-      this.emitter.emit(PoolEvents.backPressureEnd, this.info)
+      this.emitter.listenerCount(PoolEvents.backPressureEnd) > 0 &&
+        this.emitter.emit(PoolEvents.backPressureEnd, this.info)
       this.backPressureEventEmitted = false
     }
   }
 
   private checkAndEmitTaskExecutionEvents (): void {
     if (this.emitter != null && !this.busyEventEmitted && this.busy) {
-      this.emitter.emit(PoolEvents.busy, this.info)
+      this.emitter.listenerCount(PoolEvents.busy) > 0 &&
+        this.emitter.emit(PoolEvents.busy, this.info)
       this.busyEventEmitted = true
     }
   }
 
   private checkAndEmitTaskExecutionFinishedEvents (): void {
     if (this.emitter != null && this.busyEventEmitted && !this.busy) {
-      this.emitter.emit(PoolEvents.busyEnd, this.info)
+      this.emitter.listenerCount(PoolEvents.busyEnd) > 0 &&
+        this.emitter.emit(PoolEvents.busyEnd, this.info)
       this.busyEventEmitted = false
     }
   }
@@ -1467,7 +1462,8 @@ export abstract class AbstractPool<
       !this.backPressureEventEmitted &&
       this.backPressure
     ) {
-      this.emitter.emit(PoolEvents.backPressure, this.info)
+      this.emitter.listenerCount(PoolEvents.backPressure) > 0 &&
+        this.emitter.emit(PoolEvents.backPressure, this.info)
       this.backPressureEventEmitted = true
     }
   }
@@ -1670,6 +1666,22 @@ export abstract class AbstractPool<
         : new Error(`Task '${taskName}' id '${taskId}' aborted`)
   }
 
+  private getQueuedTasks (): number {
+    return this.workerNodes.reduce((accumulator, workerNode) => {
+      return accumulator + workerNode.usage.tasks.queued
+    }, 0)
+  }
+
+  private getStealingWorkerNodes (): number {
+    return this.workerNodes.reduce(
+      (accumulator, _, workerNodeKey) =>
+        this.isWorkerNodeStealing(workerNodeKey)
+          ? accumulator + 1
+          : accumulator,
+      0
+    )
+  }
+
   /**
    * Gets task function worker choice strategy, if any.
    * @param name - The task function name.
@@ -2061,7 +2073,7 @@ export abstract class AbstractPool<
   private readonly isStealingRatioReached = (): boolean => {
     return (
       this.opts.tasksQueueOptions?.tasksStealingRatio === 0 ||
-      (this.info.stealingWorkerNodes ?? 0) >
+      this.getStealingWorkerNodes() >
         Math.ceil(
           this.workerNodes.length *
             // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -2308,6 +2320,9 @@ export abstract class AbstractPool<
     message: MessageValue<Data>
   ): Promise<boolean> {
     const targetWorkerNodeKeys = [...this.workerNodes.keys()]
+    if (targetWorkerNodeKeys.length === 0) {
+      return true
+    }
     const responsesReceived: MessageValue<Response>[] = []
     const taskFunctionOperationsListener = (
       message: MessageValue<Response>,
index 1d2900677c15e2cf94c5a3d0188aa93730aeb522..48d83e4d2288383bea84af7abd3506643a264687 100644 (file)
@@ -89,7 +89,8 @@ export class DynamicClusterPool<
   protected override checkAndEmitDynamicWorkerCreationEvents (): void {
     if (this.emitter != null) {
       if (!this.fullEventEmitted && this.full) {
-        this.emitter.emit(PoolEvents.full, this.info)
+        this.emitter.listenerCount(PoolEvents.full) > 0 &&
+          this.emitter.emit(PoolEvents.full, this.info)
         this.fullEventEmitted = true
       }
       if (this.emptyEventEmitted && !this.empty) {
@@ -102,11 +103,13 @@ export class DynamicClusterPool<
   protected override checkAndEmitDynamicWorkerDestructionEvents (): void {
     if (this.emitter != null) {
       if (this.fullEventEmitted && !this.full) {
-        this.emitter.emit(PoolEvents.fullEnd, this.info)
+        this.emitter.listenerCount(PoolEvents.fullEnd) > 0 &&
+          this.emitter.emit(PoolEvents.fullEnd, this.info)
         this.fullEventEmitted = false
       }
       if (!this.emptyEventEmitted && this.empty) {
-        this.emitter.emit(PoolEvents.empty, this.info)
+        this.emitter.listenerCount(PoolEvents.empty) > 0 &&
+          this.emitter.emit(PoolEvents.empty, this.info)
         this.emptyEventEmitted = true
       }
     }
index 32beca8e6bcc3f54713e30515995dd10e00a5a98..17d759ea9c28ee4b6a624923904c2251baadafa4 100644 (file)
@@ -89,7 +89,8 @@ export class DynamicThreadPool<
   protected override checkAndEmitDynamicWorkerCreationEvents (): void {
     if (this.emitter != null) {
       if (!this.fullEventEmitted && this.full) {
-        this.emitter.emit(PoolEvents.full, this.info)
+        this.emitter.listenerCount(PoolEvents.full) > 0 &&
+          this.emitter.emit(PoolEvents.full, this.info)
         this.fullEventEmitted = true
       }
       if (this.emptyEventEmitted && !this.empty) {
@@ -102,11 +103,13 @@ export class DynamicThreadPool<
   protected override checkAndEmitDynamicWorkerDestructionEvents (): void {
     if (this.emitter != null) {
       if (this.fullEventEmitted && !this.full) {
-        this.emitter.emit(PoolEvents.fullEnd, this.info)
+        this.emitter.listenerCount(PoolEvents.fullEnd) > 0 &&
+          this.emitter.emit(PoolEvents.fullEnd, this.info)
         this.fullEventEmitted = false
       }
       if (!this.emptyEventEmitted && this.empty) {
-        this.emitter.emit(PoolEvents.empty, this.info)
+        this.emitter.listenerCount(PoolEvents.empty) > 0 &&
+          this.emitter.emit(PoolEvents.empty, this.info)
         this.emptyEventEmitted = true
       }
     }