Avoid O(n^2) entry rescan in pumpEntries (slow for archives with many files) - #91
Open
ArneFeys wants to merge 1 commit into
Open
Avoid O(n^2) entry rescan in pumpEntries (slow for archives with many files)#91ArneFeys wants to merge 1 commit into
ArneFeys wants to merge 1 commit into
Conversation
getFirstNotDoneEntry() restarted its scan at entries[0] on every pump, so building an archive cost O(n^2) over the number of entries and got pathologically slow for archives with many files. Entries are pumped strictly in array order (entry i+1 only starts once entry i reaches FILE_DATA_DONE) and an entry's state never regresses, so the index of the first not-done entry only ever advances. Cache it on the ZipFile and resume the scan from there, making the work O(n) overall. Output is byte-for-byte identical; the existing test suite passes.
ArneFeys
force-pushed
the
perf-first-not-done-entry-index
branch
from
June 3, 2026 15:51
6cf2637 to
c97bafc
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
getFirstNotDoneEntry()(insidepumpEntries) rescansself.entriesfrom index0on every pump.pumpEntriesruns once per entry as each one completes, so building an archive is O(n²) in the number of entries. For archives with many files this becomes pathologically slow.Fix
Entries are pumped strictly in array order — entry
i+1is only started once entryireachesFILE_DATA_DONE— and an entry's state never regresses. So the index of the first not-done entry only ever advances. I cache it on theZipFile(firstNotDoneEntryIndex) and resume the scan from there instead of restarting at0, making the total work O(n).The change is minimal and self-contained; output is byte-for-byte identical and the existing
test/test.jssuite passes unchanged.Benchmark
Adding N tiny
addBuffer(..., { compress: false })entries, measuring total time to the output stream'send(same machine, Node 25):Before scales ~4× per doubling of N (quadratic); after scales ~2× (linear). Output size was identical between the two for every N.