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: fix rangeerror #54772

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions doc/contributing/primordials.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ performance of code in Node.js.

* Methods that mutate the internal state of arrays:
* `ArrayPrototypePush`
* `ArrayPrototypePushApply`: also fails with a RangeError on large arrays
Copy link
Contributor

Choose a reason for hiding this comment

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

That’s not the correct place to document, as this is not a performance issue IIUC

* `ArrayPrototypePop`
* `ArrayPrototypeShift`
* `ArrayPrototypeUnshift`
Expand Down
17 changes: 12 additions & 5 deletions lib/internal/perf/observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const {
ArrayPrototypeFilter,
ArrayPrototypeIncludes,
ArrayPrototypePush,
ArrayPrototypePushApply,
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated to this PR: Can we eventually add an eslint rule to avoid using this?

Copy link
Contributor

Choose a reason for hiding this comment

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

What would be the rational for such a rule?

Copy link
Member

Choose a reason for hiding this comment

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

V8 has a hard limit on function argument count and without explicitly checking the number, it is easy to make a mistake?

Copy link
Member

Choose a reason for hiding this comment

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

Depending on use it can also be significantly faster. In undici we batch the .apply calls in our data url parser for performance: https://github.com/nodejs/undici/blob/89a46dd54f7d7db7513c435fac8042769ee9e9b5/lib/web/fetch/data-url.js#L657-L674

ArrayPrototypeSlice,
ArrayPrototypeSort,
Error,
Expand Down Expand Up @@ -307,7 +306,9 @@ class PerformanceObserver {
maybeIncrementObserverCount(type);
if (buffered) {
const entries = filterBufferMapByNameAndType(undefined, type);
ArrayPrototypePushApply(this.#buffer, entries);
for (let i = 0; i < entries.length; i++) {
ArrayPrototypePush(this.#buffer, entries[i]);
}
Copy link
Member

Choose a reason for hiding this comment

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

I understand the necessary fix but we should benchmark this to make sure it's not too much of a regression. Might make sense to split entries up and still use ...PushApply with smaller, safer chunks

kPending.add(this);
if (kPending.size)
queuePending();
Expand Down Expand Up @@ -514,9 +515,15 @@ function filterBufferMapByNameAndType(name, type) {
return [];
} else {
bufferList = [];
ArrayPrototypePushApply(bufferList, markEntryBuffer);
ArrayPrototypePushApply(bufferList, measureEntryBuffer);
ArrayPrototypePushApply(bufferList, resourceTimingBuffer);
for (let i = 0; i < markEntryBuffer.length; i++) {
ArrayPrototypePush(bufferList, markEntryBuffer[i]);
}
for (let i = 0; i < measureEntryBuffer.length; i++) {
ArrayPrototypePush(bufferList, measureEntryBuffer[i]);
}
for (let i = 0; i < resourceTimingBuffer.length; i++) {
ArrayPrototypePush(bufferList, resourceTimingBuffer[i]);
}
}
if (name !== undefined) {
bufferList = ArrayPrototypeFilter(bufferList, (buffer) => buffer.name === name);
Expand Down
Loading