GeezerVison is showing github.com — lib: recycle queues · nodejs/node@830fff0
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/internal/fixed_queue.js +10-2Lines changed: 10 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -89,8 +89,10 @@ class FixedCircularBuffer { |
89 | 89 | } |
90 | 90 |
|
91 | 91 | module.exports = class FixedQueue { |
| 92 | + static #pool = []; |
| 93 | + |
92 | 94 | constructor() { |
93 | | - this.head = this.tail = new FixedCircularBuffer(); |
| 95 | + this.head = this.tail = FixedQueue.#pool.pop() ?? new FixedCircularBuffer(); |
94 | 96 | } |
95 | 97 |
|
96 | 98 | isEmpty() { |
@@ -101,7 +103,7 @@ module.exports = class FixedQueue { |
101 | 103 | if (this.head.isFull()) { |
102 | 104 | // Head is full: Creates a new queue, sets the old queue's `.next` to it, |
103 | 105 | // and sets it as the new main queue. |
104 | | - this.head = this.head.next = new FixedCircularBuffer(); |
| 106 | + this.head = this.head.next = FixedQueue.#pool.pop() ?? new FixedCircularBuffer(); |
105 | 107 | } |
106 | 108 | this.head.push(data); |
107 | 109 | } |
@@ -113,6 +115,12 @@ module.exports = class FixedQueue { |
113 | 115 | // If there is another queue, it forms the new tail. |
114 | 116 | this.tail = tail.next; |
115 | 117 | tail.next = null; |
| 118 | + tail.bottom = 0; |
| 119 | + tail.top = 0; |
| 120 | + |
| 121 | + if (FixedQueue.#pool.length < 64) { |
| 122 | + FixedQueue.#pool.push(tail); // Recycle old tail |
| 123 | + } |
116 | 124 | } |
117 | 125 | return next; |
118 | 126 | } |
|