Skip to content

Coalesce concurrent chunk range reads by default - #30

Merged
Shane98c merged 3 commits into
mainfrom
coalesce-ranges
Nov 18, 2026
Merged

Shane98c merged 3 commits into
mainfrom
coalesce-ranges

Conversation

@espg

@espg espg commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

This came up while virtualizing some of the ISMIP7 forcing data (i.e., CMIP forcings), and performance was getting killed with lots of small range requests for data chunks that were (mostly) contiguous. Notes from Claude below. @Shane98c, probably worth a quick glance since it changes the default behavior.


🤖 (from claude)

Readers such as zarrita request one time step of a variable as N chunk keys at once, and each chunk became its own HTTP Range request. For virtual stores whose chunks sit next to each other in the source file that is latency-bound: on the ISMIP7 forcing stores on source.coop, one 2 km time step is 145 Range requests for 10.6 MB that form three contiguous byte runs. Range coalescing already existed in 0.6.1, but only when the caller passed withRangeCoalescing: zarrita.withRangeCoalescing, which gridlook (and most readers) never do.

This adds a built-in coalescer, createRangeCoalescer(options), in the same RangeCoalescingFn seam that ReadSession already uses, so the per-object stores and their partitioning by fetch client and checksum headers are unchanged and zarrita stays an optional peer dependency. IcechunkStore.open now coalesces by default. coalesce: { maxGap, maxBytes, maxWasteRatio, window } tunes it (defaults 256 KiB, 16 MiB, 1, and the current microtask turn), coalesce: false restores one request per chunk, and withRangeCoalescing still swaps in zarrita's wrapper. Reads queued in one turn are grouped per backing object, sorted, deduplicated and merged into one request per run; gap bytes are fetched and discarded, bounded by maxWasteRatio so a run never discards more than it delivers. A merged request that fails or comes back short is retried one member at a time, except after 401, 403, 404 or 412, which apply to the whole object. Aborting one read rejects only that read, whether it is still queued or already in flight, and the shared request is aborted once every reader has aborted. Readers always receive their own copies, never views into the merged buffer. store.rangeCoalescer.stats counts requests and bytes requested, fetched and discarded.

Details

Two hardening changes ride along: HttpStorage and makeUrlStore reject a 206 whose Content-Range does not match the request (with merging, one misplaced body would shift many chunks), HttpStorage cuts a 200 full body down to the requested range, and StorageError gains an optional status.

Measured against the public ISMIP7 AIS store from Node, one time step: acabf at 2 km, 145 chunks, 145 requests become 3 for 0.1% more bytes; so at 8 km, 512 chunks in an interleaved layout, 512 requests become 139 for 17% more bytes. The live measurement runs with ICECHUNK_LIVE_TESTS=1 npx vitest run tests/live. 44 new unit tests cover merging, gaps, the size cap, the waste bound, keys, deduplication, overlap, fallback, short reads, abort while queued and in flight, the batching window, stats, the Content-Range checks and the store wiring.

Public API additions only; withRangeCoalescing and the RangeCoalescingFn export are unchanged. Because the default behaviour changes, the suggested release is 0.7.0.

@espg

espg commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator Author

Timings behind the request counts in the description (posted by Claude Code, which did the implementation and the measurements).

step mode requests bytes wall
acabf, 2 km, 145 chunks (10.59 MB) no coalescing 145 10.59 MB 1.6–5.6 s
zarrita wrapper, 32 KiB gap 3 10.60 MB 2.2–2.6 s
zarrita wrapper, 1 MiB gap 2 10.67 MB 1.0 s
built-in, defaults (256 KiB, waste ≤ 1) 3 10.60 MB (9 KB discarded) 1.4–1.5 s
so, ocean 8 km, 512 chunks (20.48 MB), interleaved layout no coalescing 512 20.48 MB 6.1–7.7 s
zarrita wrapper, 32 KiB gap 182 21.13 MB 5.8–6.3 s
built-in, 128 KiB gap 126 24.85 MB 2.2–2.5 s
built-in, defaults (256 KiB, waste ≤ 1) 139 23.97 MB (3.49 MB discarded) 3.0–6.4 s
built-in, 1 MiB gap, no waste bound 62 57.89 MB (37.4 MB discarded) 2.6–2.7 s
Details Node 22 on a laptop over a cellular link, one time step read through zarrita 0.7, chunk Range requests only (manifest reads excluded), wall time until the array is decoded. The link's latency varied by about ±50 % between runs, so the wall times are indicative, not a benchmark; the request and byte counts are exact.

The ocean rows are why the default gap is 256 KiB with the waste bound rather than 1 MiB: beyond about 128–256 KiB the extra requests saved are paid for with bytes that are fetched and thrown away (2.8× the payload at 1 MiB), and the wall time stops improving. For layouts where a step's chunks are contiguous (the 2 km case, and the 8 km/4 km products that are one chunk per step) the default collapses a step to a handful of requests at no byte cost.

In the browser (gridlook, headless Chromium) the same 2 km step went from 148 Range requests to 8 for the whole level, and to one 3.45 MB request for the part in view once the viewer only loads its view window.

@espg
espg requested a review from Shane98c November 11, 2026 23:15
@espg

espg commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

...minor update for the repo pointer and ref.json (the only objects that change in place): they are now read with no-cache, which checks the ETag and still serves the cached copy on a 304, otherwise fetches the new one.

Fixes a browser staying on an old snapshot across reloads after a commit. One conditional request per repository open, so no meaningful performance impact.

@Shane98c Shane98c left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry i was slow here! this looks good!

@Shane98c
Shane98c merged commit 75d36d6 into main Nov 18, 2026
12 checks passed
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.

2 participants