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

process: faster next tick #18617

Closed
wants to merge 2 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions benchmark/streams/pipe-object-mode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const common = require('../common');
const { Readable, Writable } = require('stream');

const bench = common.createBenchmark(main, {
n: [5e6]
});

function main({ n }) {
const b = {};
const r = new Readable({ objectMode: true });
const w = new Writable({ objectMode: true });

var i = 0;

r._read = () => r.push(i++ === n ? null : b);
w._write = (data, enc, cb) => cb();

bench.start();

r.pipe(w);
w.on('finish', () => bench.end(n));
}
24 changes: 24 additions & 0 deletions benchmark/streams/pipe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const common = require('../common');
const { Readable, Writable } = require('stream');

const bench = common.createBenchmark(main, {
n: [5e6]
});

function main({ n }) {
const b = new Buffer(1024);
const r = new Readable();
const w = new Writable();

var i = 0;

r._read = () => r.push(i++ === n ? null : b);
w._write = (data, enc, cb) => cb();

bench.start();

r.pipe(w);
w.on('finish', () => bench.end(n));
}
76 changes: 49 additions & 27 deletions lib/internal/process/next_tick.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,55 @@ function setupNextTick() {
const kHasScheduled = 0;
const kHasPromiseRejections = 1;

const nextTickQueue = {
head: null,
tail: null,
// Queue size for each tick array. Must be a factor of two.
const kQueueSize = 2048;
const kQueueMask = kQueueSize - 1;

class FixedQueue {
constructor() {
this.bottom = 0;
this.top = 0;
this.list = new Array(kQueueSize);
this.next = null;
}

push(data) {
const entry = { data, next: null };
if (this.tail !== null) {
this.tail.next = entry;
} else {
this.head = entry;
tickInfo[kHasScheduled] = 1;
}
this.tail = entry;
},
this.list[this.top] = data;
this.top = (this.top + 1) & kQueueMask;
}

shift() {
if (this.head === null)
return;
const ret = this.head.data;
if (this.head === this.tail) {
this.head = this.tail = null;
const next = this.list[this.bottom];
if (next === undefined) return null;
this.list[this.bottom] = undefined;
this.bottom = (this.bottom + 1) & kQueueMask;
return next;
}
}

var head = new FixedQueue();
var tail = head;

function push(data) {
if (head.bottom === head.top) {
if (head.list[head.top] !== undefined)
head = head.next = new FixedQueue();
else
tickInfo[kHasScheduled] = 1;
}
head.push(data);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apapirovski was able to simplify the tickInfo stuff quite a bit :)

}

function shift() {
const next = tail.shift();
if (tail.top === tail.bottom) {
if (tail.next)
tail = tail.next;
else
tickInfo[kHasScheduled] = 0;
} else {
this.head = this.head.next;
}
return ret;
}
};
return next;
}

process.nextTick = nextTick;
// Needs to be accessible from beyond this scope.
Expand All @@ -69,7 +92,7 @@ function setupNextTick() {
function _tickCallback() {
let tock;
do {
while (tock = nextTickQueue.shift()) {
while (tock = shift()) {
const asyncId = tock[async_id_symbol];
emitBefore(asyncId, tock[trigger_async_id_symbol]);
// emitDestroy() places the async_id_symbol into an asynchronous queue
Expand All @@ -93,7 +116,7 @@ function setupNextTick() {
emitAfter(asyncId);
}
runMicrotasks();
} while (nextTickQueue.head !== null || emitPromiseRejectionWarnings());
} while (head.top !== head.bottom || emitPromiseRejectionWarnings());
tickInfo[kHasPromiseRejections] = 0;
}

Expand Down Expand Up @@ -139,8 +162,7 @@ function setupNextTick() {
args[i - 1] = arguments[i];
}

nextTickQueue.push(new TickObject(callback, args,
getDefaultTriggerAsyncId()));
push(new TickObject(callback, args, getDefaultTriggerAsyncId()));
}

// `internalNextTick()` will not enqueue any callback when the process is
Expand Down Expand Up @@ -168,6 +190,6 @@ function setupNextTick() {

if (triggerAsyncId === null)
triggerAsyncId = getDefaultTriggerAsyncId();
nextTickQueue.push(new TickObject(callback, args, triggerAsyncId));
push(new TickObject(callback, args, triggerAsyncId));
}
}