Skip to content

Commit 69e1fe6

Browse files
committed
perf_hooks: implement GetTimeOriginTimeStamp with fast API calls
1 parent dd4767f commit 69e1fe6

File tree

2 files changed

+71
-10
lines changed

2 files changed

+71
-10
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-10
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ using v8::Integer;
2424
using v8::Isolate;
2525
using v8::Local;
2626
using v8::MaybeLocal;
27-
using v8::Number;
2827
using v8::Object;
2928
using v8::ObjectTemplate;
3029
using v8::PropertyAttribute;
@@ -254,7 +253,7 @@ void Notify(const FunctionCallbackInfo<Value>& args) {
254253
void LoopIdleTime(const FunctionCallbackInfo<Value>& args) {
255254
Environment* env = Environment::GetCurrent(args);
256255
uint64_t idle_time = uv_metrics_idle_time(env->event_loop());
257-
args.GetReturnValue().Set(1.0 * idle_time / 1e6);
256+
args.GetReturnValue().Set(1.0 * idle_time / NANOS_PER_MILLIS);
258257
}
259258

260259
void CreateELDHistogram(const FunctionCallbackInfo<Value>& args) {
@@ -278,12 +277,6 @@ void CreateELDHistogram(const FunctionCallbackInfo<Value>& args) {
278277
args.GetReturnValue().Set(histogram->object());
279278
}
280279

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-
287280
void MarkBootstrapComplete(const FunctionCallbackInfo<Value>& args) {
288281
Realm* realm = Realm::GetCurrent(args);
289282
CHECK_EQ(realm->kind(), Realm::Kind::kPrincipal);
@@ -307,6 +300,23 @@ static void SlowPerformanceNow(const FunctionCallbackInfo<Value>& args) {
307300
static v8::CFunction fast_performance_now(
308301
v8::CFunction::Make(FastPerformanceNow));
309302

303+
static double GetTimeOriginTimeStampImpl() {
304+
return static_cast<double>(performance_process_start_timestamp) /
305+
MICROS_PER_MILLIS;
306+
}
307+
308+
static double FastGetTimeOriginTimeStamp(v8::Local<v8::Value> receiver) {
309+
return GetTimeOriginTimeStampImpl();
310+
}
311+
312+
static void SlowGetTimeOriginTimeStamp(
313+
const FunctionCallbackInfo<Value>& args) {
314+
args.GetReturnValue().Set(GetTimeOriginTimeStampImpl());
315+
}
316+
317+
static v8::CFunction fast_get_timeorigin_timestamp(
318+
v8::CFunction::Make(FastGetTimeOriginTimeStamp));
319+
310320
static void CreatePerIsolateProperties(IsolateData* isolate_data,
311321
Local<ObjectTemplate> target) {
312322
Isolate* isolate = isolate_data->isolate();
@@ -324,11 +334,15 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
324334
RemoveGarbageCollectionTracking);
325335
SetMethod(isolate, target, "notify", Notify);
326336
SetMethod(isolate, target, "loopIdleTime", LoopIdleTime);
327-
SetMethod(isolate, target, "getTimeOriginTimestamp", GetTimeOriginTimeStamp);
328337
SetMethod(isolate, target, "createELDHistogram", CreateELDHistogram);
329338
SetMethod(isolate, target, "markBootstrapComplete", MarkBootstrapComplete);
330339
SetFastMethodNoSideEffect(
331340
isolate, target, "now", SlowPerformanceNow, &fast_performance_now);
341+
SetFastMethodNoSideEffect(isolate,
342+
target,
343+
"getTimeOriginTimestamp",
344+
SlowGetTimeOriginTimeStamp,
345+
&fast_get_timeorigin_timestamp);
332346
}
333347

334348
void CreatePerContextProperties(Local<Object> target,
@@ -391,12 +405,14 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
391405
registry->Register(RemoveGarbageCollectionTracking);
392406
registry->Register(Notify);
393407
registry->Register(LoopIdleTime);
394-
registry->Register(GetTimeOriginTimeStamp);
395408
registry->Register(CreateELDHistogram);
396409
registry->Register(MarkBootstrapComplete);
397410
registry->Register(SlowPerformanceNow);
398411
registry->Register(FastPerformanceNow);
399412
registry->Register(fast_performance_now.GetTypeInfo());
413+
registry->Register(SlowGetTimeOriginTimeStamp);
414+
registry->Register(FastGetTimeOriginTimeStamp);
415+
registry->Register(fast_get_timeorigin_timestamp.GetTypeInfo());
400416
HistogramBase::RegisterExternalReferences(registry);
401417
IntervalHistogram::RegisterExternalReferences(registry);
402418
}

0 commit comments

Comments
 (0)