You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Memory estimates: verify the bound for imagecodecs codecs, the plane block reader and objects_read_plan #342
Follow-up from the independent review of #339 (finding 1). That review found that window_plan was not an upper bound of a window read's peak. With zlib 256-px uint16 tiles, a one-tile window was estimated at 262 KB but peaked at 625 KB. The cause was that _tiff_piece_memory counts one decoded piece, while tifffile holds several piece-sized buffers during a decode.
#339 fixes the window estimates. They now come from a new model (window_memory) that counts:
the decode buffers;
the codec's state;
the parsed header;
the frozen copy a cache keeps.
tests/test_window_memory.py checks that each estimate is at least the peak measured with tracemalloc over this matrix:
Format
Variants covered
TIFF
uint8 / uint16 / float32; uncompressed, deflate, deflate + predictor, and LZMA; tiles and strips; RGB; a multi-page OME stack
OME-Zarr
0.4 and 0.5; chunks spanning 1–3 channels
Reads
uncached, and through a full cache
Still open (#339 narrows the design note's claim to the cases above):
Codecs decoded only through imagecodecs (JPEG, LZW, Zstandard, WebP, JPEG 2000, floating-point predictor) are estimated with deflate's state and are not verified. Measure each one once imagecodecs is a declared optional dependency. Whole-slide JPEG images need it; see the design note's "Missing" column.
Memory that a codec allocates with its own malloc (some C libraries) is not seen by tracemalloc. Decide whether an RSS-based check is needed for those codecs.
The plane block reader's estimate (planes._tiff_piece_memory, used by read_plane block plans and max_ram) still counts one decoded piece plus the encoded bytes, so it has the same gap as the window estimate had. It was left unchanged in Lazy windows: design note (one processing-agnostic layer) + deterministic reader close and run window plan #339 because it sets block sizes for whole-plane reads, so changing it changes which planes fit in a block. It needs its own change and tests.
objects_read_plan and the mask conversions were not measured.
Platform margins: the measured worst case is 0.98 of the estimate (a large uncompressed float32 window, and a small cached OME-Zarr window), on Linux with CPython 3.11. CI checks Linux, macOS and Windows on 3.11 and 3.13; if any platform comes closer, widen OPEN_BYTES or the decode copies.
Related: #157 (read-memory estimate slightly low for tiny uncompressed tiles).
Evict-before-add test: added (27dc9c1). test_the_cache_drops_pieces_before_it_adds_one_never_over_its_bound checks the cache's contents at each addition. I moved the addition before the eviction to confirm it fails that way. The earlier bound test only checked the size after each call, so it passed even with the wrong order.
The multi-thread memory total is derived, not measured. The tests measure the peak of one window read at a time, uncached and through a full cache. The total for n concurrent reads from one reader, n × (estimate − cache_bytes) + cache_bytes, comes from those single-read bounds plus the cache bound. No test measures it with threads running concurrently. Item to add here: measure the process peak (tracemalloc, or RSS for codecs that use their own malloc) of n threads reading through one reader, and assert it stays under that total.
Another gap for this issue, found in the review of #347 (R1). The single-call estimate in planes._layout (planes.py:778, encoded_nbytes = sum(keyframe.databytecounts)) uses the first page's compressed sizes for every page. tifffile decodes each page with that page's own byte counts. So a file whose first pages compress to almost nothing is under-estimated for its other pages: for example, a blank first time point followed by noise.
Example: 2×3×3072×4096 TCYX uint8 deflate tiles, t=0 blank, t=1 noise. Reading t=1 peaks at about 73 MiB (tracemalloc), and that estimate counts almost no encoded bytes. That code is used by planes.read_plane, the histogram path and the block plans.
#347 fixes this only for RunInputs.read_plane's bound (plane_channels.xyc_plane_memory): it now sizes each page it reads from that page's own header byte counts. planes.py itself is unchanged and is left to this issue, together with item 3 (the decode-buffer gap).
Item 5 (RSS against the estimate). On a cold process, small OME-Zarr plans go over in RSS by a fixed first-use cost, while the traced peaks stay inside:
map_windows, 4 workers, plan 36.3 MiB on a 64³-chunk store: RSS 1.06 to 1.17 over 6 runs, traced 0.77 to 0.79. The same run after a warm-up map in the same process: 0.80 to 0.93.
Block reads at an 8 MiB limit: the first block plane of a process reaches RSS 1.10 to 1.23, which is also above the limit. Later planes stay at 0.09 to 0.27, traced 0.46 to 0.53.
This is process-wide memory (threads, zarr's private read pools, allocator arenas) of about 1.5 to 6 MiB. It only matters if an RSS-based gate is applied to small plans.
Activity
Make a window's memory estimate an upper bound of its peak (review of #…
Pin the region window estimate with its parsed header (review of #339,…
guijacquemet commentedon Nov 18, 2026
Update from the re-review of #339 (head
27dc9c1).27dc9c1).test_the_cache_drops_pieces_before_it_adds_one_never_over_its_boundchecks the cache's contents at each addition. I moved the addition before the eviction to confirm it fails that way. The earlier bound test only checked the size after each call, so it passed even with the wrong order.nconcurrent reads from one reader,n × (estimate − cache_bytes) + cache_bytes, comes from those single-read bounds plus the cache bound. No test measures it with threads running concurrently. Item to add here: measure the process peak (tracemalloc, or RSS for codecs that use their ownmalloc) ofnthreads reading through one reader, and assert it stays under that total.mask_window_planwas changed by Lazy windows: design note (one processing-agnostic layer) + deterministic reader close and run window plan #339;objects_read_planwas not.mask_window_plan(masks window by window) goes through the window estimate, so it now also counts the parsed header and, for a compressed mask, the decode buffers and codec state.test_objects_read_plan.pypins its region figures with the header added.RunInputs.objects_read_planis computed from the mask's shape and stored type inmasks.objects.objects_read_plan, not through the window estimate, so it is unchanged in Lazy windows: design note (one processing-agnostic layer) + deterministic reader close and run window plan #339; its tests pin the same figures as before. Item 4 above (measureobjects_read_plan's peak) therefore stays open as written.https://claude.ai/code/session_01G3qp9AxjpGPw9xfWAUJNqc
Generated by Claude Code
guijacquemet commentedon Nov 18, 2026
Another gap for this issue, found in the review of #347 (R1). The single-call estimate in
planes._layout(planes.py:778,encoded_nbytes = sum(keyframe.databytecounts)) uses the first page's compressed sizes for every page. tifffile decodes each page with that page's own byte counts. So a file whose first pages compress to almost nothing is under-estimated for its other pages: for example, a blank first time point followed by noise.Example: 2×3×3072×4096 TCYX
uint8deflate tiles, t=0 blank, t=1 noise. Reading t=1 peaks at about 73 MiB (tracemalloc), and that estimate counts almost no encoded bytes. That code is used byplanes.read_plane, the histogram path and the block plans.#347 fixes this only for
RunInputs.read_plane's bound (plane_channels.xyc_plane_memory): it now sizes each page it reads from that page's own header byte counts.planes.pyitself is unchanged and is left to this issue, together with item 3 (the decode-buffer gap).https://claude.ai/code/session_01G3qp9AxjpGPw9xfWAUJNqc
Generated by Claude Code
Read LZW and JPEG in the bounded readers, with their measured bounds (#…
guijacquemet commentedon Nov 26, 2026
Real-data evidence from #529 (draft PR #571,
mainat316be56, zarr 3.4.2, Python 3.13.18):OME-Zarr block, preview and whole-plane estimates are exceeded. Traced peaks reach 1.22× the
memory_limiton sharded stores. Filed separately as OME-Zarr whole-plane, preview and block reads exceed memory_limit on real data (chunk counted once) #569, because the zarr formula (_zarr_layout, and the zarr branch of_block_memory) is distinct from item 3's TIFF_tiff_piece_memory.Item 5 (RSS against the estimate). On a cold process, small OME-Zarr plans go over in RSS by a fixed first-use cost, while the traced peaks stay inside:
map_windows, 4 workers, plan 36.3 MiB on a 64³-chunk store: RSS 1.06 to 1.17 over 6 runs, traced 0.77 to 0.79. The same run after a warm-up map in the same process: 0.80 to 0.93.This is process-wide memory (threads, zarr's private read pools, allocator arenas) of about 1.5 to 6 MiB. It only matters if an RSS-based gate is applied to small plans.
Generated by Claude Code