Skip to content

Memory estimates: verify the bound for imagecodecs codecs, the plane block reader and objects_read_plan #342

Open

Description

@guijacquemet

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):

  1. 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.
  2. 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.
  3. 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.
  4. objects_read_plan and the mask conversions were not measured.
  5. 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).

https://claude.ai/code/session_01G3qp9AxjpGPw9xfWAUJNqc

Activity

  1. guijacquemet commented on Nov 18, 2026

    @guijacquemet
    ContributorAuthor

    Update from the re-review of #339 (head 27dc9c1).

    • 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.
    • mask_window_plan was changed by Lazy windows: design note (one processing-agnostic layer) + deterministic reader close and run window plan #339; objects_read_plan was 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.py pins its region figures with the header added. RunInputs.objects_read_plan is computed from the mask's shape and stored type in masks.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 (measure objects_read_plan's peak) therefore stays open as written.

    https://claude.ai/code/session_01G3qp9AxjpGPw9xfWAUJNqc


    Generated by Claude Code

  2. guijacquemet commented on Nov 18, 2026

    @guijacquemet
    ContributorAuthor

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

    https://claude.ai/code/session_01G3qp9AxjpGPw9xfWAUJNqc


    Generated by Claude Code

  3. guijacquemet commented on Nov 26, 2026

    @guijacquemet
    ContributorAuthor

    Real-data evidence from #529 (draft PR #571, main at 316be56, 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_limit on 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.
      • 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.


    Generated by Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions