Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Reduced the number of times the nextTick logic needs to transition from
 JS to runtime.  Did this by doing the following:

  - Added a JS file at ./lib/async_wrap.js to expose certain async-wrap
    functionality to JS code.
  - exposed array of async-wrap callbacks from runtime to JS via an
    array.  This is in anticpation of eventually supporting multiple
    async-wrap callbacks.
  - exposed current-async-ID & next-async-ID counters from runtime
    through JS via mapping an int64 to int32[2].  This allows reading/
    writing of these values without transition to runtime &
    re-allocaiton of v8 types on each read/write.
  • Loading branch information
Mike Kaufman committed Apr 19, 2016
commit 7fc44a936bbe227a3445b133428660d2ddfdb00b
170 changes: 170 additions & 0 deletions lib/async_wrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
'use strict';

const async_wrap = process.binding('async_wrap');

const nextIdArray = async_wrap.getNextAsyncIdArray();
const currentIdArray = async_wrap.getCurrentAsyncIdArray();
const fieldsArray = async_wrap.getAsyncHookFields();
const asyncCallbacks = async_wrap.getAsyncCallbacks();

function getLittleEndian(a) {
return a[0] + a[1] * 0x100000000;
}

function getBigEndian(a) {
return a[1] + a[0] * 0x100000000;
}

function setLittleEndian(a, val) {

if (val < 0) {
throw new Error('Negative value not supported');
}

var lword = val & 0xffffffff;
var hword = 0;
if (val > 0xffffffff) {
// effectively we're doing shift-right by 32 bits. Javascript bit
// operators convert operands to 32 bits, so we lose the
// high-order bits if we try to use >>> or >>.
hword = Math.floor((val / 0x100000000));
}
a[0] = lword;
a[1] = hword;
}

function setBigEndian(a, val) {

if (val < 0) {
throw new Error('Negative value not supported');
}

var lword = val & 0xffffffff;
var hword = 0;
if (val > 0xffffffff) {
// effectively we're doing shift-right by 32 bits. Javascript bit
// operators convert operands to 32 bits, so we lose the
// high-order bits if we try to use >>> or >>.
hword = Math.floor((val / 0x100000000));
}
a[1] = lword;
a[0] = hword;
}

function incrementLittleEndian(a) {
// carry-over if lsb is maxed out
if (a[0] === 0xffffffff) {
a[0] = 0;
a[1]++;
}
a[0]++;
return a[0] + a[1] * 0x100000000;
}

function incrementBigEndian(a) {
// carry-over if lsb is maxed out
if (a[1] === 0xffffffff) {
a[1] = 0;
a[0]++;
}
a[1]++;
return a[1] + a[0] * 0x100000000;
}

function getCurrentIdLittleEndian() {
return getLittleEndian(currentIdArray);
}

function getCurrentIdBigEndian() {
return getBigEndian(currentIdArray);
}

function setCurrentIdLittleEndian(val) {
return setLittleEndian(currentIdArray, val);
}

function setCurrentIdBigEndian(val) {
return setBigEndian(currentIdArray, val);
}

function incrementNextIdLittleEndian() {
return incrementLittleEndian(nextIdArray);
}

function incrementNextIdBigEndian() {
return incrementBigEndian(nextIdArray);
}

// must match enum definitions in AsyncHook class in env.h
const kEnableCallbacks = 0;

function callbacksEnabled() {
return (fieldsArray[kEnableCallbacks] !== 0 ? true : false);
}

const getCurrentAsyncId =
process.binding('os').isBigEndian ?
getCurrentIdBigEndian : getCurrentIdLittleEndian;

const setCurrentAsyncId =
process.binding('os').isBigEndian ?
setCurrentIdBigEndian : setCurrentIdLittleEndian;

const incrementNextAsyncId =
process.binding('os').isBigEndian ?
incrementNextIdBigEndian : incrementNextIdLittleEndian;

function notifyAsyncEnqueue(asyncId) {
if (callbacksEnabled()) {
const asyncState = {};
for (let i = 0; i < asyncCallbacks.length; i++) {
if (asyncCallbacks[i].init) {
/* init(asyncId, provider, parentId, parentObject) */
asyncCallbacks[i].init.call(asyncState, asyncId,
async_wrap.Providers.NEXTTICK, undefined, undefined);
}
}
return asyncState;
}
return undefined;
}

function notifyAsyncStart(asyncId, asyncState) {
setCurrentAsyncId(asyncId);
if (asyncState) {
for (let i = 0; i < asyncCallbacks.length; i++) {
if (asyncCallbacks[i].pre) {
/* pre(asyncId); */
asyncCallbacks[i].pre.call(asyncState, asyncId);
}
}
}
}

function notifyAsyncEnd(asyncId, asyncState, callbackThrew) {
if (asyncState) {
for (let i = 0; i < asyncCallbacks.length; i++) {
if (asyncCallbacks[i].post) {
/* post(asyncId, didUserCodeThrow); */
asyncCallbacks[i].post.call(asyncState, asyncId, callbackThrew);
}
}

setCurrentAsyncId(0);

for (let i = 0; i < asyncCallbacks.length; i++) {
if (asyncCallbacks[i].destroy) {
/* destroy(asyncId); */
asyncCallbacks[i].destroy.call(undefined, asyncId);
}
}
}
}

module.exports.incrementNextAsyncId = incrementNextAsyncId;
module.exports.getCurrentAsyncId = getCurrentAsyncId;
module.exports.setCurrentAsyncId = setCurrentAsyncId;
module.exports.callbacksEnabled = callbacksEnabled;
module.exports.notifyAsyncEnqueue = notifyAsyncEnqueue;
module.exports.notifyAsyncStart = notifyAsyncStart;
module.exports.notifyAsyncEnd = notifyAsyncEnd;
72 changes: 48 additions & 24 deletions lib/internal/process/next_tick.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
exports.setup = setupNextTick;

function setupNextTick() {
const async_wrap = require('async_wrap');

var asyncWrap = process.binding('async_wrap');
const promises = require('internal/process/promises');
const emitPendingUnhandledRejections = promises.setup(scheduleMicrotasks);
var nextTickQueue = [];
Expand Down Expand Up @@ -95,26 +95,32 @@ function setupNextTick() {
tock = nextTickQueue[tickInfo[kIndex]++];
callback = tock.callback;

asyncWrap.notifyAsyncStart(
tock.ranInitCallback, tock.asyncId, tock.asyncState);

args = tock.args;
var callbackThrew = true;
try {
// Using separate callback execution functions allows direct
// callback invocation with small numbers of arguments to avoid the
// performance hit associated with using `fn.apply()`
if (!tock.asyncState) {
async_wrap.setCurrentAsyncId(tock.asyncId);
_combinedTickCallback(args, callback);
callbackThrew = false;
async_wrap.setCurrentAsyncId(0);
}
finally {
asyncWrap.notifyAsyncEnd(
tock.ranInitCallback, tock.asyncId,
tock.asyncState, callbackThrew);
else {
var callbackThrew = true;
try {
async_wrap.notifyAsyncStart(tock.asyncId, tock.asyncState);

// Using separate callback execution functions allows direct
// callback invocation with small numbers of arguments to avoid the
// performance hit associated with using `fn.apply()`
_combinedTickCallback(args, callback);
callbackThrew = false;
}
finally {
async_wrap.notifyAsyncEnd(
tock.asyncId, tock.asyncState, callbackThrew);
}
}

if (1e4 < tickInfo[kIndex])
tickDone();

}
tickDone();
_runMicrotasks();
Expand All @@ -133,10 +139,29 @@ function setupNextTick() {
args = tock.args;
if (domain)
domain.enter();
// Using separate callback execution functions allows direct
// callback invocation with small numbers of arguments to avoid the
// performance hit associated with using `fn.apply()`
_combinedTickCallback(args, callback);

if (!tock.asyncState) {
async_wrap.setCurrentAsyncId(tock.asyncId);
_combinedTickCallback(args, callback);
async_wrap.setCurrentAsyncId(0);
}
else {
var callbackThrew = true;
try {
async_wrap.notifyAsyncStart(tock.asyncId, tock.asyncState);

// Using separate callback execution functions allows direct
// callback invocation with small numbers of arguments to avoid the
// performance hit associated with using `fn.apply()`
_combinedTickCallback(args, callback);
callbackThrew = false;
}
finally {
async_wrap.notifyAsyncEnd(
tock.asyncId, tock.asyncState, callbackThrew);
}
}

if (1e4 < tickInfo[kIndex])
tickDone();
if (domain)
Expand All @@ -152,12 +177,10 @@ function setupNextTick() {
this.callback = c;
this.domain = process.domain || null;
this.args = args;
this.parentAsyncId = asyncWrap.getCurrentAsyncId();
this.asyncId = asyncWrap.getNextAsyncId();
this.asyncState = {};
this.ranInitCallback = asyncWrap.notifyAsyncEnqueue(
this.asyncId, this.asyncState, undefined, undefined,
asyncWrap.Providers.NEXTTICK);
this.asyncId = async_wrap.incrementNextAsyncId();
if (async_wrap.callbacksEnabled()) {
this.asyncState = async_wrap.notifyAsyncEnqueue(this.asyncId);
}
}

function nextTick(callback) {
Expand All @@ -177,4 +200,5 @@ function setupNextTick() {
nextTickQueue.push(new TickObject(callback, args));
tickInfo[kLength]++;
}

}
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'lib/_debug_agent.js',
'lib/_debugger.js',
'lib/assert.js',
'lib/async_wrap.js',
'lib/buffer.js',
'lib/child_process.js',
'lib/console.js',
Expand Down
2 changes: 1 addition & 1 deletion src/async-wrap-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ inline AsyncWrap::AsyncWrap(Environment* env,
ProviderType provider,
AsyncWrap* parent)
: BaseObject(env, object), bits_(static_cast<uint32_t>(provider) << 1),
uid_(env->get_next_async_wrap_uid()) {
uid_(env->async_hooks()->get_next_async_wrap_uid()) {
CHECK_NE(provider, PROVIDER_NONE);
CHECK_GE(object->InternalFieldCount(), 1);

Expand Down
Loading