From: Jérôme Benoit Date: Sat, 15 Apr 2023 21:32:54 +0000 (+0200) Subject: perf: properly bind some methods in hot code paths X-Git-Tag: v2.4.10~2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=7254e41972596ec68a8790747b9fd4ef0def5afc;p=poolifier.git perf: properly bind some methods in hot code paths Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index ffbda872..4c78c28f 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -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() diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index 6a9cff23..2f190578 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -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 { diff --git a/src/pools/selection-strategies/worker-choice-strategy-context.ts b/src/pools/selection-strategies/worker-choice-strategy-context.ts index 5e9f7de9..2a487acb 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -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(pool, opts) + new (RoundRobinWorkerChoiceStrategy.bind(this))( + pool, + opts + ) ], [ WorkerChoiceStrategies.LESS_USED, - new LessUsedWorkerChoiceStrategy(pool, opts) + new (LessUsedWorkerChoiceStrategy.bind(this))( + pool, + opts + ) ], [ WorkerChoiceStrategies.LESS_BUSY, - new LessBusyWorkerChoiceStrategy(pool, opts) + new (LessBusyWorkerChoiceStrategy.bind(this))( + pool, + opts + ) ], [ WorkerChoiceStrategies.FAIR_SHARE, - new FairShareWorkerChoiceStrategy(pool, opts) - ], - [ - WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN, - new WeightedRoundRobinWorkerChoiceStrategy( + new (FairShareWorkerChoiceStrategy.bind(this))( pool, opts ) + ], + [ + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN, + new (WeightedRoundRobinWorkerChoiceStrategy.bind(this))< + Worker, + Data, + Response + >(pool, opts) ] ]) }