Skip to content

lib: use indexed append in ZIP central directory parser - #67456

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
rozzilla:lib-indexed-array-appends
Nov 24, 2026
Merged

nodejs-github-bot merged 1 commit into
nodejs:mainfrom
rozzilla:lib-indexed-array-appends

Conversation

@rozzilla

@rozzilla rozzilla commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

readCentralDirectory() appends one entry per archive file with
ArrayPrototypePush, which doc/contributing/primordials.md lists among
primordials with known performance issues. This uses an indexed assignment
instead. Behavior and errors are unchanged.

The scope is narrowed to this one loop, which runs once per archive entry.

Local comparison of the header parsing loop (10 rounds, 3 runs each,
Node v28.0.0-pre, V8 15.8.62.44-node.4, macOS arm64):

entries push to indexed
10 +8% to +12%
100 +8% to +12%
1,000 +10% to +13%

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/crypto
  • @nodejs/http
  • @nodejs/loaders
  • @nodejs/net
  • @nodejs/security-wg

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. labels Nov 13, 2026
@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.53%. Comparing base (3cac7b9) to head (b84f222).
⚠️ Report is 27 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67456      +/-   ##
==========================================
- Coverage   90.55%   90.53%   -0.02%     
==========================================
  Files         801      803       +2     
  Lines      284215   284978     +763     
  Branches    54883    54987     +104     
==========================================
+ Hits       257358   258001     +643     
- Misses      17014    17144     +130     
+ Partials     9843     9833      -10     
Files with missing lines Coverage Δ
lib/internal/zip/headers.js 97.74% <100.00%> (-0.01%) ⬇️

... and 39 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.

@gurgunday gurgunday left a comment

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.

lgtm

@bakkot

bakkot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Calls are left unchanged when the return value is used, they push multiple values or spreads, the target is not a simple identifier or member, the value expression could run user code (calls, await, assignments), or the target is a publicly reachable array (for example module.children, domain.members, repl.lines).

Other than the last restriction those seem like something that could be determined by a lint rule. What's the purpose of the "not if the target is a publicly reachable array" restriction?

@Renegade334

Copy link
Copy Markdown
Member

If this approach doesn't offer any additional protection against Array.prototype mutation and there's no performance advantage, then what's the benefit of this change?

@gurgunday

gurgunday commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

If this approach doesn't offer any additional protection against Array.prototype mutation and there's no performance advantage, then what's the benefit of this change?

It should be faster than Primordial ArrayPush– it was the case in every scenario I tested 1-to-1. These general benchmarks in node might not always show the improvement if ArrayPush is already within noise level compared to what the rest of the tested functionality is spending its time on

@gurgunday

Copy link
Copy Markdown
Member

In any case, it's a free way to remove some primordials, which I'm already +1 on

@gurgunday gurgunday added request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. author ready PRs with CI started, the required approvals, and no outstanding review comments. labels Nov 14, 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 Nov 14, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@LiviaMedeiros LiviaMedeiros left a comment

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.

In its current form, IMHO this change hurts readability and consistency of the code much more than it benefits.
I see two ways of moving forward with it:

  1. Combining with other similar changes (e.g. replacing .pop() with .length-- whenever we don't need returned value) and applying strictly everywhere. Most likely by enforcing with lint rule. Ideally with benchmark. Still -0 on it.
  2. Split this into more scoped change(s), focusing only on performance-critical paths where we really don't want primordials.

Comment on lines 574 to 578
} else if (ch === '{') {
ArrayPrototypePush(stack, '{');
stack[stack.length] = '{';
} else if (ch === '}' && top === '{') {
ArrayPrototypePop(stack);
}

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.

Code like this starts looking strange. If we do this, we probably should replace Array.prototype.pop(array) with array.length-- and apply both changes atomically.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, it's asymmetric. I'll do it here or in a follow-up PR; let me know what you think is better @LiviaMedeiros.

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.

I think, neither, unless there is some trick that makes it faster in current v8 versions. I expect length-- to be even slower than primordial.

Comment thread lib/internal/test_runner/runner.js Resolved
Comment thread lib/internal/test_runner/runner.js Resolved
Comment thread lib/internal/vfs/providers/sea.js Resolved
Comment thread lib/internal/zip/file.js Resolved
Comment thread lib/domain.js Resolved
@gurgunday gurgunday removed the author ready PRs with CI started, the required approvals, and no outstanding review comments. label Nov 15, 2026
@rozzilla

Copy link
Copy Markdown
Contributor Author

Other than the last restriction those seem like something that could be determined by a lint rule. What's the purpose of the "not if the target is a publicly reachable array" restriction?

@bakkot if users can reach the array, they can replace or reshape it, and then ArrayPrototypePush(arr, v) and arr[arr.length] = v behave differently. Two examples: a plain object without length (push sets length to 1, while the index store writes to obj["undefined"]), and an array with a non-writable length. I kept those call sites as they are, so this PR changes no "user-visible behavior". The other restrictions could be a lint rule. This one depends on whether the array escapes, which needs a human to judge.

@rozzilla

Copy link
Copy Markdown
Contributor Author

@Renegade334, it doesn't add protection, and I'm not claiming a measurable macro-benchmark win. The benefit is following doc/contributing/primordials.md, which lists ArrayPrototypePush under primordials with known performance issues. A plain indexed store also skips the uncurryThis wrapper call. As @gurgunday said, that's faster in isolation but lost in the noise in the existing end-to-end benchmarks. I can add a microbenchmark if that would help.

@LiviaMedeiros

Copy link
Copy Markdown
Member

@rozzilla Thanks for addressing the adjustments, but there are more of these and it doesn't change the big picture here.

The Usage of primordials in core guide explicitly mentions that not using primordials is preferable in specific modules where we care about pure performance more than anything, and that primordials with performance issues (such as ArrayPrototypePush) are discouraged in a hot code paths.

For indexed appends scoped only to performance-critical paths or just in tight high-trip-count loops i have no objections. (I think ideally we should have a table with v8-version-specific perf quirks and microbenchmarks so nobody has to dig through PRs to see why it looks like this and/or check if it's still applicable with newer v8, but it's a separate maintenance burden out of scope for this PR)

However when applied like this on lib scale, i wouldn't call this free way of getting rid of primordial or gaining performance boost: it comes with a cost of readability and consistency. Any of these changes can be reverted in a future "lib: use idiomatic push" PR which would IMHO look more valid as it makes code prettier without noticeable performance impact.

@rozzilla

Copy link
Copy Markdown
Contributor Author

@rozzilla Thanks for addressing the adjustments, but there are more of these and it doesn't change the big picture here.

The Usage of primordials in core guide explicitly mentions that not using primordials is preferable in specific modules where we care about pure performance more than anything, and that primordials with performance issues (such as ArrayPrototypePush) are discouraged in a hot code paths.

For indexed appends scoped only to performance-critical paths or just in tight high-trip-count loops i have no objections. (I think ideally we should have a table with v8-version-specific perf quirks and microbenchmarks so nobody has to dig through PRs to see why it looks like this and/or check if it's still applicable with newer v8, but it's a separate maintenance burden out of scope for this PR)

However when applied like this on lib scale, i wouldn't call this free way of getting rid of primordial or gaining performance boost: it comes with a cost of readability and consistency. Any of these changes can be reverted in a future "lib: use idiomatic push" PR which would IMHO look more valid as it makes code prettier without noticeable performance impact.

@LiviaMedeiros, how do you recommend proceeding with this PR?

@LiviaMedeiros

Copy link
Copy Markdown
Member

I'd recommend that if there are changes that either:

  • Give measurable performance boost in a benchmark
    • Small boost (let's say, at least +1%) is welcome but obviously it shouldn't be just microbenchmark for push itself :)
  • Are a part of tight loops in happy path
    • Let's say, something that typically loops 100+ times and/or might loop a lot. For example, code paths in fs that [recursively] walk through directories.

...then we can keep these changes and merge them, and drop the others.

Might also be worth exploring same change but for opposite purpose: we can protect code that intentionally doesn't use primordials from Array.prototype.push mutations by replacing user-mutable .push() with indexed assignments. IIRC .push() is still slightly faster so performance considerations might be flipped here. Obviously this would still come with readability cost.

readCentralDirectory() appends one CentralFileHeader per archive entry
with ArrayPrototypePush, which is listed among primordials with known
performance issues. Assign by the existing loop index instead. The
loop runs once per entry, so its trip count grows with archive size.

The change is limited to this loop. Validation and error behavior are
unchanged.

Assisted-by: Claude Code
Signed-off-by: Roberto Bianchi <roberto.bianchi@spendesk.com>
@rozzilla rozzilla changed the title lib: use indexed appends instead of ArrayPrototypePush lib: use indexed append in ZIP central directory parser Nov 23, 2026
@rozzilla
rozzilla force-pushed the lib-indexed-array-appends branch from 0c8c15a to b84f222 Compare November 23, 2026 17:01
@rozzilla

Copy link
Copy Markdown
Contributor Author

@LiviaMedeiros I dropped everything except the loop in readCentralDirectory() (lib/internal/zip/headers.js), which runs once per archive entry, so archives with hundreds or thousands of entries go through it that many times on the normal path.

There's no existing ZIP benchmark, so I compared the two versions of the full header-parsing loop locally, measuring it with 10, 100, and 1,000 entries. The indexed version was about 8–13% faster in each of 3 repeated runs. The numbers are in the PR description.

I can add the script under benchmark/ if that would help, and I'd leave protecting the non-primordial .push() calls for a separate PR.

@LiviaMedeiros LiviaMedeiros left a comment

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.

LGTM!

@LiviaMedeiros LiviaMedeiros added zlib Issues and PRs related to the zlib module and its compression dependencies. author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. labels Nov 24, 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 Nov 24, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@gurgunday gurgunday added the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 24, 2026
@nodejs-github-bot
nodejs-github-bot merged commit a337566 into nodejs:main Nov 24, 2026
72 checks passed
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in a337566

@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. zlib Issues and PRs related to the zlib module and its compression dependencies.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants