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: refactor to avoid unsafe array iteration #36723

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/perf_hooks.js
Expand Up @@ -3,6 +3,7 @@
const {
ArrayIsArray,
ArrayPrototypeFilter,
ArrayPrototypeForEach,
ArrayPrototypeIncludes,
ArrayPrototypeMap,
ArrayPrototypePush,
Expand Down Expand Up @@ -375,13 +376,13 @@ class PerformanceObserver {
disconnect() {
const observerCountsGC = observerCounts[NODE_PERFORMANCE_ENTRY_TYPE_GC];
const types = this[kTypes];
for (const key of ObjectKeys(types)) {
ArrayPrototypeForEach(ObjectKeys(types), (key) => {
const item = types[key];
if (item) {
L.remove(item);
observerCounts[key]--;
}
}
});
this[kTypes] = {};
if (observerCountsGC === 1 &&
observerCounts[NODE_PERFORMANCE_ENTRY_TYPE_GC] === 0) {
Expand All @@ -408,14 +409,14 @@ class PerformanceObserver {
this[kBuffer][kEntries] = [];
L.init(this[kBuffer][kEntries]);
this[kBuffering] = Boolean(options.buffered);
for (const entryType of filteredEntryTypes) {
ArrayPrototypeForEach(filteredEntryTypes, (entryType) => {
const list = getObserversList(entryType);
if (this[kTypes][entryType]) continue;
if (this[kTypes][entryType]) return;
const item = { obs: this };
this[kTypes][entryType] = item;
L.append(list, item);
observerCounts[entryType]++;
}
});
if (observerCountsGC === 0 &&
observerCounts[NODE_PERFORMANCE_ENTRY_TYPE_GC] === 1) {
installGarbageCollectionTracking();
Expand Down Expand Up @@ -640,6 +641,7 @@ function sortedInsert(list, entry) {
}

class ELDHistogram extends Histogram {
constructor(i) { super(i); } // eslint-disable-line no-useless-constructor
Copy link
Member

Choose a reason for hiding this comment

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

I know this has been discussed elsewhere but I can't remember the reason we're doing this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that's a bug in the ECMAScript spec, it should be fixed by tc39/ecma262#2216.

Relevant spec section: https://tc39.es/ecma262/#sec-runtime-semantics-classdefinitionevaluation

If constructor is empty, then

If ClassHeritageopt is present, then

Let constructorText be the source text

constructor(...args) { super(...args); }

enable() { return this[kHandle].enable(); }
disable() { return this[kHandle].disable(); }
}
Expand Down