Skip to content

Commit

Permalink
perf_hooks: reduce overhead of new user timings
Browse files Browse the repository at this point in the history
PR-URL: #49914
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
  • Loading branch information
H4ad authored and targos committed Nov 11, 2023
1 parent 17581c2 commit e8f024b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
13 changes: 4 additions & 9 deletions lib/internal/perf/performance_entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ class PerformanceEntry {
throw new ERR_ILLEGAL_CONSTRUCTOR();
}

initPerformanceEntry(this, name, type, start, duration);
this[kName] = name;
this[kEntryType] = type;
this[kStartTime] = start;
this[kDuration] = duration;
}

get name() {
Expand Down Expand Up @@ -94,13 +97,6 @@ ObjectDefineProperties(PerformanceEntry.prototype, {
toJSON: kEnumerableProperty,
});

function initPerformanceEntry(entry, name, type, start, duration) {
entry[kName] = name;
entry[kEntryType] = type;
entry[kStartTime] = start;
entry[kDuration] = duration;
}

function createPerformanceEntry(name, type, start, duration) {
return new PerformanceEntry(kSkipThrow, name, type, start, duration);
}
Expand Down Expand Up @@ -135,7 +131,6 @@ function createPerformanceNodeEntry(name, type, start, duration, detail) {
}

module.exports = {
initPerformanceEntry,
createPerformanceEntry,
PerformanceEntry,
isPerformanceEntry,
Expand Down
47 changes: 28 additions & 19 deletions lib/internal/perf/usertiming.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

const {
ObjectDefineProperties,
ObjectSetPrototypeOf,
SafeMap,
SafeSet,
SafeArrayIterator,
Symbol,
SymbolToStringTag,
ReflectConstruct,
} = primordials;

const { initPerformanceEntry, PerformanceEntry } = require('internal/perf/performance_entry');
const { PerformanceEntry, kSkipThrow } = require('internal/perf/performance_entry');
const { now } = require('internal/perf/utils');
const { enqueue, bufferUserTiming } = require('internal/perf/observe');
const nodeTiming = require('internal/perf/nodetiming');
Expand All @@ -35,7 +33,6 @@ const {

const { structuredClone } = require('internal/structured_clone');
const {
kEmptyObject,
lazyDOMException,
kEnumerableProperty,
} = require('internal/util');
Expand Down Expand Up @@ -69,27 +66,29 @@ function getMark(name) {
return ts;
}

class PerformanceMark {
constructor(name, options = kEmptyObject) {
class PerformanceMark extends PerformanceEntry {
constructor(name, options = undefined) {
if (arguments.length === 0) {
throw new ERR_MISSING_ARGS('name');
}
name = `${name}`;
options ??= kEmptyObject;
if (nodeTimingReadOnlyAttributes.has(name))
throw new ERR_INVALID_ARG_VALUE('name', name);
validateObject(options, 'options');
const startTime = options.startTime ?? now();
if (options != null) {
validateObject(options, 'options');
}
const startTime = options?.startTime ?? now();
validateNumber(startTime, 'startTime');
if (startTime < 0)
throw new ERR_PERFORMANCE_INVALID_TIMESTAMP(startTime);
markTimings.set(name, startTime);

let detail = options.detail;
let detail = options?.detail;
detail = detail != null ?
structuredClone(detail) :
null;
initPerformanceEntry(this, name, 'mark', startTime, 0);

super(kSkipThrow, name, 'mark', startTime, 0);
this[kDetail] = detail;
}

Expand All @@ -108,8 +107,7 @@ class PerformanceMark {
};
}
}
ObjectSetPrototypeOf(PerformanceMark, PerformanceEntry);
ObjectSetPrototypeOf(PerformanceMark.prototype, PerformanceEntry.prototype);

ObjectDefineProperties(PerformanceMark.prototype, {
detail: kEnumerableProperty,
[SymbolToStringTag]: {
Expand All @@ -120,8 +118,18 @@ ObjectDefineProperties(PerformanceMark.prototype, {
});

class PerformanceMeasure extends PerformanceEntry {
constructor() {
throw new ERR_ILLEGAL_CONSTRUCTOR();
constructor(
skipThrowSymbol = undefined,
name = undefined,
type = undefined,
start = undefined,
duration = undefined,
) {
if (skipThrowSymbol !== kSkipThrow) {
throw new ERR_ILLEGAL_CONSTRUCTOR();
}

super(skipThrowSymbol, name, type, start, duration);
}

get detail() {
Expand All @@ -139,10 +147,11 @@ ObjectDefineProperties(PerformanceMeasure.prototype, {
});

function createPerformanceMeasure(name, start, duration, detail) {
return ReflectConstruct(function PerformanceMeasure() {
initPerformanceEntry(this, name, 'measure', start, duration);
this[kDetail] = detail;
}, [], PerformanceMeasure);
const measure = new PerformanceMeasure(kSkipThrow, name, 'measure', start, duration);

measure[kDetail] = detail;

return measure;
}

function mark(name, options) {
Expand Down

0 comments on commit e8f024b

Please sign in to comment.