Skip to content

http: avoid mutable push on private arrays - #67885

Open
rozzilla wants to merge 2 commits into
nodejs:mainfrom
rozzilla:http-private-arrays-no-push
Open

rozzilla wants to merge 2 commits into
nodejs:mainfrom
rozzilla:http-private-arrays-no-push

Conversation

@rozzilla

@rozzilla rozzilla commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

The server's per-connection incoming/outgoing queues, the uncork() and end() callback lists in OutgoingMessage, and the socket sets in Agent.prototype.removeSocket() used .push() on private arrays. If user code replaced Array.prototype.push, these paths went through it. This uses indexed assignments instead.

lib/internal/http/*.js must not use prototype primordials, the same as lib/zlib.js, so this follows #67748. Private arrays only, no performance claim.

The server's per-connection request and response queues, the uncork()
and end() callback lists in OutgoingMessage, and the socket sets in
Agent.prototype.removeSocket() used .push() on private arrays, which
looks up Array.prototype.push at call time. These files do not use
prototype primordials, so replacing that method from user code changed
how they ran. Use indexed assignments instead.

Signed-off-by: Roberto Bianchi <roberto.bianchi@spendesk.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added http Issues and PRs related to the http subsystem. needs-ci PRs that need a full CI run. labels Nov 30, 2026
@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 90.57%. Comparing base (1611823) to head (4b458f4).
⚠️ Report is 68 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67885      +/-   ##
==========================================
- Coverage   90.58%   90.57%   -0.01%     
==========================================
  Files         802      802              
  Lines      286656   287048     +392     
  Branches    55369    55431      +62     
==========================================
+ Hits       259668   260004     +336     
- Misses      17089    17120      +31     
- Partials     9899     9924      +25     
Files with missing lines Coverage Ξ”
lib/internal/http/agent.js 96.88% <100.00%> (+0.01%) ⬆️
lib/internal/http/outgoing.js 98.04% <100.00%> (+0.01%) ⬆️
lib/internal/http/server.js 96.70% <100.00%> (+0.24%) ⬆️

... and 52 files with indirect coverage changes

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Ethan-Arrowood Ethan-Arrowood left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Unsure if you deliberately omitted these other two calls since they have multiple arguments. Going to approve for now but will wait on requesting CI until you confirm (or push another commit and then I'll reapprove and request CI).

Comment thread lib/internal/http/outgoing.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still some push calls in here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

these should probably use ArrayPrototypePush even if they can't be converted to the length syntax

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we avoid those in http files for performance reasons.

Comment thread lib/internal/http/outgoing.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Here too.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

same here

Comment thread lib/internal/http/outgoing.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

these should probably use ArrayPrototypePush even if they can't be converted to the length syntax

Comment thread lib/internal/http/outgoing.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

same here

@Ethan-Arrowood Ethan-Arrowood added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Dec 3, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Dec 3, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@rozzilla

rozzilla commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Unsure if you deliberately omitted these other two calls since they have multiple arguments. Going to approve for now but will wait on requesting CI until you confirm (or push another commit and then I'll reapprove and request CI).

@Ethan-Arrowood yes, that's deliberate. Those two calls push three values at once, so each would become:

const buf = msg[kChunkedBuffer];
const i = buf.length;
buf[i] = chunk;
buf[i + 1] = encoding;
buf[i + 2] = callback;

That's a lot of noise on the corked write path for little gain, and ArrayPrototypePush isn't allowed in lib/internal/http/*.js. Happy to change them if you prefer.

@Ethan-Arrowood

Copy link
Copy Markdown
Contributor

Hm... the principle of this PR (and the others) is that Array.prototype.push is unsafe. And I think the basic change of arr.push(item) to arr[arr.length] = item is already "noisey" so I'm fine with what you've proposed. It should be just as fast. And honestly I'd even be okay with the even noisier:

msg[kChunkedBuffer][msg[kChunkedBuffer].length] = chunk;
msg[kChunkedBuffer][msg[kChunkedBuffer].length] = encoding;
msg[kChunkedBuffer][msg[kChunkedBuffer].length] = callback;

So we avoid any declarations. Performance-sensitive, secure code is not going to be pretty lol

Signed-off-by: Roberto Bianchi <roberto.bianchi@spendesk.com>
@rozzilla

rozzilla commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

@Ethan-Arrowood updated on 4b458f4 πŸ‘πŸΌ
I used the initial version I proposed, mainly to read .length only once rather than 3 times. Let me know if you are fine with that, thanks πŸ™πŸΌ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

http Issues and PRs related to the http subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants