Skip to content

perf: reduce large-file memory amplification - #2967

Merged
chaliy merged 4 commits into
mainfrom
perf/large-file-memory
Nov 22, 2026
Merged

chaliy merged 4 commits into
mainfrom
perf/large-file-memory

Conversation

@chaliy

@chaliy chaliy commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

What changed

Large-file commands retain fewer full-size buffers. wc computes only requested counts from raw bytes; jq parses ordinary inputs on demand and generates --stream events directly from tokens; curl moves response bodies into output. Native curl/wget collection reserves at most 1 MiB for known response lengths.

Add just stress-files 256 and five allocation regression checks. Preserve byte integrity, write-out metadata, partial-error output, input locations, raw sequence input, resource limits, and recovery after rejection. Source text and shared shell I/O remain buffered.

Why

A 256 MiB JSONL file required approximately 2.9 GiB of extra heap for input reduction or --stream; full response copies and unrequested text statistics also amplified memory use.

Before / After

Conservative peak extra heap for 256 MiB fixtures, excluding preloaded fixtures and persistent state. Successful cases explicitly raise resource limits. HTTP measurements use loopback with content-length and chunked framing.

Workload Before After
jq input reduction / --stream 2,964 MiB 256 MiB
jq slurp 5,470 MiB 3,286 MiB
curl, known length 992 MiB 512 MiB
wget, known length 752 MiB 512 MiB
wc -c through input redirection 1,920 MiB 1,600 MiB

Direct wc -c FILE allocation regression: approximately 11 MiB -> 1 MiB for a 1 MiB invalid-UTF-8 file. All 140 workload runs and 35 boundary checks passed across 1, 8, 32, 64, and 256 MiB fixtures.

Validation after rebasing on current main:

  • just pre-pr, including formatting, default Clippy, repository checks, and cargo-vet.
  • just test: 3,993 core unit tests, 2,706 integration tests, six large-file stress/allocation tests, the remaining workspace and documentation tests, and all 19 failpoint tests.
  • cargo clippy --all-targets --all-features -- -D warnings.
  • CLI smoke tests compare wc, JSONL reduction, token-stream output, and raw sequence input against native Bash/jq.
  • Saved full parallel Criterion results and a 96-case Bash/Bashkit benchmark with zero errors and 100% output matches; refreshed site benchmark history. The documented Node data generator was used directly because pnpm attempted to write outside the workspace.

The final review also covers all scalar control-character boundaries: only SP, HT, LF, and CR delimit JSON values; invalid controls fail before emitting partial scalar output.

Risk

  • Medium: jq input scheduling and token parsing change; regressions compare events and metadata with real jq, including malformed prefixes, incomplete containers, depth limits, and unread malformed tails.
  • Download response and VFS size limits remain enforced; over-cap responses preserve existing files.

Checklist

  • Tests added or updated
  • Backward compatibility considered

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
bashkit 5b7c021 Commit Preview URL

Branch Preview URL
Nov 22 2026, 03:10 AM

@chaliy
chaliy force-pushed the perf/large-file-memory branch from bb12fbc to ec10f78 Compare November 22, 2026 03:04
@chaliy
chaliy force-pushed the perf/large-file-memory branch from ec10f78 to 5b7c021 Compare November 22, 2026 03:09
@chaliy
chaliy merged commit e711ac4 into main Nov 22, 2026
82 of 84 checks passed
@chaliy
chaliy deleted the perf/large-file-memory branch November 22, 2026 03:45
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