Skip to content

Fix: stream the file cache as newline-delimited records to avoid RangeError on large collections#2385

Open
terafin wants to merge 1 commit into
emmercm:mainfrom
terafin:fix/cache-ndjson-streaming
Open

Fix: stream the file cache as newline-delimited records to avoid RangeError on large collections#2385
terafin wants to merge 1 commit into
emmercm:mainfrom
terafin:fix/cache-ndjson-streaming

Conversation

@terafin

@terafin terafin commented Jul 16, 2026

Copy link
Copy Markdown

Problem

Cache (src/cache/cache.ts) serializes its entire contents into a single string:

  • save: JSON.stringify(Object.fromEntries(this.keyValues))
  • load: 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:

RangeError: Invalid string length

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-size doesn'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:

  • save: a generator yields one gzipped JSON.stringify(entry) + "\n" at a time; no monolithic string is built.
  • load: the gzipped stream is decoded incrementally with a 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 (an Array.isArray guard 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:

  • round-trips values containing newlines, quotes, and backslashes (a newline-delimited format must not corrupt values that themselves contain newlines);
  • ignores a legacy single-object-format cache file gracefully;
  • asserts the on-disk format is one newline-delimited [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 full test/cache suite pass locally on Node 22.

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.
@github-actions

Copy link
Copy Markdown
Contributor

👋 Welcome

Thank 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.

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