node: improve performance of hrtime()

process.hrtime() was performing too many operations in C++ that could be
done faster in JS. Move those operations over by creating a length 4
Uint32Array and perform bitwise operations on the seconds so that it was
unnecessary for the native API to do any object creation or set any
fields.

This has improved performance from ~350 ns/op to ~65 ns/op. Light
benchmark included to demonstrate the performance change.

PR-URL: https://github.com/nodejs/node/pull/3780
Reviewed-By: Fedor Indutny <fedor@indutny.com>
This commit is contained in:
Trevor Norris 2015-11-11 00:15:15 -07:00 committed by Jeremiah Senkpiel
parent c8fc217dc7
commit 89f056bdf3
3 changed files with 46 additions and 12 deletions

View File

@ -0,0 +1,18 @@
'use strict';
const common = require('../common');
const bench = common.createBenchmark(main, {
n: [1e6]
});
function main(conf) {
const n = conf.n >>> 0;
bench.start();
for (var i = 0; i < n; i++) {
process.hrtime();
}
bench.end(n);
}

View File

@ -2132,22 +2132,23 @@ void Hrtime(const FunctionCallbackInfo<Value>& args) {
uint64_t t = uv_hrtime(); uint64_t t = uv_hrtime();
if (args.Length() > 0) { if (!args[1]->IsUndefined()) {
// return a time diff tuple if (!args[1]->IsArray()) {
if (!args[0]->IsArray()) {
return env->ThrowTypeError( return env->ThrowTypeError(
"process.hrtime() only accepts an Array tuple."); "process.hrtime() only accepts an Array tuple");
} }
Local<Array> inArray = Local<Array>::Cast(args[0]); args.GetReturnValue().Set(true);
uint64_t seconds = inArray->Get(0)->Uint32Value();
uint64_t nanos = inArray->Get(1)->Uint32Value();
t -= (seconds * NANOS_PER_SEC) + nanos;
} }
Local<Array> tuple = Array::New(env->isolate(), 2); Local<ArrayBuffer> ab = args[0].As<Uint32Array>()->Buffer();
tuple->Set(0, Integer::NewFromUnsigned(env->isolate(), t / NANOS_PER_SEC)); uint32_t* fields = static_cast<uint32_t*>(ab->GetContents().Data());
tuple->Set(1, Integer::NewFromUnsigned(env->isolate(), t % NANOS_PER_SEC));
args.GetReturnValue().Set(tuple); // These three indices will contain the values for the hrtime tuple. The
// seconds value is broken into the upper/lower 32 bits and stored in two
// uint32 fields to be converted back in JS.
fields[0] = (t / NANOS_PER_SEC) >> 32;
fields[1] = (t / NANOS_PER_SEC) & 0xffffffff;
fields[2] = t % NANOS_PER_SEC;
} }
extern "C" void node_module_register(void* m) { extern "C" void node_module_register(void* m) {

View File

@ -181,12 +181,27 @@
} }
startup.setupProcessObject = function() { startup.setupProcessObject = function() {
const _hrtime = process.hrtime;
const hrValues = new Uint32Array(3);
process._setupProcessObject(pushValueToArray); process._setupProcessObject(pushValueToArray);
function pushValueToArray() { function pushValueToArray() {
for (var i = 0; i < arguments.length; i++) for (var i = 0; i < arguments.length; i++)
this.push(arguments[i]); this.push(arguments[i]);
} }
process.hrtime = function hrtime(ar) {
const ret = [0, 0];
if (_hrtime(hrValues, ar)) {
ret[0] = (hrValues[0] * 0x100000000 + hrValues[1]) - ar[0];
ret[1] = hrValues[2] - ar[1];
} else {
ret[0] = hrValues[0] * 0x100000000 + hrValues[1];
ret[1] = hrValues[2];
}
return ret;
};
}; };
startup.globalVariables = function() { startup.globalVariables = function() {