Skip to content

Commit 4f2eb8f

Browse files
committed
perf: avoid recursion in task queueing iterator
Signed-off-by: Jérôme Benoit <[email protected]>
1 parent 462f362 commit 4f2eb8f

File tree

2 files changed

+32
-34
lines changed

2 files changed

+32
-34
lines changed

src/queues/abstract-fixed-queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export abstract class AbstractFixedQueue<T> implements IFixedQueue<T> {
130130
let index = this.start
131131
let i = 0
132132
return {
133-
next: () => {
133+
next: (): IteratorResult<T> => {
134134
if (i >= this.size) {
135135
return {
136136
value: undefined,

src/queues/priority-queue.ts

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,13 @@ export class PriorityQueue<T> {
152152
targetNode = targetNode.next
153153
}
154154
}
155-
if (targetNode?.empty() === true) {
155+
if (targetNode == null || targetNode.empty()) {
156156
return undefined
157157
}
158-
const data = targetNode!.dequeue()
158+
const data = targetNode.dequeue()
159159
--this.size
160-
if (targetNode!.empty()) {
161-
this.removePriorityQueueNode(targetNode!, prev)
160+
if (targetNode.empty()) {
161+
this.removePriorityQueueNode(targetNode, prev)
162162
}
163163
return data
164164
}
@@ -181,30 +181,28 @@ export class PriorityQueue<T> {
181181
public [Symbol.iterator](): Iterator<T> {
182182
let node: PriorityQueueNode<T> | undefined = this.tail
183183
let index = 0
184-
const getNextValue = (): IteratorResult<T> => {
185-
if (node == null) {
186-
return { done: true, value: undefined }
187-
}
188-
189-
while (index >= node.size) {
190-
node = node.next
191-
index = 0
192-
if (node == null) {
193-
return { done: true, value: undefined }
194-
}
195-
}
196-
197-
const value = node.get(index)
198-
if (value == null) {
199-
++index
200-
return getNextValue()
201-
}
202-
203-
++index
204-
return { done: false, value }
205-
}
206184
return {
207-
next: getNextValue,
185+
next: (): IteratorResult<T> => {
186+
while (true) {
187+
if (node == null) {
188+
return { done: true, value: undefined }
189+
}
190+
191+
while (index >= node.size) {
192+
node = node.next
193+
index = 0
194+
if (node == null) {
195+
return { done: true, value: undefined }
196+
}
197+
}
198+
199+
const value = node.get(index)
200+
++index
201+
if (value != null) {
202+
return { done: false, value }
203+
}
204+
}
205+
},
208206
}
209207
}
210208

@@ -226,13 +224,13 @@ export class PriorityQueue<T> {
226224
return
227225
}
228226

229-
if (nodeToRemove === this.tail) {
230-
this.tail = nodeToRemove.next!
231-
} else if (nodeToRemove === this.head) {
232-
this.head = previousNode!
227+
if (nodeToRemove === this.tail && nodeToRemove.next != null) {
228+
this.tail = nodeToRemove.next
229+
} else if (nodeToRemove === this.head && previousNode != null) {
230+
this.head = previousNode
233231
this.head.next = undefined
234-
} else {
235-
previousNode!.next = nodeToRemove.next
232+
} else if (previousNode != null) {
233+
previousNode.next = nodeToRemove.next
236234
}
237235

238236
nodeToRemove.next = undefined

0 commit comments

Comments
 (0)