Skip to content

Commit

Permalink
perf_hooks: reduce overhead of new performance_entries
Browse files Browse the repository at this point in the history
PR-URL: #49803
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Stephen Belanger <[email protected]>
  • Loading branch information
H4ad authored Sep 27, 2023
1 parent fef7927 commit a6ad048
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
36 changes: 36 additions & 0 deletions benchmark/perf_hooks/timerfied.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const assert = require('assert');
const common = require('../common.js');

const {
PerformanceObserver,
performance,
} = require('perf_hooks');

function randomFn() {
return Math.random();
}

const bench = common.createBenchmark(main, {
n: [1e5],
observe: ['function'],
});

let _result;

function main({ n, observe }) {
const obs = new PerformanceObserver(() => {
bench.end(n);
});
obs.observe({ entryTypes: [observe], buffered: true });

const timerfied = performance.timerify(randomFn);

bench.start();
for (let i = 0; i < n; i++)
_result = timerfied();

// Avoid V8 deadcode (elimination)
assert.ok(_result);
}
26 changes: 16 additions & 10 deletions lib/internal/perf/performance_entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const {
ObjectDefineProperties,
ReflectConstruct,
Symbol,
} = primordials;

Expand All @@ -25,14 +24,17 @@ const kEntryType = Symbol('PerformanceEntry.EntryType');
const kStartTime = Symbol('PerformanceEntry.StartTime');
const kDuration = Symbol('PerformanceEntry.Duration');
const kDetail = Symbol('NodePerformanceEntry.Detail');
const kSkipThrow = Symbol('kSkipThrow');

function isPerformanceEntry(obj) {
return obj?.[kName] !== undefined;
}

class PerformanceEntry {
constructor() {
throw new ERR_ILLEGAL_CONSTRUCTOR();
constructor(skipThrowSymbol = undefined) {
if (skipThrowSymbol !== kSkipThrow) {
throw new ERR_ILLEGAL_CONSTRUCTOR();
}
}

get name() {
Expand Down Expand Up @@ -92,9 +94,11 @@ function initPerformanceEntry(entry, name, type, start, duration) {
}

function createPerformanceEntry(name, type, start, duration) {
return ReflectConstruct(function PerformanceEntry() {
initPerformanceEntry(this, name, type, start, duration);
}, [], PerformanceEntry);
const entry = new PerformanceEntry(kSkipThrow);

initPerformanceEntry(entry, name, type, start, duration);

return entry;
}

/**
Expand All @@ -119,10 +123,12 @@ class PerformanceNodeEntry extends PerformanceEntry {
}

function createPerformanceNodeEntry(name, type, start, duration, detail) {
return ReflectConstruct(function PerformanceNodeEntry() {
initPerformanceEntry(this, name, type, start, duration);
this[kDetail] = detail;
}, [], PerformanceNodeEntry);
const entry = new PerformanceNodeEntry(kSkipThrow);

initPerformanceEntry(entry, name, type, start, duration);
entry[kDetail] = detail;

return entry;
}

module.exports = {
Expand Down

0 comments on commit a6ad048

Please sign in to comment.