Skip to content

contour: remove redundant np.empty_like allocation in coordinate transform#3354

Merged
brendancol merged 3 commits into
xarray-contrib:mainfrom
Melissari1997:issue-3351
Jun 15, 2026
Merged

contour: remove redundant np.empty_like allocation in coordinate transform#3354
brendancol merged 3 commits into
xarray-contrib:mainfrom
Melissari1997:issue-3351

Conversation

@Melissari1997

Copy link
Copy Markdown
Collaborator

Reduce peak memory in contours() by removing a redundant np.empty_like allocation during the coordinate-space transform step.

np.interp already returns a new array, so the intermediate np.empty_like allocation was immediately overwritten. Replaced with np.column_stack which builds the output directly from the two interpolation results.

Closes #3351

Backend coverage: numpy only (the transform runs after backend dispatch completes, on the merged numpy results common to all paths).

@github-actions github-actions Bot added the performance PR touches performance-sensitive code label Jun 15, 2026

@Melissari1997 Melissari1997 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Review: contour: remove redundant np.empty_like allocation in coordinate transform

Blockers (must fix before merge)

None.

Suggestions (should fix, not blocking)

None.

Nits (optional improvements)

None.

What looks good

  • Correctness is preserved. coords is a freshly-allocated array returned by _stitch_segments (np.column_stack at contour.py:299). No other reference aliases it — _deduplicate_lines only reads the array and places the same object into the deduped list, so in-place mutation of coords in the transform loop is safe.
  • Memory improvement is real. The old code allocated an extra np.empty_like(coords) per polyline that was immediately overwritten. The new code avoids that allocation, reducing peak memory by one full coordinate array.
  • Surgical change. Only 3 lines changed. No new imports, no new dependencies, no refactoring of surrounding code.
  • Backend coverage is correctly scoped. The transform runs after backend dispatch completes, on the merged numpy results common to all paths.
  • Existing tests cover the changed code path. The test suite exercises the numpy contour path with several test cases that all hit the modified transform loop.
  • Edge cases are handled correctly. Empty results, single-point polylines (already dropped by _stitch_segments), and closed rings all behave identically to the old code.

Checklist

  • Algorithm matches reference/paper
  • All implemented backends produce consistent results
  • NaN handling is correct
  • Edge cases are covered by tests
  • Dask chunk boundaries handled correctly
  • No premature materialization or unnecessary copies
  • Benchmark exists or is not needed
  • README feature matrix updated (if applicable)
  • Docstrings present and accurate

@brendancol brendancol merged commit 57a5136 into xarray-contrib:main Jun 15, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance PR touches performance-sensitive code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

contour: contours() coordinate transform copies all coordinates a second time

2 participants