Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: reduce memory allocation for arrays #44474

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions benchmark/process/next-tick-loop.js
@@ -0,0 +1,30 @@
'use strict';
const common = require('../common.js');
const bench = common.createBenchmark(main, {
n: [1e4, 2e4, 4e4],
loop: [1e3],
});

function main({ n, loop }) {
bench.start();
run();
function run() {
let j = 0;

function cb() {
j++;
if (j === n) {
loop--;
if (loop === 0) {
bench.end(n);
} else {
run();
}
}
}

for (let i = 0; i < n; i++) {
process.nextTick(cb);
}
}
}
28 changes: 18 additions & 10 deletions lib/internal/fixed_queue.js
Expand Up @@ -78,40 +78,48 @@ class FixedCircularBuffer {
}

shift() {
const nextItem = this.list[this.bottom];
if (nextItem === undefined)
if (this.isEmpty())
return null;
const data = this.list[this.bottom];
this.list[this.bottom] = undefined;
this.bottom = (this.bottom + 1) & kMask;
return nextItem;
return data;
}
}

module.exports = class FixedQueue {
constructor() {
this.head = this.tail = new FixedCircularBuffer();
this.head = this.tail = this.buffer = new FixedCircularBuffer();
}

isEmpty() {
return this.head.isEmpty();
}

push(data) {
if (this.head.isFull()) {
// Head is full: Creates a new queue, sets the old queue's `.next` to it,
// and sets it as the new main queue.
this.head = this.head.next = new FixedCircularBuffer();
const head = this.head;
if (head.isFull()) {
// Head is full: Reuses the empty tail or creates a new queue,
// sets the old queue's `.next` to it, and sets it as the new main queue.
const buffer = this.buffer;
if (buffer.isEmpty()) {
this.buffer = buffer.next;
buffer.next = null;
this.head = head.next = buffer;
} else {
this.head = head.next = new FixedCircularBuffer();
}
}
this.head.push(data);
}

shift() {
const tail = this.tail;
const next = tail.shift();
const data = tail.shift();
if (tail.isEmpty() && tail.next !== null) {
// If there is another queue, it forms the new tail.
this.tail = tail.next;
}
return next;
return data;
}
};
9 changes: 8 additions & 1 deletion test/parallel/test-fixed-queue.js
Expand Up @@ -13,7 +13,11 @@ const FixedQueue = require('internal/fixed_queue');
queue.push('a');
assert(!queue.isEmpty());
assert.strictEqual(queue.shift(), 'a');
assert.strictEqual(queue.head, queue.tail);
assert.strictEqual(queue.head.next, null);
assert.strictEqual(queue.shift(), null);
assert.strictEqual(queue.head, queue.tail);
assert.strictEqual(queue.head.next, null);
}

{
Expand All @@ -23,11 +27,14 @@ const FixedQueue = require('internal/fixed_queue');
assert(queue.head.isFull());
queue.push('a');
assert(!queue.head.isFull());

assert.notStrictEqual(queue.head, queue.tail);
assert.strictEqual(queue.head, queue.tail.next);
assert.strictEqual(queue.head.next, null);

for (let i = 0; i < 2047; i++)
assert.strictEqual(queue.shift(), 'a');
assert.strictEqual(queue.head, queue.tail);
assert.strictEqual(queue.head.next, null);
assert(!queue.isEmpty());
assert.strictEqual(queue.shift(), 'a');
assert(queue.isEmpty());
Expand Down