Skip to content

Commit

Permalink
Minor optimizations based on IRHydra2.
Browse files Browse the repository at this point in the history
The comparison to PROMISE_IDENTITY/PROMISE_THROWER were being handled
as unoptimized calls to a generic strict comparison function; not worth it,
considering these cases are very rarely taken.

Try to avoid polymorphic functions by ensuring that the
PROMISE_FAKE_CAPABILITY is also a valid PromiseCapability.

`x == null` seems to trigger deoptimization at least once; just use
strict equality tests.
  • Loading branch information
cscott committed Dec 12, 2015
1 parent 3216461 commit 44d3b8b
Showing 1 changed file with 8 additions and 23 deletions.
31 changes: 8 additions & 23 deletions lib/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,13 @@ function tryCatch1n(Fn, arg1) {
// for moar speed.

function TypeIsObject(x) {
/* jshint eqnull:true */
// This is expensive when it returns false; use this function
// when you expect it to return true in the common case.
return x != null && Object(x) === x;
return x !== void 0 && x !== null && Object(x) === x;
}
function TypeIsNotObject(val) {
// This is more appropriate when TypeIsObject may return false.
/* jshint eqnull:true */
return val == null || val === true || val === false ||
return val === void 0 || val === null || val === true || val === false ||
typeof val === 'string' || typeof val === 'number';
}

Expand Down Expand Up @@ -192,11 +190,7 @@ var fakeRetvalFromThen = false;
// Constants for Promise implementation
var PROMISE_IDENTITY = (function PROMISE_IDENTITY(v) { return v; });
var PROMISE_THROWER = (function PROMISE_THROWER(t) { throw t; });
var PROMISE_FAKE_CAPABILITY = {
resolve: function PROMISE_FAKE_CAPABILITY_RESOLVE() {},
reject: function PROMISE_FAKE_CAPABILITY_REJECT() {},
promise: { fakePromise: true },
};
var PROMISE_FAKE_CAPABILITY = new PromiseCapability(void 0, void 0);
var PROMISE_PENDING = 0;
var PROMISE_RESOLVING = 1; // PROMISE_PENDING combined with alreadyResolved
var PROMISE_FULFILLED = 2;
Expand Down Expand Up @@ -237,21 +231,12 @@ function promiseReactionReject(promiseCapability, handlerResult) {
}
function promiseReactionJob(handler, promiseCapability, argument) {
var handlerResult;
if (handler === PROMISE_IDENTITY) {
if (promiseCapability === PROMISE_FAKE_CAPABILITY) { return; }
handlerResult = argument;
} else if (handler === PROMISE_THROWER) {
if (promiseCapability === PROMISE_FAKE_CAPABILITY) { return; }
handlerResult = argument;
// Encapsulate try/catch here to avoid deoptimization.
handlerResult = tryCatch1(handler, argument);
if (promiseCapability === PROMISE_FAKE_CAPABILITY) { return; }
if (handlerResult === errorObj) {
handlerResult = handlerResult.e;
return promiseReactionReject(promiseCapability, handlerResult);
} else {
// Encapsulate try/catch here to avoid deoptimization.
handlerResult = tryCatch1(handler, argument);
if (promiseCapability === PROMISE_FAKE_CAPABILITY) { return; }
if (handlerResult === errorObj) {
handlerResult = handlerResult.e;
return promiseReactionReject(promiseCapability, handlerResult);
}
}
return promiseReactionResolve(promiseCapability, handlerResult);
}
Expand Down

0 comments on commit 44d3b8b

Please sign in to comment.