Created
October 4, 2010 20:13
-
-
Save zimbatm/610338 to your computer and use it in GitHub Desktop.
ev_now patches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 314733bb26efdd0a9b6fcb9ea7c1a352bf34f902 Mon Sep 17 00:00:00 2001 | |
From: Jonas Pfenniger <jonas@pfenniger.name> | |
Date: Fri, 1 Oct 2010 21:29:53 +0200 | |
Subject: [PATCH 1/2] Add process.now() | |
To avoid making a syscall, process.now() re-uses the timestamp | |
of the current libev loop, which is provided by ev_now(). | |
The date object of process.now() is cached until ev_now() changes for | |
performance reasons. | |
Note that Date.now() also seems to optimize, making only one syscall | |
if used consecutively. | |
--- | |
src/node.cc | 18 ++++++++++++++++++ | |
1 files changed, 18 insertions(+), 0 deletions(-) | |
diff --git a/src/node.cc b/src/node.cc | |
index 004fa99..704bf5e 100644 | |
--- a/src/node.cc | |
+++ b/src/node.cc | |
@@ -1541,6 +1541,19 @@ static void ProcessTitleSetter(Local<String> property, | |
OS::SetProcessTitle(*title); | |
} | |
+static Handle<Value> Now(const Arguments& args) { | |
+ static ev_tstamp cached_now = 0; | |
+ static Persistent<Value> cached_date; | |
+ | |
+ ev_tstamp now = ev_now(EV_DEFAULT_UC); | |
+ if (now != cached_now) { | |
+ cached_date.Dispose(); | |
+ cached_date = Persistent<Value>::New(NODE_UNIXTIME_V8(now)); | |
+ cached_now = now; | |
+ } | |
+ | |
+ return cached_date; | |
+} | |
static void Load(int argc, char *argv[]) { | |
HandleScope scope; | |
@@ -1555,6 +1568,11 @@ static void Load(int argc, char *argv[]) { | |
ProcessTitleGetter, | |
ProcessTitleSetter); | |
+ Local<FunctionTemplate> now = FunctionTemplate::New(Now); | |
+ now->InstanceTemplate()->SetInternalFieldCount(0); | |
+ now->SetClassName(String::NewSymbol("now")); | |
+ process->Set(String::NewSymbol("now"), now->GetFunction()); | |
+ | |
// process.version | |
process->Set(String::NewSymbol("version"), String::New(NODE_VERSION)); | |
-- | |
1.7.3.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From b32625003369d997dcf0bec77cc27284c9e57ab8 Mon Sep 17 00:00:00 2001 | |
From: Jonas Pfenniger <jonas@pfenniger.name> | |
Date: Mon, 4 Oct 2010 22:10:02 +0200 | |
Subject: [PATCH 2/2] Add process.loopTime | |
process.loopTime is updated on each iteration of the grand loop with | |
the Date of ev_now(). | |
--- | |
src/node.cc | 11 +++++++++++ | |
1 files changed, 11 insertions(+), 0 deletions(-) | |
diff --git a/src/node.cc b/src/node.cc | |
index 704bf5e..0a7d672 100644 | |
--- a/src/node.cc | |
+++ b/src/node.cc | |
@@ -68,6 +68,8 @@ static Persistent<String> listeners_symbol; | |
static Persistent<String> uncaught_exception_symbol; | |
static Persistent<String> emit_symbol; | |
+static Persistent<String> loop_time_symbol; | |
+ | |
static int option_end_index = 0; | |
static bool use_debug_agent = false; | |
static bool debug_wait_connect = false; | |
@@ -181,6 +183,11 @@ static void Spin(EV_P_ ev_idle *watcher, int revents) { | |
} | |
+static void UpdateLoopTime(void) { | |
+ HandleScope scope; | |
+ process->Set(loop_time_symbol, Local<Value>::New(NODE_UNIXTIME_V8(ev_now(EV_DEFAULT_UC)))); | |
+} | |
+ | |
static void Tick(void) { | |
// Avoid entering a V8 scope. | |
if (!need_tick_cb) return; | |
@@ -220,6 +227,7 @@ static void PrepareTick(EV_P_ ev_prepare *watcher, int revents) { | |
static void CheckTick(EV_P_ ev_check *watcher, int revents) { | |
assert(watcher == &check_tick_watcher); | |
assert(revents == EV_CHECK); | |
+ UpdateLoopTime(); | |
Tick(); | |
} | |
@@ -1558,6 +1566,9 @@ static Handle<Value> Now(const Arguments& args) { | |
static void Load(int argc, char *argv[]) { | |
HandleScope scope; | |
+ // some constants | |
+ loop_time_symbol = NODE_PSYMBOL("loopTime"); | |
+ | |
Local<FunctionTemplate> process_template = FunctionTemplate::New(); | |
node::EventEmitter::Initialize(process_template); | |
-- | |
1.7.3.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.now().getTime() -> 13.70 MHz | |
process.loopTime.getTime() -> 37.31 MHz | |
Date.now() -> 7.97 MHz | |
(new Date).getTime() -> 5.30 MHz | |
----------- | |
process.now().getTime() -> 13.89 MHz | |
process.loopTime.getTime() -> 39.06 MHz | |
Date.now() -> 7.79 MHz | |
(new Date).getTime() -> 5.45 MHz | |
----------- | |
process.now().getTime() -> 13.89 MHz | |
process.loopTime.getTime() -> 39.68 MHz | |
Date.now() -> 7.76 MHz | |
(new Date).getTime() -> 5.90 MHz | |
----------- | |
process.now().getTime() -> 13.66 MHz | |
process.loopTime.getTime() -> 38.46 MHz | |
Date.now() -> 7.69 MHz | |
(new Date).getTime() -> 5.90 MHz | |
----------- | |
process.now().getTime() -> 14.12 MHz | |
process.loopTime.getTime() -> 40.65 MHz | |
Date.now() -> 7.74 MHz | |
(new Date).getTime() -> 5.92 MHz | |
----------- | |
process.now().getTime() -> 13.77 MHz | |
process.loopTime.getTime() -> 39.06 MHz | |
Date.now() -> 8.13 MHz | |
(new Date).getTime() -> 5.75 MHz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var kLoops= 5e5; | |
String.prototype.pad= function pad (len) { | |
var str= this; | |
while (str.length < len) str= " "+ str; | |
return str; | |
} | |
function loop () { | |
if (process.now) { | |
var ctr= kLoops; | |
var t= Date.now(); | |
while (ctr--) { | |
process.now().getTime(); process.now().getTime(); process.now().getTime(); process.now().getTime(); process.now().getTime(); | |
process.now().getTime(); process.now().getTime(); process.now().getTime(); process.now().getTime(); process.now().getTime(); | |
} | |
t= Date.now()- t; | |
console.log("process.now().getTime() ->"+ (kLoops*10/t/1e3).toFixed(2).pad(8)+ " MHz"); | |
} | |
if (process.loopTime) { | |
var ctr= kLoops; | |
var t= Date.now(); | |
while (ctr--) { | |
process.loopTime.getTime(); process.loopTime.getTime(); process.loopTime.getTime(); process.loopTime.getTime(); process.loopTime.getTime(); | |
process.loopTime.getTime(); process.loopTime.getTime(); process.loopTime.getTime(); process.loopTime.getTime(); process.loopTime.getTime(); | |
} | |
t= Date.now()- t; | |
console.log("process.loopTime.getTime() ->"+ (kLoops*10/t/1e3).toFixed(2).pad(8)+ " MHz"); | |
} | |
var ctr= kLoops; | |
var t= Date.now(); | |
while (ctr--) { | |
Date.now(); Date.now(); Date.now(); Date.now(); Date.now(); | |
Date.now(); Date.now(); Date.now(); Date.now(); Date.now(); | |
} | |
t= Date.now()- t; | |
console.log("Date.now() ->"+ (kLoops*10/t/1e3).toFixed(2).pad(8)+ " MHz"); | |
var ctr= kLoops; | |
var t= Date.now(); | |
while (ctr--) { | |
(new Date).getTime(); (new Date).getTime(); (new Date).getTime(); (new Date).getTime(); (new Date).getTime(); | |
(new Date).getTime(); (new Date).getTime(); (new Date).getTime(); (new Date).getTime(); (new Date).getTime(); | |
} | |
t= Date.now()- t; | |
console.log("(new Date).getTime() ->"+ (kLoops*10/t/1e3).toFixed(2).pad(8)+ " MHz"); | |
console.log("-----------") | |
setTimeout(loop, 999); | |
} | |
setTimeout(loop, 999); |
patches rebased on HEAD: dbf0d6b ClearWeak on ObjectWraps...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE: there is a bug on the second patch, where process.loopTime is not set on the first loop