diff --git a/.kilo/worktrees/sweep-performance-state.csv b/.kilo/worktrees/sweep-performance-state.csv new file mode 100644 index 000000000..ed6b1fe04 --- /dev/null +++ b/.kilo/worktrees/sweep-performance-state.csv @@ -0,0 +1,2 @@ +module,last_inspected,oom_verdict,bottleneck,high_count,issue,notes +contour,2026-06-15,WILL OOM,memory-bound,1,3337,_deduplicate_by_level materializes all segments from all chunks (OOM at 30TB); _process_chunk_numpy unnecessary copy removed; double allocation in dedup fixed; cuda-unavailable diff --git a/xrspatial/contour.py b/xrspatial/contour.py index 0cf60da93..e7e8607b0 100644 --- a/xrspatial/contour.py +++ b/xrspatial/contour.py @@ -494,13 +494,11 @@ def _process_chunk_numpy(chunk_data, levels, r_offset, c_offset): return [] local_results = _contours_numpy(chunk_data, levels) - # Offset coordinates to global raster space. offset_results = [] for level, coords in local_results: - shifted = coords.copy() - shifted[:, 0] += r_offset - shifted[:, 1] += c_offset - offset_results.append((level, shifted)) + coords[:, 0] += r_offset + coords[:, 1] += c_offset + offset_results.append((level, coords)) return offset_results @@ -528,21 +526,23 @@ def _deduplicate_by_level(results): merged = [] for level in sorted(by_level.keys()): lines = by_level[level] - # Re-stitch all segments across chunk boundaries. - all_segs_r = [] - all_segs_c = [] - for line in lines: - for i in range(len(line) - 1): - all_segs_r.append([line[i, 0], line[i + 1, 0]]) - all_segs_c.append([line[i, 1], line[i + 1, 1]]) - - if not all_segs_r: + # Count total segments first so we can pre-allocate. + n_segs = sum(len(line) - 1 for line in lines) + if n_segs == 0: continue - seg_rows = np.array(all_segs_r, dtype=np.float64) - seg_cols = np.array(all_segs_c, dtype=np.float64) + seg_rows = np.empty((n_segs, 2), dtype=np.float64) + seg_cols = np.empty((n_segs, 2), dtype=np.float64) + + idx = 0 + for line in lines: + for i in range(len(line) - 1): + seg_rows[idx, 0] = line[i, 0] + seg_rows[idx, 1] = line[i + 1, 0] + seg_cols[idx, 0] = line[i, 1] + seg_cols[idx, 1] = line[i + 1, 1] + idx += 1 - # Remove exact duplicate segments. seg_rows, seg_cols = _remove_duplicate_segments(seg_rows, seg_cols) stitched = _stitch_segments(seg_rows, seg_cols, len(seg_rows))