perf: properly bind some methods in hot code paths
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 15 Apr 2023 21:32:54 +0000 (23:32 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 15 Apr 2023 21:32:54 +0000 (23:32 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts
src/pools/selection-strategies/abstract-worker-choice-strategy.ts
src/pools/selection-strategies/worker-choice-strategy-context.ts

index ffbda872af76d22fd7d037b39dd54025568e39c5..4c78c28f1479f7988e10a9096710e1ec2e94c7cb 100644 (file)
@@ -84,10 +84,10 @@ export abstract class AbstractPool<
     this.checkFilePath(this.filePath)
     this.checkPoolOptions(this.opts)
 
-    this.chooseWorkerNode.bind(this)
-    this.executeTask.bind(this)
-    this.enqueueTask.bind(this)
-    this.checkAndEmitEvents.bind(this)
+    this.chooseWorkerNode = this.chooseWorkerNode.bind(this)
+    this.executeTask = this.executeTask.bind(this)
+    this.enqueueTask = this.enqueueTask.bind(this)
+    this.checkAndEmitEvents = this.checkAndEmitEvents.bind(this)
 
     this.setupHook()
 
index 6a9cff236a2def16eee80786e869e795d774f586..2f1905782f7c4a6c3d6d4c98053b3965eaa8021d 100644 (file)
@@ -39,7 +39,7 @@ export abstract class AbstractWorkerChoiceStrategy<
     protected opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
   ) {
     this.isDynamicPool = this.pool.type === PoolType.DYNAMIC
-    this.choose.bind(this)
+    this.choose = this.choose.bind(this)
   }
 
   protected checkOptions (opts: WorkerChoiceStrategyOptions): void {
index 5e9f7de9dce24e1510e52d68c09bfb6486df1f52..2a487acbdfde0f2d07c2f9eae9233d26da387604 100644 (file)
@@ -43,33 +43,46 @@ export class WorkerChoiceStrategyContext<
     private workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN,
     opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
   ) {
-    this.execute.bind(this)
+    this.execute = this.execute.bind(this)
     this.workerChoiceStrategies = new Map<
     WorkerChoiceStrategy,
     IWorkerChoiceStrategy
     >([
       [
         WorkerChoiceStrategies.ROUND_ROBIN,
-        new RoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool, opts)
+        new (RoundRobinWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
+          pool,
+          opts
+        )
       ],
       [
         WorkerChoiceStrategies.LESS_USED,
-        new LessUsedWorkerChoiceStrategy<Worker, Data, Response>(pool, opts)
+        new (LessUsedWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
+          pool,
+          opts
+        )
       ],
       [
         WorkerChoiceStrategies.LESS_BUSY,
-        new LessBusyWorkerChoiceStrategy<Worker, Data, Response>(pool, opts)
+        new (LessBusyWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
+          pool,
+          opts
+        )
       ],
       [
         WorkerChoiceStrategies.FAIR_SHARE,
-        new FairShareWorkerChoiceStrategy<Worker, Data, Response>(pool, opts)
-      ],
-      [
-        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN,
-        new WeightedRoundRobinWorkerChoiceStrategy<Worker, Data, Response>(
+        new (FairShareWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
           pool,
           opts
         )
+      ],
+      [
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN,
+        new (WeightedRoundRobinWorkerChoiceStrategy.bind(this))<
+        Worker,
+        Data,
+        Response
+        >(pool, opts)
       ]
     ])
   }