Skip to content

Remove unnecessary array copies to reduce memory allocations#933

Merged
brendancol merged 2 commits intomasterfrom
remove-unnecessary-copies
Mar 3, 2026
Merged

Remove unnecessary array copies to reduce memory allocations#933
brendancol merged 2 commits intomasterfrom
remove-unnecessary-copies

Conversation

@brendancol
Copy link
Contributor

Summary

  • Replace np.zeros_like + [:] = np.nan with np.empty + [:] = np.nan in @ngjit functions (aspect, slope, classify) to skip a wasted zeroing pass
  • Drop redundant .copy() after cupy.asnumpy().astype() in erosion -- asnumpy already produces a fresh numpy array
  • Drop redundant .copy() after boolean fancy indexing in bump -- fancy indexing always copies
  • Use .data instead of .values on numpy-backed DataArrays in viewshed and min_observable_height to avoid unnecessary property dispatch and potential forced numpy conversion
  • Use np.asarray instead of np.array for barriers in pathfinding to skip copying when the input is already an ndarray
  • Replace np.array(float64_slice, dtype=float64) with .copy() in viewshed where no dtype conversion is needed
  • Widen test_perlin_gpu tolerance to rtol=1e-4, atol=1e-6 to match fastmath=True precision, consistent with test_perlin_dask_gpu

Test plan

  • All 1105 tests pass (the perlin GPU test that was previously failing now passes with corrected tolerances)

- Replace zeros_like + fill NaN with empty + fill NaN in ngjit functions
  (aspect, slope, classify) to avoid a wasted zeroing pass
- Drop redundant .copy() after cupy.asnumpy().astype() in erosion
  (asnumpy already produces a fresh numpy array)
- Drop redundant .copy() after boolean fancy indexing in bump
  (fancy indexing always returns a copy)
- Use .data instead of .values on numpy-backed DataArrays in viewshed
  and min_observable_height (.values forces numpy conversion)
- Use np.asarray instead of np.array for barriers in pathfinding
  (avoids copying when input is already an ndarray)
- Replace np.array(float64_slice, dtype=float64) with .copy() in
  viewshed (no dtype conversion needed)
- Widen test_perlin_gpu tolerance to match fastmath precision
  (rtol 1e-4, atol 1e-6); consistent with test_perlin_dask_gpu
@github-actions github-actions bot added the performance PR touches performance-sensitive code label Mar 3, 2026
Six standalone examples demonstrating cost distance, erosion,
fire propagation, flood simulation, landslide risk, and watershed
delineation.
@brendancol brendancol merged commit 19f9539 into master Mar 3, 2026
10 of 11 checks passed
@brendancol brendancol deleted the remove-unnecessary-copies branch March 5, 2026 03:31
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.

1 participant