Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf_hooks: reducing overhead of performance observer entry list #50008

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions benchmark/perf_hooks/performance-observer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'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],
pending: [1, 10],
}, {
options: ['--expose-internals'],
});

let _result;

function fillQueue(timerfied, pending) {
for (let i = 0; i < pending; i++) {
_result = timerfied();
}
// Avoid V8 deadcode (elimination)
assert.ok(_result);
}

function main({ n, pending }) {
const timerfied = performance.timerify(randomFn);

let count = 0;
const obs = new PerformanceObserver((entries) => {
count += entries.getEntries().length;

if (count >= n) {
bench.end(count);
} else {
fillQueue(timerfied, pending);
}
});
obs.observe({ entryTypes: ['function'], buffered: true });

bench.start();
fillQueue(timerfied, pending);
}
31 changes: 15 additions & 16 deletions lib/internal/perf/observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const {
ObjectDefineProperties,
ObjectFreeze,
ObjectKeys,
ReflectConstruct,
SafeMap,
SafeSet,
Symbol,
Expand Down Expand Up @@ -171,9 +170,18 @@ function maybeIncrementObserverCount(type) {
}
}

const kSkipThrow = Symbol('kSkipThrow');
const performanceObserverSorter = (first, second) => {
return first.startTime - second.startTime;
};

class PerformanceObserverEntryList {
constructor() {
throw new ERR_ILLEGAL_CONSTRUCTOR();
constructor(skipThrowSymbol = undefined, entries = []) {
if (skipThrowSymbol !== kSkipThrow) {
throw new ERR_ILLEGAL_CONSTRUCTOR();
}

this[kBuffer] = ArrayPrototypeSort(entries, performanceObserverSorter);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it's a common case or not, but it may/may not be worth guarding the sort with a conditional that checks if entries.length > 1 before sorting.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added, I got 8% faster for 1 item but degraded the else path by ~8%, so I didn't think that was worth it since probably this queue will be called with more items than 1.

}

getEntries() {
Expand Down Expand Up @@ -232,14 +240,6 @@ ObjectDefineProperties(PerformanceObserverEntryList.prototype, {
},
});

function createPerformanceObserverEntryList(entries) {
return ReflectConstruct(function PerformanceObserverEntryList() {
this[kBuffer] = ArrayPrototypeSort(entries, (first, second) => {
return first.startTime - second.startTime;
});
}, [], PerformanceObserverEntryList);
}

class PerformanceObserver {
#buffer = [];
#entryTypes = new SafeSet();
Expand Down Expand Up @@ -349,8 +349,9 @@ class PerformanceObserver {
}

[kDispatch]() {
this.#callback(createPerformanceObserverEntryList(this.takeRecords()),
this);
const entryList = new PerformanceObserverEntryList(kSkipThrow, this.takeRecords());

this.#callback(entryList, this);
}

[kInspect](depth, options) {
Expand Down Expand Up @@ -523,9 +524,7 @@ function filterBufferMapByNameAndType(name, type) {
bufferList = ArrayPrototypeSlice(bufferList);
}

return ArrayPrototypeSort(bufferList, (first, second) => {
return first.startTime - second.startTime;
});
return ArrayPrototypeSort(bufferList, performanceObserverSorter);
}

function observerCallback(name, type, startTime, duration, details) {
Expand Down