timers: re-enter C++ less frequently
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: https://github.com/nodejs/node/pull/18486 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
This commit is contained in:
parent
9b8e1c2e4f
commit
a986158cbf
@ -223,13 +223,11 @@ function TimersList(msecs, unrefed) {
|
|||||||
|
|
||||||
// adds listOnTimeout to the C++ object prototype, as
|
// adds listOnTimeout to the C++ object prototype, as
|
||||||
// V8 would not inline it otherwise.
|
// V8 would not inline it otherwise.
|
||||||
TimerWrap.prototype[kOnTimeout] = function listOnTimeout() {
|
TimerWrap.prototype[kOnTimeout] = function listOnTimeout(now) {
|
||||||
var list = this._list;
|
var list = this._list;
|
||||||
var msecs = list.msecs;
|
var msecs = list.msecs;
|
||||||
|
|
||||||
debug('timeout callback %d', msecs);
|
debug('timeout callback %d', msecs);
|
||||||
|
|
||||||
var now = TimerWrap.now();
|
|
||||||
debug('now: %d', now);
|
debug('now: %d', now);
|
||||||
|
|
||||||
var diff, timer;
|
var diff, timer;
|
||||||
@ -430,11 +428,12 @@ setTimeout[internalUtil.promisify.custom] = function(after, value) {
|
|||||||
exports.setTimeout = setTimeout;
|
exports.setTimeout = setTimeout;
|
||||||
|
|
||||||
|
|
||||||
function ontimeout(timer) {
|
function ontimeout(timer, start) {
|
||||||
var args = timer._timerArgs;
|
var args = timer._timerArgs;
|
||||||
if (typeof timer._onTimeout !== 'function')
|
if (typeof timer._onTimeout !== 'function')
|
||||||
return promiseResolve(timer._onTimeout, args[0]);
|
return promiseResolve(timer._onTimeout, args[0]);
|
||||||
const start = TimerWrap.now();
|
if (start === undefined && timer._repeat)
|
||||||
|
start = TimerWrap.now();
|
||||||
if (!args)
|
if (!args)
|
||||||
timer._onTimeout();
|
timer._onTimeout();
|
||||||
else
|
else
|
||||||
@ -518,11 +517,11 @@ exports.clearInterval = function(timer) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
function unrefdHandle() {
|
function unrefdHandle(now) {
|
||||||
try {
|
try {
|
||||||
// Don't attempt to call the callback if it is not a function.
|
// Don't attempt to call the callback if it is not a function.
|
||||||
if (typeof this.owner._onTimeout === 'function') {
|
if (typeof this.owner._onTimeout === 'function') {
|
||||||
ontimeout(this.owner);
|
ontimeout(this.owner, now);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
// Make sure we clean up if the callback is no longer a function
|
// Make sure we clean up if the callback is no longer a function
|
||||||
|
@ -37,6 +37,7 @@ using v8::FunctionTemplate;
|
|||||||
using v8::HandleScope;
|
using v8::HandleScope;
|
||||||
using v8::Integer;
|
using v8::Integer;
|
||||||
using v8::Local;
|
using v8::Local;
|
||||||
|
using v8::Number;
|
||||||
using v8::Object;
|
using v8::Object;
|
||||||
using v8::String;
|
using v8::String;
|
||||||
using v8::Value;
|
using v8::Value;
|
||||||
@ -139,8 +140,10 @@ class TimerWrap : public HandleWrap {
|
|||||||
HandleScope handle_scope(env->isolate());
|
HandleScope handle_scope(env->isolate());
|
||||||
Context::Scope context_scope(env->context());
|
Context::Scope context_scope(env->context());
|
||||||
Local<Value> ret;
|
Local<Value> ret;
|
||||||
|
Local<Value> args[1];
|
||||||
do {
|
do {
|
||||||
ret = wrap->MakeCallback(kOnTimeout, 0, nullptr).ToLocalChecked();
|
args[0] = GetNow(env);
|
||||||
|
ret = wrap->MakeCallback(kOnTimeout, 1, args).ToLocalChecked();
|
||||||
} while (ret->IsUndefined() &&
|
} while (ret->IsUndefined() &&
|
||||||
!env->tick_info()->has_thrown() &&
|
!env->tick_info()->has_thrown() &&
|
||||||
wrap->object()->Get(env->context(),
|
wrap->object()->Get(env->context(),
|
||||||
@ -150,14 +153,18 @@ class TimerWrap : public HandleWrap {
|
|||||||
|
|
||||||
static void Now(const FunctionCallbackInfo<Value>& args) {
|
static void Now(const FunctionCallbackInfo<Value>& args) {
|
||||||
Environment* env = Environment::GetCurrent(args);
|
Environment* env = Environment::GetCurrent(args);
|
||||||
|
args.GetReturnValue().Set(GetNow(env));
|
||||||
|
}
|
||||||
|
|
||||||
|
static Local<Value> GetNow(Environment* env) {
|
||||||
uv_update_time(env->event_loop());
|
uv_update_time(env->event_loop());
|
||||||
uint64_t now = uv_now(env->event_loop());
|
uint64_t now = uv_now(env->event_loop());
|
||||||
CHECK(now >= env->timer_base());
|
CHECK(now >= env->timer_base());
|
||||||
now -= env->timer_base();
|
now -= env->timer_base();
|
||||||
if (now <= 0xfffffff)
|
if (now <= 0xfffffff)
|
||||||
args.GetReturnValue().Set(static_cast<uint32_t>(now));
|
return Integer::New(env->isolate(), static_cast<uint32_t>(now));
|
||||||
else
|
else
|
||||||
args.GetReturnValue().Set(static_cast<double>(now));
|
return Number::New(env->isolate(), static_cast<double>(now));
|
||||||
}
|
}
|
||||||
|
|
||||||
uv_timer_t handle_;
|
uv_timer_t handle_;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user