From 359c1fd0234b654adb375bd8dabd9910343d59b5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 10 Jul 2025 21:57:57 +0200 Subject: [PATCH] perf: avoid recursion in task queueing iterator MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/queues/abstract-fixed-queue.ts | 2 +- src/queues/priority-queue.ts | 71 ++++++++++++++---------------- 2 files changed, 33 insertions(+), 40 deletions(-) diff --git a/src/queues/abstract-fixed-queue.ts b/src/queues/abstract-fixed-queue.ts index 36f178f55..3d77377d1 100644 --- a/src/queues/abstract-fixed-queue.ts +++ b/src/queues/abstract-fixed-queue.ts @@ -130,7 +130,7 @@ export abstract class AbstractFixedQueue implements IFixedQueue { let index = this.start let i = 0 return { - next: () => { + next: (): IteratorResult => { if (i >= this.size) { return { done: true, diff --git a/src/queues/priority-queue.ts b/src/queues/priority-queue.ts index 7ae8e6d8e..48be511c7 100644 --- a/src/queues/priority-queue.ts +++ b/src/queues/priority-queue.ts @@ -136,16 +136,13 @@ export class PriorityQueue { targetNode = targetNode.next } } - if (targetNode?.empty() === true) { + if (targetNode == null || targetNode.empty()) { return undefined } - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const data = targetNode!.dequeue() + const data = targetNode.dequeue() --this.size - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - if (targetNode!.empty()) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.removePriorityQueueNode(targetNode!, prev) + if (targetNode.empty()) { + this.removePriorityQueueNode(targetNode, prev) } return data } @@ -176,30 +173,29 @@ export class PriorityQueue { public [Symbol.iterator] (): Iterator { let node: PriorityQueueNode | undefined = this.tail let index = 0 - const getNextValue = (): IteratorResult => { - if (node == null) { - return { done: true, value: undefined } - } - - while (index >= node.size) { - node = node.next - index = 0 - if (node == null) { - return { done: true, value: undefined } - } - } - - const value = node.get(index) - if (value == null) { - ++index - return getNextValue() - } - - ++index - return { done: false, value } - } return { - next: getNextValue, + next: (): IteratorResult => { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + while (true) { + if (node == null) { + return { done: true, value: undefined } + } + + while (index >= node.size) { + node = node.next + index = 0 + if (node == null) { + return { done: true, value: undefined } + } + } + + const value = node.get(index) + ++index + if (value != null) { + return { done: false, value } + } + } + }, } } @@ -221,16 +217,13 @@ export class PriorityQueue { return } - if (nodeToRemove === this.tail) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.tail = nodeToRemove.next! - } else if (nodeToRemove === this.head) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.head = previousNode! + if (nodeToRemove === this.tail && nodeToRemove.next != null) { + this.tail = nodeToRemove.next + } else if (nodeToRemove === this.head && previousNode != null) { + this.head = previousNode this.head.next = undefined - } else { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - previousNode!.next = nodeToRemove.next + } else if (previousNode != null) { + previousNode.next = nodeToRemove.next } nodeToRemove.next = undefined -- 2.43.0