From 66c08e6f68fbe7aeaa8da91fe8ed812e1befefc9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 22 Aug 2025 21:39:33 +0200 Subject: [PATCH] perf: add adaptive aging factor to priority queue MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/queues/fixed-priority-queue.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/queues/fixed-priority-queue.ts b/src/queues/fixed-priority-queue.ts index 509b1ad43..fa4b02574 100644 --- a/src/queues/fixed-priority-queue.ts +++ b/src/queues/fixed-priority-queue.ts @@ -30,16 +30,19 @@ export class FixedPriorityQueue } priority = priority ?? 0 const now = performance.now() + const effectiveAgingFactor = + this.agingFactor * (1 + 2 * (this.size / this.capacity)) let insertionPhysicalIndex = -1 let currentPhysicalIndex = this.start for (let i = 0; i < this.size; i++) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const node = this.nodeArray[currentPhysicalIndex]! - const nodeEffectivePriority = - node.priority - (now - node.timestamp) * this.agingFactor - if (nodeEffectivePriority > priority) { - insertionPhysicalIndex = currentPhysicalIndex - break + const node = this.nodeArray[currentPhysicalIndex] + if (node != null) { + const nodeEffectivePriority = + node.priority - (now - node.timestamp) * effectiveAgingFactor + if (nodeEffectivePriority > priority) { + insertionPhysicalIndex = currentPhysicalIndex + break + } } ++currentPhysicalIndex if (currentPhysicalIndex === this.capacity) { -- 2.43.0