Skip to content

perf: phased codecpipeline#3885

Open
d-v-b wants to merge 13 commits intozarr-developers:mainfrom
d-v-b:perf/prepared-write-v2
Open

perf: phased codecpipeline#3885
d-v-b wants to merge 13 commits intozarr-developers:mainfrom
d-v-b:perf/prepared-write-v2

Conversation

@d-v-b
Copy link
Copy Markdown
Contributor

@d-v-b d-v-b commented Apr 8, 2026

This PR defines a new codec pipeline class called PhasedCodecPipeline that enables much higher performance for chunk encoding and decoding than the current BatchedCodecPipeline.

The approach here is to completely ignore how the v3 spec defines array -> bytes codecs 😆. Instead of treating codecs as functions that mix IO and compute, we treat codec encoding and decoding as a sequence:

  1. preparatory IO, async
    fetch exactly what we need to fetch from storage, given the codecs we have. So if there's a sharding codec in the first array->bytes position, the codec pipeline knows it must fetch the shard index, then fetch the involved subchunks, before passing them to compute.
  2. pure compute. sync. Apply filters and compressors. safe to parallelize over chunks.
  3. (if writing): final IO, async. reconcile the in-memory compressed chunks against our model of the stored chunk. Write out bytes.

Basically, we use the first array -> bytes codec to figure out what kind of preparatory IO and final IO we need to perform, and the rest of the codecs to figure out what kind of chunk encoding we need to do. Separating IO from compute in different phases makes things simpler and faster.

Happy to chat more about this direction. IMO the spec should be re-written with this framing, because it makes much more sense than trying to shoe-horn sharding in as a codec.

I don't want to make our benchmarking suite any bigger but on my laptop this codec pipeline is 2-5x faster than the batchedcodec pipeline for a lot of workloads. I can include some of those benchmarks later.

This was mostly written by claude, based on previous work in #3719. All these changes should be non-breaking, so I think this is in principle safe for us to play around with in a patch or minor release.

d-v-b added 4 commits April 7, 2026 10:38
`PreparedWrite` models a set of per-chunk changes that would be applied to a stored chunk. `SupportsChunkPacking`
is a protocol for array -> bytes codecs that can use `PreparedWrite` objects to update an existing chunk.
@github-actions github-actions bot added the needs release notes Automatically applied to PRs which haven't added release notes label Apr 8, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 8, 2026

Codecov Report

❌ Patch coverage is 86.62207% with 80 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.09%. Comparing base (9681cf9) to head (5fb28b9).

Files with missing lines Patch % Lines
src/zarr/core/codec_pipeline.py 86.18% 63 Missing ⚠️
src/zarr/codecs/numcodecs/_codecs.py 66.66% 8 Missing ⚠️
src/zarr/codecs/sharding.py 92.30% 7 Missing ⚠️
src/zarr/codecs/_v2.py 80.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3885      +/-   ##
==========================================
- Coverage   92.98%   92.09%   -0.90%     
==========================================
  Files          87       87              
  Lines       11246    11809     +563     
==========================================
+ Hits        10457    10875     +418     
- Misses        789      934     +145     
Files with missing lines Coverage Δ
src/zarr/abc/codec.py 98.86% <100.00%> (+0.09%) ⬆️
src/zarr/core/array.py 97.65% <100.00%> (+0.01%) ⬆️
src/zarr/core/config.py 100.00% <ø> (ø)
src/zarr/codecs/_v2.py 90.19% <80.00%> (-3.43%) ⬇️
src/zarr/codecs/sharding.py 75.42% <92.30%> (-13.99%) ⬇️
src/zarr/codecs/numcodecs/_codecs.py 93.18% <66.66%> (-3.21%) ⬇️
src/zarr/core/codec_pipeline.py 87.62% <86.18%> (-6.57%) ⬇️

... and 4 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@d-v-b
Copy link
Copy Markdown
Contributor Author

d-v-b commented Apr 9, 2026

@TomAugspurger how would this design work with CUDA codecs?

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

Labels

needs release notes Automatically applied to PRs which haven't added release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant