Skip to content

Fix generateAsync being quadratic in the number of files - #978

Open
OsamaAnsar wants to merge 1 commit into
Stuk:mainfrom
OsamaAnsar:perf/generate-sources-queue
Open

OsamaAnsar wants to merge 1 commit into
Stuk:mainfrom
OsamaAnsar:perf/generate-sources-queue

Conversation

@OsamaAnsar

Copy link
Copy Markdown

What was wrong

ZipFileWorker keeps the files still to be written in this._sources and takes the next one with this._sources.shift() (prepareNextSource). Array#shift is O(n) once the array is larger than about 16k elements, so generateAsync() / generateInternalStream() get quadratic in the number of files.

Measurements (my runs, Node v24.19.0, Windows)

Each run adds N files ("x" content, fixed date) and times await zip.generateAsync({type: "uint8array"}); three runs per build, all values in ms:

files master (609d95f) this branch
10000 266, 249, 266 278, 250, 271
20000 937, 1137, 1063 409, 466, 464
40000 3528, 3454, 3983 720, 844, 839
80000 13104, 13200, 11930 1791, 2056, 1828

Below the threshold there is no difference (10000 files), above it the time now grows roughly linearly instead of quadratically. These are wall-clock numbers from one machine and will vary; the unit tests below deliberately do not assert on time.

Fix

this._sources is now consumed through this._sourcesStart instead of shift(): prepareNextSource reads _sources[_sourcesStart], sets that slot to null (so the source can be collected) and increments the index. Every place that used the length of the list now accounts for the start index: push (remainingFiles, used for the progress percentage), the end handler, resume, and the loops in error and lock (which only touch the sources that have not been picked up yet, as before).

Output is unchanged

I compared this branch with master for 0, 1, 3, 50 and 200 files, each with streamFiles false and true and with STORE and DEFLATE (20 configurations, fixed file dates, a zip comment set). For each I took the SHA-1 of the resulting zip and a SHA-1 of every progress callback's percent and currentFile. All 20 lines are identical between master and this branch (for example 3 files, streamFiles: true, DEFLATE gives the same zip SHA-1 and the same number of progress calls on both).

Tests

Added to test/asserts/generate.js:

  • generateAsync with many files keeps the files, their order and the progress: 3000 files; checks that the progress never goes backward and ends at 100%, that files are reported in the order they were added, that the written zip lists them in that order, and that the last file's content is right. (This passes on master too; it guards the behaviour.)
  • generateAsync does not shift the list of sources: temporarily wraps Array.prototype.shift and asserts it is never called on an array with at least 1500 elements while 3000 files are written. No wall-clock assertion.

Results from my runs: npm run test-node was 195 pass / 0 fail before and is 197 pass / 0 fail after; npm run lint is clean. I did not run the browser tests (npm run test-browser needs SauceLabs/Playwright), and dist/ is not touched.

Revert check: with only lib/generate/ZipFileWorker.js reverted to master (confirmed by an empty git diff -- lib and no _sourcesStart in the file) and the new tests kept, generateAsync does not shift the list of sources fails (196 pass / 1 fail) and everything else passes. Restoring the fix gives 197 pass / 0 fail again.

ZipFileWorker consumed its list of sources with Array#shift. Once the
array grows past roughly 16k elements V8 can no longer shift it in
constant time, so generating an archive with tens of thousands of files
spent most of its time moving the remaining sources around.

Consume the list through a start index instead and release each source
once it has been picked up. The places that depended on the length of the
list (push, resume, the end handler, error and lock) now account for the
start index. The produced archive and the progress metadata are unchanged.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant