From 9d9fb7b64a28d150583fe1bcd895893f6736d9d4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 22 Dec 2023 22:30:32 +0100 Subject: [PATCH] perf: avoid branching on pool type MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/circular-array.ts | 1 + src/pools/abstract-pool.ts | 4 +--- src/pools/cluster/dynamic.ts | 5 +++++ src/pools/cluster/fixed.ts | 5 +++++ src/pools/thread/dynamic.ts | 5 +++++ src/pools/thread/fixed.ts | 5 +++++ src/pools/utils.ts | 1 - 7 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/circular-array.ts b/src/circular-array.ts index 8f545179..cc58d8ac 100644 --- a/src/circular-array.ts +++ b/src/circular-array.ts @@ -5,6 +5,7 @@ export const DEFAULT_CIRCULAR_ARRAY_SIZE = 1024 /** * Array with a maximum length and shifting items when full. * + * @typeParam T - Type of items. * @internal */ export class CircularArray extends Array { diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 9045b063..617d0d8b 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -1178,9 +1178,7 @@ export abstract class AbstractPool< * * @returns Whether to create a dynamic worker or not. */ - private shallCreateDynamicWorker (): boolean { - return this.type === PoolTypes.dynamic && !this.full && this.internalBusy() - } + protected abstract shallCreateDynamicWorker (): boolean /** * Sends a message to worker given its worker node key. diff --git a/src/pools/cluster/dynamic.ts b/src/pools/cluster/dynamic.ts index 9636b423..ce28e421 100644 --- a/src/pools/cluster/dynamic.ts +++ b/src/pools/cluster/dynamic.ts @@ -38,6 +38,11 @@ export class DynamicClusterPool< ) } + /** @inheritDoc */ + protected shallCreateDynamicWorker (): boolean { + return !this.full && this.internalBusy() + } + /** @inheritDoc */ protected get type (): PoolType { return PoolTypes.dynamic diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index 545238b0..b7bf9c1a 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -89,6 +89,11 @@ export class FixedClusterPool< this.workerNodes[workerNodeKey].worker.off('message', listener) } + /** @inheritDoc */ + protected shallCreateDynamicWorker (): boolean { + return false + } + /** @inheritDoc */ protected get type (): PoolType { return PoolTypes.fixed diff --git a/src/pools/thread/dynamic.ts b/src/pools/thread/dynamic.ts index cd9d2325..cf317ca2 100644 --- a/src/pools/thread/dynamic.ts +++ b/src/pools/thread/dynamic.ts @@ -38,6 +38,11 @@ export class DynamicThreadPool< ) } + /** @inheritDoc */ + protected shallCreateDynamicWorker (): boolean { + return !this.full && this.internalBusy() + } + /** @inheritDoc */ protected get type (): PoolType { return PoolTypes.dynamic diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index 13df2b5c..088d72c9 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -108,6 +108,11 @@ export class FixedThreadPool< ) } + /** @inheritDoc */ + protected shallCreateDynamicWorker (): boolean { + return false + } + /** @inheritDoc */ protected get type (): PoolType { return PoolTypes.fixed diff --git a/src/pools/utils.ts b/src/pools/utils.ts index c7ab2d1e..619e2061 100644 --- a/src/pools/utils.ts +++ b/src/pools/utils.ts @@ -164,7 +164,6 @@ export const checkWorkerNodeArguments = ( * @param measurementStatistics - The measurement statistics to update. * @param measurementRequirements - The measurement statistics requirements. * @param measurementValue - The measurement value. - * @param numberOfMeasurements - The number of measurements. * @internal */ const updateMeasurementStatistics = ( -- 2.34.1