Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f05bad9

Browse filesBrowse files
ronagaduh95
authored andcommitted
lib: recycle queues
Avoid creating unnecessary garbage in the old space. PR-URL: #61461 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent fd8be14 commit f05bad9
Copy full SHA for f05bad9

1 file changed

+10-2Lines changed: 10 additions & 2 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/fixed_queue.js‎

Copy file name to clipboardExpand all lines: lib/internal/fixed_queue.js
+10-2Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ class FixedCircularBuffer {
8989
}
9090

9191
module.exports = class FixedQueue {
92+
static #pool = [];
93+
9294
constructor() {
93-
this.head = this.tail = new FixedCircularBuffer();
95+
this.head = this.tail = FixedQueue.#pool.pop() ?? new FixedCircularBuffer();
9496
}
9597

9698
isEmpty() {
@@ -101,7 +103,7 @@ module.exports = class FixedQueue {
101103
if (this.head.isFull()) {
102104
// Head is full: Creates a new queue, sets the old queue's `.next` to it,
103105
// 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();
105107
}
106108
this.head.push(data);
107109
}
@@ -113,6 +115,12 @@ module.exports = class FixedQueue {
113115
// If there is another queue, it forms the new tail.
114116
this.tail = tail.next;
115117
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+
}
116124
}
117125
return next;
118126
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.