From 46464e73e30d215eb30f0a5bdcbc0f08dad85fa6 Mon Sep 17 00:00:00 2001 From: anon Date: Wed, 10 Jun 2026 13:50:22 +0200 Subject: [PATCH] perf(images): materialize rasterized image once to avoid per-channel re-warp `spatialdata.rasterize` returns a lazy dask array; `_render_images` then reads it once per channel (NaN check, compositing, draw), so the affine warp re-runs N_passes x N_channels times per render. Materializing the rasterized result once collapses that to a single warp. Pixel-identical output (no baseline change); the rasterized array is always display-sized so the eager compute is memory-bounded. Measured ~15-20x on multi-channel images (8ch 6000^2: 10.4s -> 0.5s); neutral for single-channel. Covers images and labels. Closes #707 --- src/spatialdata_plot/pl/utils.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/spatialdata_plot/pl/utils.py b/src/spatialdata_plot/pl/utils.py index b6eab503..25720fb5 100644 --- a/src/spatialdata_plot/pl/utils.py +++ b/src/spatialdata_plot/pl/utils.py @@ -2283,6 +2283,10 @@ def _rasterize_if_necessary( coordinate_system, target_unit_to_pixels=target_unit_to_pixels, ) + if hasattr(image.data, "compute"): + # rasterize is lazy; downstream reads the result once per channel (NaN check, + # compositing, draw), so materialize once instead of re-running the warp each time. + image = image.copy(data=image.data.compute()) return image