contour: remove redundant np.empty_like allocation in coordinate transform#3354
Merged
Conversation
Melissari1997
commented
Jun 15, 2026
Melissari1997
left a comment
Collaborator
Author
There was a problem hiding this comment.
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.
coordsis a freshly-allocated array returned by_stitch_segments(np.column_stackatcontour.py:299). No other reference aliases it —_deduplicate_linesonly reads the array and places the same object into the deduped list, so in-place mutation ofcoordsin 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
approved these changes
Jun 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reduce peak memory in
contours()by removing a redundantnp.empty_likeallocation during the coordinate-space transform step.np.interpalready returns a new array, so the intermediatenp.empty_likeallocation was immediately overwritten. Replaced withnp.column_stackwhich 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).