From: Jérôme Benoit Date: Fri, 22 Dec 2023 22:07:13 +0000 (+0100) Subject: perf: avoid branching on pool type X-Git-Tag: v3.1.10~2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=d087803425ae5b3577028029dd1d2dae67d7f63b;p=poolifier.git perf: avoid branching on pool type Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 617d0d8b..cfef89e3 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -1727,13 +1727,10 @@ export abstract class AbstractPool< } } - private checkAndEmitDynamicWorkerCreationEvents (): void { - if (this.type === PoolTypes.dynamic) { - if (this.full) { - this.emitter?.emit(PoolEvents.full, this.info) - } - } - } + /** + * Emits dynamic worker creation events. + */ + protected abstract checkAndEmitDynamicWorkerCreationEvents (): void /** * Gets the worker information given its worker node key. diff --git a/src/pools/cluster/dynamic.ts b/src/pools/cluster/dynamic.ts index ce28e421..08ebc75c 100644 --- a/src/pools/cluster/dynamic.ts +++ b/src/pools/cluster/dynamic.ts @@ -1,5 +1,5 @@ import { checkDynamicPoolSize } from '../utils' -import { type PoolType, PoolTypes } from '../pool' +import { PoolEvents, type PoolType, PoolTypes } from '../pool' import { type ClusterPoolOptions, FixedClusterPool } from './fixed' /** @@ -43,6 +43,13 @@ export class DynamicClusterPool< return !this.full && this.internalBusy() } + /** @inheritDoc */ + protected checkAndEmitDynamicWorkerCreationEvents (): void { + if (this.full) { + this.emitter?.emit(PoolEvents.full, this.info) + } + } + /** @inheritDoc */ protected get type (): PoolType { return PoolTypes.dynamic diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index b7bf9c1a..b1930f2f 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -94,6 +94,9 @@ export class FixedClusterPool< return false } + /** @inheritDoc */ + protected checkAndEmitDynamicWorkerCreationEvents (): void {} + /** @inheritDoc */ protected get type (): PoolType { return PoolTypes.fixed diff --git a/src/pools/thread/dynamic.ts b/src/pools/thread/dynamic.ts index cf317ca2..d3764c28 100644 --- a/src/pools/thread/dynamic.ts +++ b/src/pools/thread/dynamic.ts @@ -1,4 +1,4 @@ -import { type PoolType, PoolTypes } from '../pool' +import { PoolEvents, type PoolType, PoolTypes } from '../pool' import { checkDynamicPoolSize } from '../utils' import { FixedThreadPool, type ThreadPoolOptions } from './fixed' @@ -43,6 +43,13 @@ export class DynamicThreadPool< return !this.full && this.internalBusy() } + /** @inheritDoc */ + protected checkAndEmitDynamicWorkerCreationEvents (): void { + if (this.full) { + this.emitter?.emit(PoolEvents.full, this.info) + } + } + /** @inheritDoc */ protected get type (): PoolType { return PoolTypes.dynamic diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index 088d72c9..f7557a57 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -113,6 +113,9 @@ export class FixedThreadPool< return false } + /** @inheritDoc */ + protected checkAndEmitDynamicWorkerCreationEvents (): void {} + /** @inheritDoc */ protected get type (): PoolType { return PoolTypes.fixed