Commit bed0daf
perf(merge): ⚡ derive global ids from tile counts, dropping two full passes
The merge rewrote the whole staged store twice for bookkeeping. Pass 0
(_make_globally_unique) streamed every chunk and renumbered it in place, so
tile-local 1..N ids stopped colliding. relabel_sequential_zarr then made a
second full read+write to compact the ids, building a Python set of every
label id on the way (~60 bytes each).
Neither pass needs to touch the volume. stage_tile already knows how many
labels it wrote, so it now renumbers its trimmed output densely and returns
that count; the segment job records it in the .done marker, which until now
was an empty file. Global ids are then offset + local with offset an
exclusive cumulative sum over those counts -- O(n_tiles) arithmetic. The
offsets are applied where they are needed instead: on the two chunks either
side of each boundary during the scan, and per chunk in the relabel worker.
That makes the id domain dense by construction, so the sequential renumber
becomes np.unique over the LUT -- length = object count, not voxels -- and
folds into the lookup the relabel pass already performs. Passing no counts
keeps the old streaming path, so the dask-array entry point and any external
caller are unaffected.
The relabel worker also stops widening whole blocks to int64, gathering only
the non-zero ids instead; labels are sparse, so its peak drops accordingly.
Verified on a synthetic workflow run: byte-identical labels and object count
to the previous implementation, and a test asserts the two paths agree.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>1 parent bbf7da2 commit bed0daf
5 files changed
Lines changed: 267 additions & 51 deletions
File tree
- src/patchworks
- tests
- workflow/scripts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| |||
158 | 160 | | |
159 | 161 | | |
160 | 162 | | |
161 | | - | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
162 | 168 | | |
163 | 169 | | |
164 | 170 | | |
| |||
175 | 181 | | |
176 | 182 | | |
177 | 183 | | |
178 | | - | |
179 | | - | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
180 | 191 | | |
181 | | - | |
182 | | - | |
| 192 | + | |
| 193 | + | |
0 commit comments