perf: optimize tasks stealing in corner case
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sat, 2 Dec 2023 20:49:12 +0000 (21:49 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sat, 2 Dec 2023 20:49:12 +0000 (21:49 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
CHANGELOG.md
src/pools/abstract-pool.ts

index a17a4ed5a41e86ad3c90179a7f23130f2a96361a..41e0f41199c0fbe53710afa116617f6059712ab8 100644 (file)
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+### Changed
+
+- Add a fastpath when tasks stealing or redistribution is impossible
+
 ## [3.0.9] - 2023-11-26
 
 ### Fixed
index 3da4c70fa41e0cf5168dfccbe517d2ae626f8542..7ea9379665050a982259a4181c934a7eecc81adb 100644 (file)
@@ -1445,6 +1445,9 @@ export abstract class AbstractPool<
   }
 
   private redistributeQueuedTasks (workerNodeKey: number): void {
+    if (this.workerNodes.length <= 1) {
+      return
+    }
     while (this.tasksQueueSize(workerNodeKey) > 0) {
       const destinationWorkerNodeKey = this.workerNodes.reduce(
         (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => {
@@ -1538,6 +1541,9 @@ export abstract class AbstractPool<
     eventDetail: WorkerNodeEventDetail,
     previousStolenTask?: Task<Data>
   ): void => {
+    if (this.workerNodes.length <= 1) {
+      return
+    }
     const { workerNodeKey } = eventDetail
     if (workerNodeKey == null) {
       throw new Error(
@@ -1629,6 +1635,9 @@ export abstract class AbstractPool<
   private readonly handleBackPressureEvent = (
     eventDetail: WorkerNodeEventDetail
   ): void => {
+    if (this.workerNodes.length <= 1) {
+      return
+    }
     const { workerId } = eventDetail
     const sizeOffset = 1
     if ((this.opts.tasksQueueOptions?.size as number) <= sizeOffset) {