Fix: stream the file cache as newline-delimited records to avoid RangeError on large collections#2385
Open
terafin wants to merge 1 commit into
Open
Fix: stream the file cache as newline-delimited records to avoid RangeError on large collections#2385terafin wants to merge 1 commit into
terafin wants to merge 1 commit into
Conversation
The file cache serialized its entire contents with a single JSON.stringify() on save, and reassembled the whole file into one string with Buffer.concat().toString() on load. Once the serialized cache exceeds V8's maximum string length (~512 MiB) — which happens on very large collections (hundreds of thousands of files) — both throw `RangeError: Invalid string length`, forcing `--disable-cache` and losing all cross-run cache reuse. Raising --max-old-space-size does not help; this is a string-length limit, not a heap limit. Serialize one newline-delimited [key, value] record per entry and stream them through gzip on save, and parse the gzipped stream incrementally with a StringDecoder on load, so no single string is ever built from the whole cache. Cache files written as a single JSON object are not parseable as records and are ignored gracefully, so those caches simply rebuild.
Contributor
👋 WelcomeThank you for your first pull request, @terafin! If you haven't yet, please familiarize yourself with Igir's contribution guidelines. Some GitHub Actions CI workflows may not automatically run for you due to GitHub's security best practices, so a maintainer may need to manually approve the workflows to run. As a result, it is important to make sure tests pass locally before submitting a pull request to help ensure a fast review. Thank you! Comment generated by the GitHub First Interaction workflow. |
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
Cache(src/cache/cache.ts) serializes its entire contents into a single string:JSON.stringify(Object.fromEntries(this.keyValues))JSON.parse(Buffer.concat(chunks).toString('utf8'))Once that string would exceed V8's maximum string length (
require('node:buffer').constants.MAX_STRING_LENGTH, ~512 MiB), both throw:This is hit on very large collections — I ran into it on a ~700,000-file collection, where the serialized cache is over 512 MiB. It is not an out-of-memory condition: raising
--max-old-space-sizedoesn't help, because it's a string-length limit, not a heap limit. The only workaround today is--disable-cache, which throws away all cross-run cache reuse on exactly the large collections that benefit from it most. v5.4.0's gzip + streamed file I/O reduced the on-disk/heap footprint but still builds the one monolithic string in memory, so the crash remains.Fix
Serialize the cache as newline-delimited JSON records — one
[key, value]array per line — and stream them:JSON.stringify(entry) + "\n"at a time; no monolithic string is built.StringDecoder, split on\n, and each line parsed individually.No single string is ever built from the whole cache, so the size ceiling is now per-record rather than per-cache.
save()'s existing write-validation (reload the temp file, compare entry counts) still holds because load and save use the same format.Backward compatibility
A cache file written in the previous single-object format is not parseable as newline-delimited records;
load()ignores it gracefully (anArray.isArrayguard skips non-record lines) and the cache simply rebuilds on the next run. No migration or version bump needed.Tests
Added to
test/cache/cache.test.ts:[key, value]record per entry, and round-trips back — this is the property that keeps any single string small.I deliberately kept a literal ">512 MiB" reproduction out of the committed tests: reproducing the crash needs ~1 GiB of resident data and tens of seconds, which would be a heavy/slow CI addition. The format-invariant test proves the mechanism (one record per line, never one giant string) deterministically and cheaply.
npm run build,eslint,tsc --noEmit, and the fulltest/cachesuite pass locally on Node 22.