]> Piment Noir Git Repositories - poolifier.git/commitdiff
perf: avoid recursion in task queueing iterator
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 10 Jul 2025 19:57:57 +0000 (21:57 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 10 Jul 2025 19:57:57 +0000 (21:57 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/queues/abstract-fixed-queue.ts
src/queues/priority-queue.ts

index 36f178f5528219bb1903be27075f972a0edd1de0..3d77377d1b513a46b4599396b292be961a380bba 100644 (file)
@@ -130,7 +130,7 @@ export abstract class AbstractFixedQueue<T> implements IFixedQueue<T> {
     let index = this.start
     let i = 0
     return {
-      next: () => {
+      next: (): IteratorResult<T> => {
         if (i >= this.size) {
           return {
             done: true,
index 7ae8e6d8e40ceb15a5c2d3dd015e486b3cf60275..48be511c7b4c9cf06156085fb30c6271296286b2 100644 (file)
@@ -136,16 +136,13 @@ export class PriorityQueue<T> {
         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<T> {
   public [Symbol.iterator] (): Iterator<T> {
     let node: PriorityQueueNode<T> | undefined = this.tail
     let index = 0
-    const getNextValue = (): IteratorResult<T> => {
-      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<T> => {
+        // 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<T> {
       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