From: Jérôme Benoit Date: Fri, 22 Aug 2025 20:57:52 +0000 (+0200) Subject: perf: soften priority queue adaptive aging X-Git-Tag: v5.1.5~2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=6730e51eee812924b6a39eeefb694df83aee9ca3;p=poolifier.git perf: soften priority queue adaptive aging Signed-off-by: Jérôme Benoit --- diff --git a/src/queues/fixed-priority-queue.ts b/src/queues/fixed-priority-queue.ts index fa4b02574..83a44ae17 100644 --- a/src/queues/fixed-priority-queue.ts +++ b/src/queues/fixed-priority-queue.ts @@ -11,16 +11,23 @@ export class FixedPriorityQueue extends AbstractFixedQueue implements IFixedQueue { private readonly agingFactor: number + private readonly loadExponent: number /** * Constructs a FixedPriorityQueue. * @param size - Fixed queue size. @defaultValue defaultQueueSize - * @param agingFactor - Aging factor to apply to items in priority points per millisecond. A higher value makes items age faster. + * @param agingFactor - Aging factor to apply to items (priority points per millisecond). + * @param loadExponent - Load exponent applied to normalized load when computing effective aging. * @returns IFixedQueue. */ - public constructor (size?: number, agingFactor = 0.001) { + public constructor ( + size?: number, + agingFactor = 0.001, + loadExponent = 1.0 / 1.5 + ) { super(size) this.agingFactor = agingFactor + this.loadExponent = loadExponent } /** @inheritdoc */ @@ -31,18 +38,18 @@ export class FixedPriorityQueue priority = priority ?? 0 const now = performance.now() const effectiveAgingFactor = - this.agingFactor * (1 + 2 * (this.size / this.capacity)) + this.agingFactor * + (1 + ((this.size + 1) / this.capacity) ** this.loadExponent) let insertionPhysicalIndex = -1 let currentPhysicalIndex = this.start for (let i = 0; i < this.size; i++) { - const node = this.nodeArray[currentPhysicalIndex] - if (node != null) { - const nodeEffectivePriority = - node.priority - (now - node.timestamp) * effectiveAgingFactor - if (nodeEffectivePriority > priority) { - insertionPhysicalIndex = currentPhysicalIndex - break - } + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const node = this.nodeArray[currentPhysicalIndex]! + const nodeEffectivePriority = + node.priority - (now - node.timestamp) * effectiveAgingFactor + if (nodeEffectivePriority > priority) { + insertionPhysicalIndex = currentPhysicalIndex + break } ++currentPhysicalIndex if (currentPhysicalIndex === this.capacity) {