Repository navigation
lib: use indexed append in ZIP central directory parser - #67456
Conversation
|
Review requested:
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
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? |
|
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 |
|
In any case, it's a free way to remove some primordials, which I'm already +1 on |
LiviaMedeiros
left a comment
There was a problem hiding this comment.
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:
- 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. - Split this into more scoped change(s), focusing only on performance-critical paths where we really don't want primordials.
| } else if (ch === '{') { | ||
| ArrayPrototypePush(stack, '{'); | ||
| stack[stack.length] = '{'; | ||
| } else if (ch === '}' && top === '{') { | ||
| ArrayPrototypePop(stack); | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Agreed, it's asymmetric. I'll do it here or in a follow-up PR; let me know what you think is better @LiviaMedeiros.
There was a problem hiding this comment.
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.
@bakkot if users can reach the array, they can replace or reshape it, and then |
|
@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 |
|
@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 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 |
@LiviaMedeiros, how do you recommend proceeding with this PR? |
|
I'd recommend that if there are changes that either:
...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 |
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>
0c8c15a to
b84f222
Compare
|
@LiviaMedeiros I dropped everything except the loop in readCentralDirectory() ( 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 |
|
Landed in a337566 |
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> PR-URL: #67456 Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
readCentralDirectory()appends one entry per archive file withArrayPrototypePush, whichdoc/contributing/primordials.mdlists amongprimordials 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):