Skip to content

Commit

Permalink
timers: re-enter C++ less frequently
Browse files Browse the repository at this point in the history
Pass in Timer.now() as an argument of kOnTimeout instead of always
re-entering C++ to get it. Also don't constantly call Timer.now()
from ontimeout, even when it isn't needed. Improves performance
on our pooled benchmark by upwards of 40%.

PR-URL: nodejs#18486
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
  • Loading branch information
apapirovski authored and MayaLekova committed May 8, 2018
1 parent 248d984 commit b531515
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
13 changes: 6 additions & 7 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,11 @@ function TimersList(msecs, unrefed) {

// adds listOnTimeout to the C++ object prototype, as
// V8 would not inline it otherwise.
TimerWrap.prototype[kOnTimeout] = function listOnTimeout() {
TimerWrap.prototype[kOnTimeout] = function listOnTimeout(now) {
var list = this._list;
var msecs = list.msecs;

debug('timeout callback %d', msecs);

var now = TimerWrap.now();
debug('now: %d', now);

var diff, timer;
Expand Down Expand Up @@ -430,11 +428,12 @@ setTimeout[internalUtil.promisify.custom] = function(after, value) {
exports.setTimeout = setTimeout;


function ontimeout(timer) {
function ontimeout(timer, start) {
var args = timer._timerArgs;
if (typeof timer._onTimeout !== 'function')
return promiseResolve(timer._onTimeout, args[0]);
const start = TimerWrap.now();
if (start === undefined && timer._repeat)
start = TimerWrap.now();
if (!args)
timer._onTimeout();
else
Expand Down Expand Up @@ -518,11 +517,11 @@ exports.clearInterval = function(timer) {
};


function unrefdHandle() {
function unrefdHandle(now) {
try {
// Don't attempt to call the callback if it is not a function.
if (typeof this.owner._onTimeout === 'function') {
ontimeout(this.owner);
ontimeout(this.owner, now);
}
} finally {
// Make sure we clean up if the callback is no longer a function
Expand Down
13 changes: 10 additions & 3 deletions src/timer_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Integer;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::String;
using v8::Value;
Expand Down Expand Up @@ -139,8 +140,10 @@ class TimerWrap : public HandleWrap {
HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());
Local<Value> ret;
Local<Value> args[1];
do {
ret = wrap->MakeCallback(kOnTimeout, 0, nullptr).ToLocalChecked();
args[0] = GetNow(env);
ret = wrap->MakeCallback(kOnTimeout, 1, args).ToLocalChecked();
} while (ret->IsUndefined() &&
!env->tick_info()->has_thrown() &&
wrap->object()->Get(env->context(),
Expand All @@ -150,14 +153,18 @@ class TimerWrap : public HandleWrap {

static void Now(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
args.GetReturnValue().Set(GetNow(env));
}

static Local<Value> GetNow(Environment* env) {
uv_update_time(env->event_loop());
uint64_t now = uv_now(env->event_loop());
CHECK(now >= env->timer_base());
now -= env->timer_base();
if (now <= 0xfffffff)
args.GetReturnValue().Set(static_cast<uint32_t>(now));
return Integer::New(env->isolate(), static_cast<uint32_t>(now));
else
args.GetReturnValue().Set(static_cast<double>(now));
return Number::New(env->isolate(), static_cast<double>(now));
}

uv_timer_t handle_;
Expand Down

0 comments on commit b531515

Please sign in to comment.