Skip to content

Commit 5d0062f

Browse files
committed
perf_hooks: implement GetTimeOriginTimeStamp with fast API calls
1 parent dd4767f commit 5d0062f

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

benchmark/perf_hooks/time-origin.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const common = require('../common.js');
5+
6+
const bench = common.createBenchmark(main, {
7+
n: [1e6],
8+
method: ['timeOrigin', 'toJSON'],
9+
});
10+
11+
function main({ method, n }) {
12+
switch (method) {
13+
case 'timeOrigin':
14+
benchTimeOrigin(n);
15+
break;
16+
case 'toJSON':
17+
benchToJSON(n);
18+
break;
19+
default:
20+
throw new Error(`Unsupported method ${method}`);
21+
}
22+
}
23+
24+
function benchTimeOrigin(n) {
25+
const arr = [];
26+
for (let i = 0; i < n; ++i) {
27+
arr.push(performance.timeOrigin);
28+
}
29+
30+
bench.start();
31+
for (let i = 0; i < n; i++) {
32+
arr[i] = performance.timeOrigin;
33+
}
34+
bench.end(n);
35+
36+
assert.strictEqual(arr.length, n);
37+
}
38+
39+
function benchToJSON(n) {
40+
bench.start();
41+
for (let i = 0; i < n; i++) {
42+
performance.toJSON();
43+
}
44+
bench.end(n);
45+
}

src/node_perf.cc

+26-9
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ void Notify(const FunctionCallbackInfo<Value>& args) {
254254
void LoopIdleTime(const FunctionCallbackInfo<Value>& args) {
255255
Environment* env = Environment::GetCurrent(args);
256256
uint64_t idle_time = uv_metrics_idle_time(env->event_loop());
257-
args.GetReturnValue().Set(1.0 * idle_time / 1e6);
257+
args.GetReturnValue().Set(1.0 * idle_time / NANOS_PER_MILLIS);
258258
}
259259

260260
void CreateELDHistogram(const FunctionCallbackInfo<Value>& args) {
@@ -278,12 +278,6 @@ void CreateELDHistogram(const FunctionCallbackInfo<Value>& args) {
278278
args.GetReturnValue().Set(histogram->object());
279279
}
280280

281-
void GetTimeOriginTimeStamp(const FunctionCallbackInfo<Value>& args) {
282-
Environment* env = Environment::GetCurrent(args);
283-
args.GetReturnValue().Set(Number::New(
284-
args.GetIsolate(), env->time_origin_timestamp() / MICROS_PER_MILLIS));
285-
}
286-
287281
void MarkBootstrapComplete(const FunctionCallbackInfo<Value>& args) {
288282
Realm* realm = Realm::GetCurrent(args);
289283
CHECK_EQ(realm->kind(), Realm::Kind::kPrincipal);
@@ -307,6 +301,23 @@ static void SlowPerformanceNow(const FunctionCallbackInfo<Value>& args) {
307301
static v8::CFunction fast_performance_now(
308302
v8::CFunction::Make(FastPerformanceNow));
309303

304+
static double GetTimeOriginTimeStampImpl() {
305+
return static_cast<double>(performance_process_start_timestamp) /
306+
MICROS_PER_MILLIS;
307+
}
308+
309+
static double FastGetTimeOriginTimeStamp(v8::Local<v8::Value> receiver) {
310+
return GetTimeOriginTimeStampImpl();
311+
}
312+
313+
static void SlowGetTimeOriginTimeStamp(
314+
const FunctionCallbackInfo<Value>& args) {
315+
args.GetReturnValue().Set(GetTimeOriginTimeStampImpl());
316+
}
317+
318+
static v8::CFunction fast_get_timeorigin_timestamp(
319+
v8::CFunction::Make(FastGetTimeOriginTimeStamp));
320+
310321
static void CreatePerIsolateProperties(IsolateData* isolate_data,
311322
Local<ObjectTemplate> target) {
312323
Isolate* isolate = isolate_data->isolate();
@@ -324,11 +335,15 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
324335
RemoveGarbageCollectionTracking);
325336
SetMethod(isolate, target, "notify", Notify);
326337
SetMethod(isolate, target, "loopIdleTime", LoopIdleTime);
327-
SetMethod(isolate, target, "getTimeOriginTimestamp", GetTimeOriginTimeStamp);
328338
SetMethod(isolate, target, "createELDHistogram", CreateELDHistogram);
329339
SetMethod(isolate, target, "markBootstrapComplete", MarkBootstrapComplete);
330340
SetFastMethodNoSideEffect(
331341
isolate, target, "now", SlowPerformanceNow, &fast_performance_now);
342+
SetFastMethodNoSideEffect(isolate,
343+
target,
344+
"getTimeOriginTimestamp",
345+
SlowGetTimeOriginTimeStamp,
346+
&fast_get_timeorigin_timestamp);
332347
}
333348

334349
void CreatePerContextProperties(Local<Object> target,
@@ -391,12 +406,14 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
391406
registry->Register(RemoveGarbageCollectionTracking);
392407
registry->Register(Notify);
393408
registry->Register(LoopIdleTime);
394-
registry->Register(GetTimeOriginTimeStamp);
395409
registry->Register(CreateELDHistogram);
396410
registry->Register(MarkBootstrapComplete);
397411
registry->Register(SlowPerformanceNow);
398412
registry->Register(FastPerformanceNow);
399413
registry->Register(fast_performance_now.GetTypeInfo());
414+
registry->Register(SlowGetTimeOriginTimeStamp);
415+
registry->Register(FastGetTimeOriginTimeStamp);
416+
registry->Register(fast_get_timeorigin_timestamp.GetTypeInfo());
400417
HistogramBase::RegisterExternalReferences(registry);
401418
IntervalHistogram::RegisterExternalReferences(registry);
402419
}

0 commit comments

Comments
 (0)