timers: improve Timer.now() performance
Record the start time so we can make the return value of Timer.now() relative to it, increasing the chances that it fits in a tagged integer instead of a heap-allocated double, at least for the first one or two billion milliseconds. PR-URL: https://github.com/nodejs/io.js/pull/2256 Reviewed-By: Trevor Norris <trev.norris@gmail.com>
This commit is contained in:
parent
3663b124e6
commit
543dabb609
@ -171,6 +171,7 @@ inline Environment::Environment(v8::Local<v8::Context> context,
|
|||||||
uv_loop_t* loop)
|
uv_loop_t* loop)
|
||||||
: isolate_(context->GetIsolate()),
|
: isolate_(context->GetIsolate()),
|
||||||
isolate_data_(IsolateData::GetOrCreate(context->GetIsolate(), loop)),
|
isolate_data_(IsolateData::GetOrCreate(context->GetIsolate(), loop)),
|
||||||
|
timer_base_(uv_now(loop)),
|
||||||
using_smalloc_alloc_cb_(false),
|
using_smalloc_alloc_cb_(false),
|
||||||
using_domains_(false),
|
using_domains_(false),
|
||||||
using_abort_on_uncaught_exc_(false),
|
using_abort_on_uncaught_exc_(false),
|
||||||
@ -286,6 +287,10 @@ inline Environment::TickInfo* Environment::tick_info() {
|
|||||||
return &tick_info_;
|
return &tick_info_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline uint64_t Environment::timer_base() const {
|
||||||
|
return timer_base_;
|
||||||
|
}
|
||||||
|
|
||||||
inline bool Environment::using_smalloc_alloc_cb() const {
|
inline bool Environment::using_smalloc_alloc_cb() const {
|
||||||
return using_smalloc_alloc_cb_;
|
return using_smalloc_alloc_cb_;
|
||||||
}
|
}
|
||||||
|
@ -398,6 +398,7 @@ class Environment {
|
|||||||
inline AsyncHooks* async_hooks();
|
inline AsyncHooks* async_hooks();
|
||||||
inline DomainFlag* domain_flag();
|
inline DomainFlag* domain_flag();
|
||||||
inline TickInfo* tick_info();
|
inline TickInfo* tick_info();
|
||||||
|
inline uint64_t timer_base() const;
|
||||||
|
|
||||||
static inline Environment* from_cares_timer_handle(uv_timer_t* handle);
|
static inline Environment* from_cares_timer_handle(uv_timer_t* handle);
|
||||||
inline uv_timer_t* cares_timer_handle();
|
inline uv_timer_t* cares_timer_handle();
|
||||||
@ -501,6 +502,7 @@ class Environment {
|
|||||||
AsyncHooks async_hooks_;
|
AsyncHooks async_hooks_;
|
||||||
DomainFlag domain_flag_;
|
DomainFlag domain_flag_;
|
||||||
TickInfo tick_info_;
|
TickInfo tick_info_;
|
||||||
|
const uint64_t timer_base_;
|
||||||
uv_timer_t cares_timer_handle_;
|
uv_timer_t cares_timer_handle_;
|
||||||
ares_channel cares_channel_;
|
ares_channel cares_channel_;
|
||||||
ares_task_list cares_task_list_;
|
ares_task_list cares_task_list_;
|
||||||
|
@ -101,6 +101,8 @@ class TimerWrap : public HandleWrap {
|
|||||||
Environment* env = Environment::GetCurrent(args);
|
Environment* env = Environment::GetCurrent(args);
|
||||||
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());
|
||||||
|
now -= env->timer_base();
|
||||||
if (now <= 0xfffffff)
|
if (now <= 0xfffffff)
|
||||||
args.GetReturnValue().Set(static_cast<uint32_t>(now));
|
args.GetReturnValue().Set(static_cast<uint32_t>(now));
|
||||||
else
|
else
|
||||||
|
8
test/parallel/test-timers-now.js
Normal file
8
test/parallel/test-timers-now.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const common = require('../common');
|
||||||
|
const assert = require('assert');
|
||||||
|
|
||||||
|
// Return value of Timer.now() should easily fit in a SMI right after start-up.
|
||||||
|
const Timer = process.binding('timer_wrap').Timer;
|
||||||
|
assert(Timer.now() < 0x3ffffff);
|
Loading…
x
Reference in New Issue
Block a user