Skip to content

Make longitude range lazy for lazy Grid constructors - #1791

Merged
cmdupuis3 merged 14 commits into
UXARRAY:mainfrom
cmdupuis3:cmd/lazy-lon-range
Oct 26, 2026
Merged

cmdupuis3 merged 14 commits into
UXARRAY:mainfrom
cmdupuis3:cmd/lazy-lon-range

Conversation

@cmdupuis3

@cmdupuis3 cmdupuis3 commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1792

Overview

_set_desired_longitude_range does if da.max() > 180 on a lazy reduction, three times (node_lon, edge_lon, face_lon). Re-fires on every Grid construction, so every isel and every copy() pays it again.

The basic idea is to use a memoized xr.where so that we don't need to materialize the whole dimension to view the values that are available per chunk.

PR Checklist

General

  • An issue is created and linked
  • Added appropriate labels (if your uxarray repo permissions allow it)
  • Filled out Overview and Expected Usage (if applicable) sections

Testing & Benchmarking

  • There is adequate test coverage of changes from this PR (add new tests if needed)
  • If this PR could affect performance, ran ASV benchmarks and confirmed they show expected behavior (add a new benchmark if necessary)

Documentation and Examples

  • Docstrings updated with any function changes, and included in all new functions
  • User (public) functions added to docs/api.rst; internal (private) function names start with an underscore (_)

AI Disclosure

AI Usage: Claude Opus 5.5

  • I have tested and take responsibility for all AI-generated content in my PR.

cmdupuis3 and others added 3 commits September 25, 2026 18:46
_set_desired_longitude_range decided whether to wrap by asking
lon.max() > 180. On a dask-backed coordinate that reduction is a compute,
and Grid.__init__ calls it -- so opening a chunked grid read and reduced
every longitude array before the caller had asked for anything, and did it
again on every isel and every copy(), each of which builds a new Grid.

The wrap is now xr.where((lon > 180) | (lon < -180), (lon + 180) % 360 - 180,
lon): elementwise, lazy, chunk-parallel, no reduction.

Measured on a synthetic 4M-node UGRID file, chunked at 500k nodes, best of 5:

                       dask computes    wall      tracemalloc peak
    open_grid (before)             3    23.3ms          12.2 MB
    open_grid (after)              2    16.2ms           1.0 MB
    isel      (before)             2
    isel      (after)              1

The computes that remain are a separate site on connectivity rather than
coordinates: _standardize_connectivity's conn.isnull().any() in io/_ugrid.py,
reached twice on the read path, and _slice_face_indices in grid/slice.py
materializing the connectivity it slices by. Neither is touched here.

Three behavioural differences, all from doing this per element rather than
per array.

  * In-range longitudes are now left exactly alone. (lon + 180) - 180 does
    not round-trip, so wrapping the whole array perturbed values that were
    already in range by up to 3e-14 degrees -- in outCSne30, 2.1182935e-14
    became 2.8421709e-14. On the elements that do need wrapping the two forms
    are bit-identical.

  * Longitudes below -180 are normalized. The old test was on the maximum
    alone, so it reached the negative tail only when the same array also held
    a value above 180.

  * Both endpoints are kept, so the interval is the closed [-180, 180].
    Folding 180.0 to -180.0 would match _xyz_to_lonlat_deg, which wraps
    unconditionally into the half-open interval, but it breaks
    antimeridian_face_indices: that reads a face as crossing from the span of
    its longitudes, and a face with one vertex at 180 and the rest near -170
    goes from a span of 350 to a span of 10 and disappears. Caught by
    test_antimeridian_point_on and
    test_to_geodataframe_preserves_antimeridian_faces.

Each variable is wrapped at most once, keyed on the xr.Variable object.
Without that, edge_lat -- which calls this on every property access,
outside its populate guard -- would stack a where layer onto the graph per
access. Keying on the Variable makes the memo
self-invalidating: assigning into _ds replaces that object, so a repopulated
or user-assigned coordinate is wrapped again.

The two Exodus round-trip tests compared a grid against its own
lon -> xyz -> lon reload with assert_allclose(rtol=1e-8), and passed only
because the old whole-array wrap applied to the original the identical
perturbation the reload applies. With the original left alone, the reload's
own error is exposed, and rtol is the wrong instrument for it twice over: a
longitude near zero has no magnitude for a relative tolerance to measure
against (outCSne30 nodes 4372, 4749, 7e-15 degrees apart), and longitude is
periodic, so a node on or one ulp short of the antimeridian reads 180.0 on
the original and -180.0 on the reload -- the same meridian, scored as a
360-degree error (179 nodes of outRLL1deg; outCSne30 nodes 3966, 5155).
Those two assertions now compare the difference modulo 360 to an absolute
tolerance, still ERROR_TOLERANCE. The helper still catches a 1e-7 shift and
still rejects an antipode.

Tier 0.2 of the chunked refactor plan.

Test suite: 962 passed, 1 skipped. test_plot_with_features fails identically
before and after (matplotlib figure size, unrelated).

Co-Authored-By: Claude Opus 5 <[email protected]>
The elementwise wrap is the right shape for a dask-backed coordinate and the
wrong one for an array already in memory. There was never a compute to defer
on that path, only a scan, and elementwise costs a pass plus ~18 bytes per
node of temporaries -- two bool masks, the arithmetic, the result -- where a
reduction costs a pass and allocates nothing.

That matters because every open_grid/open_dataset call in the benchmark suite
is eager; none pass chunks=. So the previous commit, measured there, was a
regression and nothing else.

Measured on a synthetic 4M-node UGRID file, best of 5. The wrap in isolation:

                              in range         0..360
    old reduction               4.6ms  0MB    36.8ms  64MB
    elementwise only           20.9ms 72MB    38.1ms  72MB
    elementwise + guard         9.3ms  0MB    42.7ms  72MB

and through eager open_grid, where the file read dominates and the peak does
not move at all (180.0 MB in every arm):

                              in range         0..360
    base (cmd/nogil)           145.5ms         181.9ms
    elementwise only           164.4ms         186.1ms
    elementwise + guard        147.9ms         185.9ms

_lon_within_range is a guard, not a decision: when it is true the wrap is the
identity on every element, so skipping it cannot change a value.
test_eager_fast_path_agrees_with_the_where_element_for_element asserts that
directly against the unguarded expression rather than assuming it, over five
inputs including both endpoints, the negative tail and NaN.

The reductions are the thing that made the old code compute, so they are
allowed only where there is nothing to defer -- da.chunks is None. A
dask-backed array skips the guard entirely, which
test_guard_is_skipped_for_dask_backed_arrays pins by asserting the guard does
compute when handed one. The chunked numbers are unchanged: 2 graph
executions, 16.0ms, 1.0 MB peak.

`and` short-circuits, so an array that does need wrapping usually pays a
single max -- the same reduction the old code paid -- before falling through.
The 0..360 column above is that extra max: ~4.6ms on a 190ms open_grid.

Test suite: 973 passed, 1 skipped. test_plot_with_features fails identically
before and after (matplotlib figure size, unrelated).

Co-Authored-By: Claude Opus 5 <[email protected]>
Every other open_grid in the suite is eager, so none of it can see what
Grid construction does to a dask-backed grid -- which is the path the lazy
longitude wrap changed, and the path the rest of the chunked refactor will
keep changing. OpenGridChunked adds time_open_grid and
track_peakmem_open_grid, parametrized over the oQU 480km and 120km meshes
already registered in helpers/_fixtures.py.

Chunks are held at N_CHUNKS=4 per grid dimension, so the graph is the same
shape at both resolutions and only the data under it grows. The file is read
directly rather than through CachedFixtures, because reading it is the
subject.

On MPAS this is where the lazy wrap matters most. node_lon, edge_lon and
face_lon all exist at construction, so the old max() > 180 check ran three
computes per open; the branch runs none. Measured with this benchmark's own
setup, best of 15, against HEAD's tree with coordinates.py taken from
cmd/nogil:

                      time               tracemalloc peak
                  base    branch        base    branch
    480km       60.5ms    56.5ms      3.27MB    3.27MB
    120km       42.3ms    38.0ms      3.53MB    4.02MB

The 120km peak reads higher on the branch, and it is not data. With gc
disabled, building the where graph allocates ~1.9 MB of transient objects,
nearly all in inspect.signature via dask/xarray op dispatch -- the same at
both resolutions and with a single chunk, so it does not scale with the
grid. At 480km it sits under the HDF5 read's own high-water mark; at 120km
the netCDF3 read is cheap enough that it becomes the peak. After a
gc.collect() the branch retains ~30 kB more than base, which is the extra
graph layers. Worth knowing for later steps: this benchmark sees
graph-construction overhead, not only bytes read.

Three things the numbers above depend on:

  * Compare each resolution to its own history, not to the other. The two
    files are different formats -- oQU480.grid.nc is netCDF4/HDF5,
    oQU120.grid.nc is netCDF3 -- and the HDF5 open costs more, so 480km reads
    slower than 120km despite a sixteenth of the data.

  * Each open gets a fresh copy of the chunks dict. match_chunks_to_ugrid
    (core/utils.py) writes the source-format dimension names into the
    dict it is handed, so a reused one gives every sample after the first a
    different argument.

  * The warning "The specified chunks separate the stored chunks" is
    filtered. oQU480 stores layerThickness, ssh and zMid as one chunk of all
    1791 cells, so any n_face chunking splits them and xarray warns once per
    open -- about data variables the grid reader drops.

Checked by calling the class the way asv does (setup(param), then the
time_/track_ methods) under -W error::UserWarning. Not run through asv
itself: its discovery subprocess cannot import uxarray from the uxarray
conda env, where the package is not installed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cmdupuis3 cmdupuis3 self-assigned this Sep 25, 2026
@cmdupuis3 cmdupuis3 added scalability Related to scalability & performance efforts run-benchmark Run ASV benchmark workflow labels Sep 25, 2026
@github-actions

github-actions Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

ASV Benchmarking

Benchmark Comparison Results

Benchmarks that have improved:

Change Before [e2e5d6a] After [5a4d34f] Ratio Benchmark (Parameter)
- 2 1 0.5 lazy_grid_construction.LazyGridConstruction.track_computes_isel
- 3 2 0.67 lazy_grid_construction.LazyGridConstruction.track_computes_open_grid_chunked
- 95.6±1ms 84.8±0.6ms 0.89 lazy_grid_construction.OpenGridChunked.time_open_grid('120km')
- 2.61M 2.11M 0.81 lazy_grid_construction.OpenGridChunked.track_peakmem_open_grid('120km')

Benchmarks that have stayed the same:

Change Before [e2e5d6a] After [5a4d34f] Ratio Benchmark (Parameter)
726±20μs 767±30μs 1.06 connectivity.Connectivity.time_edge_face('120km')
293±7μs 296±20μs 1.01 connectivity.Connectivity.time_edge_face('480km')
3.48±0.06ms 3.64±0.1ms 1.05 connectivity.Connectivity.time_edge_node('120km')
647±20μs 690±20μs 1.07 connectivity.Connectivity.time_edge_node('480km')
38.4±6μs 46.3±3μs ~1.21 connectivity.Connectivity.time_face_edge('120km')
36.5±3μs 38.4±4μs 1.05 connectivity.Connectivity.time_face_edge('480km')
453±20μs 481±10μs 1.06 connectivity.Connectivity.time_face_face('120km')
281±20μs 285±10μs 1.01 connectivity.Connectivity.time_face_face('480km')
37.7±3μs 41.9±3μs ~1.11 connectivity.Connectivity.time_face_node('120km')
37.1±3μs 39.9±3μs 1.08 connectivity.Connectivity.time_face_node('480km')
286±20μs 315±10μs 1.10 connectivity.Connectivity.time_n_nodes_per_face('120km')
240±20μs 246±20μs 1.02 connectivity.Connectivity.time_n_nodes_per_face('480km')
699±10μs 716±20μs 1.02 connectivity.Connectivity.time_node_edge('120km')
271±10μs 276±10μs 1.02 connectivity.Connectivity.time_node_edge('480km')
39.5±1ms 49.3±3ms ~1.25 connectivity.Connectivity.time_node_face('120km')
2.57±0.04ms 2.55±0.04ms 0.99 connectivity.Connectivity.time_node_face('480km')
1.42M 1.42M 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_edge_face('120km')
106k 106k 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_edge_face('480km')
6.48M 6.48M 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_edge_node('120km')
419k 419k 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_edge_node('480km')
2.42k 2.42k 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_face_edge('120km')
2.42k 2.42k 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_face_edge('480km')
1.6M 1.6M 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_face_face('120km')
101k 101k 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_face_face('480km')
2.54k 2.54k 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_face_node('120km')
2.54k 2.54k 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_face_node('480km')
241k 241k 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_n_nodes_per_face('120km')
27k 26.8k 0.99 connectivity.ConnectivityTracemalloc.track_peakmem_n_nodes_per_face('480km')
1.9M 1.9M 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_node_edge('120km')
127k 127k 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_node_edge('480km')
11.9M 11.9M 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_node_face('120km')
747k 747k 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_node_face('480km')
5.45±0.05ms 5.58±0.04ms 1.02 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/mpas/QU/oQU480.231010.nc'))
1.67±0.02ms 1.70±0.07ms 1.02 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/scrip/outCSne8/outCSne8.nc'))
7.14±0.08ms 7.17±0.06ms 1.00 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/geoflow-small/grid.nc'))
873±10μs 841±20μs 0.96 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/quad-hexagon/grid.nc'))
57.3k 57.3k 1.00 face_bounds.FaceBounds.track_nbytes_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/mpas/QU/oQU480.231010.nc'))
12.3k 12.3k 1.00 face_bounds.FaceBounds.track_nbytes_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/scrip/outCSne8/outCSne8.nc'))
123k 123k 1.00 face_bounds.FaceBounds.track_nbytes_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/geoflow-small/grid.nc'))
128 128 1.00 face_bounds.FaceBounds.track_nbytes_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/quad-hexagon/grid.nc'))
1.27M 1.27M 1.00 face_bounds.FaceBounds.track_nbytes_grid_with_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/mpas/QU/oQU480.231010.nc'))
50.1k 50.1k 1.00 face_bounds.FaceBounds.track_nbytes_grid_with_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/scrip/outCSne8/outCSne8.nc'))
1.48M 1.48M 1.00 face_bounds.FaceBounds.track_nbytes_grid_with_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/geoflow-small/grid.nc'))
712 712 1.00 face_bounds.FaceBounds.track_nbytes_grid_with_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/quad-hexagon/grid.nc'))
1.91M 1.91M 1.00 face_bounds.FaceBounds.track_peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/mpas/QU/oQU480.231010.nc'))
1.9M 1.9M 1.00 face_bounds.FaceBounds.track_peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/scrip/outCSne8/outCSne8.nc'))
2.07M 2.06M 1.00 face_bounds.FaceBounds.track_peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/geoflow-small/grid.nc'))
35.6k 35.6k 1.00 face_bounds.FaceBounds.track_peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/quad-hexagon/grid.nc'))
1.18±0.02μs 1.20±0.04μs 1.02 geometry_kernels.AccucrossKernels.time_accucross
2.67±0.05μs 2.65±0.06μs 1.00 geometry_kernels.AccucrossKernels.time_accucross_pair
410±10ns 406±10ns 0.99 geometry_kernels.EFTPrimitives.time_acc_sqrt_re
391±4ns 410±10ns 1.05 geometry_kernels.EFTPrimitives.time_diff_of_products
371±10ns 366±10ns 0.99 geometry_kernels.EFTPrimitives.time_two_prod
350±9ns 341±20ns 0.97 geometry_kernels.EFTPrimitives.time_two_sum
652±10ns 692±30ns 1.06 geometry_kernels.GCAConstLatIntersection.time_accux_constlat_kernel
711±10ns 692±10ns 0.97 geometry_kernels.GCAConstLatIntersection.time_gca_const_lat_intersection
801±50ns 807±30ns 1.01 geometry_kernels.GCAConstLatIntersection.time_try_gca_const_lat_intersection
421±4ns 431±5ns 1.02 geometry_kernels.GCAGCAIntersection.time_accux_gca_kernel
595±5ns 606±10ns 1.02 geometry_kernels.GCAGCAIntersection.time_gca_gca_intersection
646±20ns 666±20ns 1.03 geometry_kernels.GCAGCAIntersection.time_try_gca_gca_intersection
22.6±0.2μs 23.1±0.7μs 1.02 geometry_kernels.OrientPredicates.time_on_minor_arc
22.6±0.3μs 23.4±0.4μs 1.04 geometry_kernels.OrientPredicates.time_orient3d_on_sphere
2.61±0ms 2.61±0.01ms 1.00 geometry_samebody.SameBodyConstLat.time_accux_dispatch
684±6μs 682±3μs 1.00 geometry_samebody.SameBodyConstLat.time_accux_kernel
2.55±0.01ms 2.52±0.01ms 0.99 geometry_samebody.SameBodyConstLat.time_fp64_dispatch
85.4±0.9μs 83.9±0.3μs 0.98 geometry_samebody.SameBodyConstLat.time_fp64_kernel
29.6±0.06ms 29.6±0.02ms 1.00 geometry_samebody_gcagca.SameBodyGcaGca.time_accux_dispatch
6.34±0.06ms 6.28±0.01ms 0.99 geometry_samebody_gcagca.SameBodyGcaGca.time_accux_kernel
23.7±0.06ms 23.7±0.08ms 1.00 geometry_samebody_gcagca.SameBodyGcaGca.time_fp64_dispatch
859±1μs 863±4μs 1.00 geometry_samebody_gcagca.SameBodyGcaGca.time_fp64_kernel
569±10ms 628±40ms ~1.10 import.Imports.timeraw_import_uxarray
126±1ms 118±2ms 0.93 lazy_grid_construction.OpenGridChunked.time_open_grid('480km')
2.61±0.03ms 2.51±0.04ms 0.96 mpas_ocean.CheckNorm.time_check_norm('120km')
2.02±0.02ms 2.01±0.04ms 0.99 mpas_ocean.CheckNorm.time_check_norm('480km')
1.14±0.01ms 1.16±0.02ms 1.02 mpas_ocean.ConnectivityConstruction.time_face_face_connectivity('120km')
547±10μs 552±10μs 1.01 mpas_ocean.ConnectivityConstruction.time_face_face_connectivity('480km')
646±9μs 651±20μs 1.01 mpas_ocean.ConnectivityConstruction.time_n_nodes_per_face('120km')
590±6μs 602±20μs 1.02 mpas_ocean.ConnectivityConstruction.time_n_nodes_per_face('480km')
5.40±0.3ms 5.24±0.03ms 0.97 mpas_ocean.ConstructFaceLatLon.time_cartesian_averaging('120km')
3.77±0.01ms 3.81±0.05ms 1.01 mpas_ocean.ConstructFaceLatLon.time_cartesian_averaging('480km')
98.7±0.3ms 96.7±0.3ms 0.98 mpas_ocean.ConstructFaceLatLon.time_welzl('120km')
9.76±0.4ms 9.71±0.4ms 1.00 mpas_ocean.ConstructFaceLatLon.time_welzl('480km')
18.6±0.01ms 18.8±0.1ms 1.01 mpas_ocean.ConstructTreeStructures.time_ball_tree('120km')
973±8μs 992±6μs 1.02 mpas_ocean.ConstructTreeStructures.time_ball_tree('480km')
9.99±0.03ms 10.1±0.03ms 1.01 mpas_ocean.ConstructTreeStructures.time_kd_tree('120km')
616±10μs 618±20μs 1.00 mpas_ocean.ConstructTreeStructures.time_kd_tree('480km')
520±3ms 570±5ms 1.10 mpas_ocean.CrossSections.time_const_lat('120km', 1)
264±2ms 288±5ms 1.09 mpas_ocean.CrossSections.time_const_lat('120km', 2)
136±0.5ms 148±1ms 1.09 mpas_ocean.CrossSections.time_const_lat('120km', 4)
470±2ms 517±8ms 1.10 mpas_ocean.CrossSections.time_const_lat('480km', 1)
242±1ms 255±0.2ms 1.05 mpas_ocean.CrossSections.time_const_lat('480km', 2)
122±2ms 132±2ms 1.07 mpas_ocean.CrossSections.time_const_lat('480km', 4)
16.1±0.2ms 17.2±0.3ms 1.07 mpas_ocean.DualMesh.time_dual_mesh_construction('120km')
14.5±0.7ms 14.8±0.7ms 1.02 mpas_ocean.FaceAreas.time_face_areas('120km')
4.18±0.08ms 4.51±0.2ms 1.08 mpas_ocean.FaceAreas.time_face_areas('480km')
229k 229k 1.00 mpas_ocean.FaceAreas.track_nbytes_face_areas('120km')
14.3k 14.3k 1.00 mpas_ocean.FaceAreas.track_nbytes_face_areas('480km')
2.12M 2.12M 1.00 mpas_ocean.FaceAreas.track_peakmem_face_areas('120km')
722k 717k 0.99 mpas_ocean.FaceAreas.track_peakmem_face_areas('480km')
81.6±3ms 84.0±0.4ms 1.03 mpas_ocean.GeoDataFrame.time_to_geodataframe('120km', False)
27.7±0.6ms 30.2±0.3ms 1.09 mpas_ocean.GeoDataFrame.time_to_geodataframe('120km', True)
15.0±0.3ms 16.3±0.07ms 1.09 mpas_ocean.GeoDataFrame.time_to_geodataframe('480km', False)
2.87±0.2ms 3.36±0.3ms ~1.17 mpas_ocean.GeoDataFrame.time_to_geodataframe('480km', True)
13.1±0.04ms 13.2±0.04ms 1.00 mpas_ocean.Gradient.time_gradient('120km')
1.70±0.01ms 1.72±0.01ms 1.01 mpas_ocean.Gradient.time_gradient('480km')
457k 457k 1.00 mpas_ocean.Gradient.track_nbytes_gradient('120km')
28.7k 28.7k 1.00 mpas_ocean.Gradient.track_nbytes_gradient('480km')
3.2M 3.2M 1.00 mpas_ocean.Gradient.track_peakmem_gradient('120km')
204k 204k 1.00 mpas_ocean.Gradient.track_peakmem_gradient('480km')
265±4μs 268±6μs 1.01 mpas_ocean.HoleEdgeIndices.time_construct_hole_edge_indices('120km')
125±3μs 127±2μs 1.01 mpas_ocean.HoleEdgeIndices.time_construct_hole_edge_indices('480km')
205±2μs 202±2μs 0.99 mpas_ocean.Integrate.time_integrate('120km')
190±10μs 186±1μs 0.98 mpas_ocean.Integrate.time_integrate('480km')
18.4M 18.4M 1.00 mpas_ocean.Integrate.track_nbytes_integrate('120km')
1.2M 1.2M 1.00 mpas_ocean.Integrate.track_nbytes_integrate('480km')
187±2ms 186±2ms 1.00 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('120km', 'exclude')
185±2ms 184±1ms 0.99 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('120km', 'include')
184±2ms 184±2ms 1.00 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('120km', 'split')
12.8±0.07ms 13.1±0.2ms 1.02 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('480km', 'exclude')
12.8±0.06ms 12.9±0.2ms 1.01 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('480km', 'include')
12.9±0.1ms 12.9±0.06ms 1.00 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('480km', 'split')
180±2ms 177±0.3ms 0.98 mpas_ocean.NeighborhoodBuild.time_build('120km', 1.0)
375±2ms 373±1ms 1.00 mpas_ocean.NeighborhoodBuild.time_build('120km', 5.0)
9.53±0.1ms 9.62±0.07ms 1.01 mpas_ocean.NeighborhoodBuild.time_build('480km', 1.0)
11.8±0.08ms 12.1±0.05ms 1.03 mpas_ocean.NeighborhoodBuild.time_build('480km', 5.0)
179±2ms 175±0.7ms 0.98 mpas_ocean.NeighborhoodBuild.time_query_radius('120km', 1.0)
368±3ms 369±0.8ms 1.00 mpas_ocean.NeighborhoodBuild.time_query_radius('120km', 5.0)
9.22±0.06ms 9.41±0.02ms 1.02 mpas_ocean.NeighborhoodBuild.time_query_radius('480km', 1.0)
11.4±0.1ms 11.7±0.08ms 1.02 mpas_ocean.NeighborhoodBuild.time_query_radius('480km', 5.0)
612.76 612.76 1.00 mpas_ocean.NeighborhoodBuild.track_mean_neighbors('120km', 15.0)
74.17 74.17 1.00 mpas_ocean.NeighborhoodBuild.track_mean_neighbors('120km', 5.0)
37.29 37.29 1.00 mpas_ocean.NeighborhoodBuild.track_mean_neighbors('480km', 15.0)
6.57 6.57 1.00 mpas_ocean.NeighborhoodBuild.track_mean_neighbors('480km', 5.0)
141M 141M 1.00 mpas_ocean.NeighborhoodBuild.track_nbytes_neighbors('120km', 15.0)
17.4M 17.4M 1.00 mpas_ocean.NeighborhoodBuild.track_nbytes_neighbors('120km', 5.0)
563k 563k 1.00 mpas_ocean.NeighborhoodBuild.track_nbytes_neighbors('480km', 15.0)
123k 123k 1.00 mpas_ocean.NeighborhoodBuild.track_nbytes_neighbors('480km', 5.0)
145M 145M 1.00 mpas_ocean.NeighborhoodBuild.track_peakmem_build('120km', 15.0)
21.5M 21.5M 1.00 mpas_ocean.NeighborhoodBuild.track_peakmem_build('120km', 5.0)
825k 825k 1.00 mpas_ocean.NeighborhoodBuild.track_peakmem_build('480km', 15.0)
384k 384k 1.00 mpas_ocean.NeighborhoodBuild.track_peakmem_build('480km', 5.0)
40.3±0.6ms 40.0±0.5ms 0.99 mpas_ocean.NeighborhoodDask.time_mean('120km', 'grid_chunks')
18.7±0.05ms 18.4±0.07ms 0.99 mpas_ocean.NeighborhoodDask.time_mean('120km', 'numpy')
36.7±0.7ms 37.0±1ms 1.01 mpas_ocean.NeighborhoodDask.time_mean('120km', 'time_chunks')
9.69±0.08ms 9.62±0.1ms 0.99 mpas_ocean.NeighborhoodDask.time_mean('480km', 'grid_chunks')
643±9μs 654±10μs 1.02 mpas_ocean.NeighborhoodDask.time_mean('480km', 'numpy')
6.61±0.04ms 6.62±0.05ms 1.00 mpas_ocean.NeighborhoodDask.time_mean('480km', 'time_chunks')
5.9M 5.74M 0.97 mpas_ocean.NeighborhoodDask.track_peakmem_mean('120km', 'grid_chunks')
2.75M 2.75M 1.00 mpas_ocean.NeighborhoodDask.track_peakmem_mean('120km', 'numpy')
5.68M 5.68M 1.00 mpas_ocean.NeighborhoodDask.track_peakmem_mean('120km', 'time_chunks')
678k 628k 0.93 mpas_ocean.NeighborhoodDask.track_peakmem_mean('480km', 'grid_chunks')
177k 177k 1.00 mpas_ocean.NeighborhoodDask.track_peakmem_mean('480km', 'numpy')
536k 536k 1.00 mpas_ocean.NeighborhoodDask.track_peakmem_mean('480km', 'time_chunks')
4.50±0s 4.52±0s 1.01 mpas_ocean.NeighborhoodReduce.time_dataset_reduce('120km', 'mean')
4.61±0.01s 4.64±0s 1.01 mpas_ocean.NeighborhoodReduce.time_dataset_reduce('120km', 'median')
122±0.1ms 122±1ms 1.01 mpas_ocean.NeighborhoodReduce.time_dataset_reduce('480km', 'mean')
126±1ms 123±0.8ms 0.98 mpas_ocean.NeighborhoodReduce.time_dataset_reduce('480km', 'median')
513±3ms 511±2ms 1.00 mpas_ocean.NeighborhoodReduce.time_neighborhood_reduce('120km', 'mean')
543±3ms 547±4ms 1.01 mpas_ocean.NeighborhoodReduce.time_neighborhood_reduce('120km', 'median')
17.1±0.05ms 16.9±0.04ms 0.99 mpas_ocean.NeighborhoodReduce.time_neighborhood_reduce('480km', 'mean')
17.1±0.02ms 17.4±0.06ms 1.02 mpas_ocean.NeighborhoodReduce.time_neighborhood_reduce('480km', 'median')
4.59±0.09ms 4.57±0.04ms 0.99 mpas_ocean.NeighborhoodReduce.time_reduce('120km', 'mean')
37.3±0.2ms 37.2±0.2ms 1.00 mpas_ocean.NeighborhoodReduce.time_reduce('120km', 'median')
250±10μs 243±7μs 0.97 mpas_ocean.NeighborhoodReduce.time_reduce('480km', 'mean')
562±6μs 549±10μs 0.98 mpas_ocean.NeighborhoodReduce.time_reduce('480km', 'median')
234k 234k 1.00 mpas_ocean.NeighborhoodReduce.track_peakmem_reduce('120km', 'mean')
235k 235k 1.00 mpas_ocean.NeighborhoodReduce.track_peakmem_reduce('120km', 'median')
19.5k 19.5k 1.00 mpas_ocean.NeighborhoodReduce.track_peakmem_reduce('480km', 'mean')
19.5k 19.5k 1.00 mpas_ocean.NeighborhoodReduce.track_peakmem_reduce('480km', 'median')
356±10μs 364±30μs 1.02 mpas_ocean.PointInPolygon.time_face_search_lonlat('120km')
351±10μs 347±20μs 0.99 mpas_ocean.PointInPolygon.time_face_search_lonlat('480km')
340±40μs 340±20μs 1.00 mpas_ocean.PointInPolygon.time_face_search_xyz('120km')
334±20μs 333±20μs 1.00 mpas_ocean.PointInPolygon.time_face_search_xyz('480km')
103±0.4ms 105±1ms 1.02 mpas_ocean.RemapDownsample.time_bilinear_remapping
16.9±0.1ms 16.9±0.06ms 1.00 mpas_ocean.RemapDownsample.time_inverse_distance_weighted_remapping
15.3±0.1ms 15.2±0.06ms 0.99 mpas_ocean.RemapDownsample.time_nearest_neighbor_remapping
483±5ms 494±2ms 1.02 mpas_ocean.RemapUpsample.time_bilinear_remapping
16.2±0.2ms 17.8±0.2ms 1.10 mpas_ocean.RemapUpsample.time_inverse_distance_weighted_remapping
6.33±0.02ms 6.85±0.02ms 1.08 mpas_ocean.RemapUpsample.time_nearest_neighbor_remapping
8.03±0.08ms 8.00±0.08ms 1.00 mpas_ocean.ZonalAverage.time_zonal_average('120km')
4.73±0.06ms 4.77±0.08ms 1.01 mpas_ocean.ZonalAverage.time_zonal_average('480km')
1.0260462774350552 1.0291495920389624 1.00 nogil_scaling.GILScaling.track_gil_scaling
312±2ms 312±5ms 1.00 plots.PlotHealpix.time_default_plot(6)
2.60±0.01s 2.63±0s 1.01 plots.PlotHealpix.time_default_plot(8)
28M 28M 1.00 plots.PlotHealpix.track_peakmem_default_plot(6)
443M 443M 1.00 plots.PlotHealpix.track_peakmem_default_plot(8)
4.09±0.03ms 4.46±0.2ms 1.09 quad_hexagon.QuadHexagon.time_open_dataset
3.39±0.03ms 3.72±0.1ms 1.10 quad_hexagon.QuadHexagon.time_open_grid
408 408 1.00 quad_hexagon.QuadHexagon.track_nbytes_open_dataset
392 392 1.00 quad_hexagon.QuadHexagon.track_nbytes_open_grid
74k 73.1k 0.99 quad_hexagon.QuadHexagon.track_peakmem_open_dataset
73.1k 72.4k 0.99 quad_hexagon.QuadHexagon.track_peakmem_open_grid
1.11±0s 1.13±0.01s 1.01 to_raster.ToRaster.time_to_raster((10000.0, 10.0))
2.71±0.02s 2.71±0.02s 1.00 to_raster.ToRaster.time_to_raster((10000.0, 100.0))
3.10±0.03s 3.12±0.03s 1.01 to_raster.ToRaster.time_to_raster((100000.0, 100.0))
271M 271M 1.00 to_raster.ToRaster.track_peakmem((10000.0, 10.0))
684M 684M 1.00 to_raster.ToRaster.track_peakmem((10000.0, 100.0))
768M 768M 1.00 to_raster.ToRaster.track_peakmem((100000.0, 100.0))

Benchmarks that have got worse:

Change Before [e2e5d6a] After [5a4d34f] Ratio Benchmark (Parameter)
+ 321M 411M 1.28 face_bounds.FaceBoundsColdStartRss.track_peakmem_open_and_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/mpas/QU/oQU480.231010.nc'))
+ 323M 411M 1.27 face_bounds.FaceBoundsColdStartRss.track_peakmem_open_and_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/scrip/outCSne8/outCSne8.nc'))
+ 323M 411M 1.27 face_bounds.FaceBoundsColdStartRss.track_peakmem_open_and_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/geoflow-small/grid.nc'))
+ 323M 411M 1.27 face_bounds.FaceBoundsColdStartRss.track_peakmem_open_and_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/quad-hexagon/grid.nc'))
+ 299M 411M 1.38 import.Imports.track_peakmem_import_uxarray
+ 2.04M 2.29M 1.13 lazy_grid_construction.OpenGridChunked.track_peakmem_open_grid('480km')
+ 342M 411M 1.2 mpas_ocean.CrossSectionsPeakMem.track_peakmem_const_lat('120km', 1)
+ 343M 411M 1.2 mpas_ocean.CrossSectionsPeakMem.track_peakmem_const_lat('120km', 2)
+ 342M 411M 1.2 mpas_ocean.CrossSectionsPeakMem.track_peakmem_const_lat('120km', 4)
+ 325M 411M 1.26 mpas_ocean.CrossSectionsPeakMem.track_peakmem_const_lat('480km', 1)
+ 325M 411M 1.26 mpas_ocean.CrossSectionsPeakMem.track_peakmem_const_lat('480km', 2)
+ 325M 411M 1.26 mpas_ocean.CrossSectionsPeakMem.track_peakmem_const_lat('480km', 4)
+ 1.62±0.02ms 1.84±0.07ms 1.14 mpas_ocean.DualMesh.time_dual_mesh_construction('480km')
+ 338M 410M 1.21 mpas_ocean.GradientColdStartRss.track_peakmem_gradient('120km')
+ 318M 410M 1.29 mpas_ocean.GradientColdStartRss.track_peakmem_gradient('480km')
+ 345M 410M 1.19 mpas_ocean.ZonalAveragePeakMem.track_peakmem_zonal_average('120km')
+ 325M 410M 1.26 mpas_ocean.ZonalAveragePeakMem.track_peakmem_zonal_average('480km')

@cmdupuis3
cmdupuis3 marked this pull request as ready for review September 28, 2026 21:02
@dylannelson

Copy link
Copy Markdown
Member

While hunting for where it may perform differently from main other than performance I think I found an example and a slowdown

  1. Subsetting seems like it can potentially result in lost faces (or at least the value stored in n_faces?)
  2. edge_lat may be slower now when ran many times, but if this isn't commonly looped the slowdown may be negligible (up to you)

Notebooks side by side (left is this branch, right is main):
image

Code to try if you want to see how it works on your end/env/os:

grid = ux.open_grid(MESH + r"\exodus\outCSne8\outCSne8.g")

section = grid.subset.constant_longitude(90.0)
box = grid.subset.bounding_box(lon_bounds=(-50, 50), lat_bounds=(-85, 85))

print("faces on the section at lon = 90:", section.n_face)
print("faces in the box lon -50..50:    ", box.n_face)

and

grid = ux.open_grid(MESH + r"\mpas\QU\oQU480.231010.nc", chunks={"n_node": 1000})

for step in range(100):
    grid.edge_lat

start = time.perf_counter()
grid.node_lon.values
elapsed = time.perf_counter() - start

print("dask graph layers behind node_lon:", len(grid.node_lon.data.dask.layers))
print(f"time to load node_lon: {1000 * elapsed:.0f} ms")

@Sevans711 Sevans711 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The benchmarks seem reasonable and show improvements which is a good sign that this is working as intended. (I see that the lazy_grid_construction.OpenGridChunked.time_open_grid('120km') benchmark shows ratio of 0.91, even though it wasn't listed directly in the "improved" section.)

Looks reasonable overall, just left some minor comments in-line!

Comment thread uxarray/grid/coordinates.py Resolved
Comment thread uxarray/grid/coordinates.py Resolved
Comment thread uxarray/grid/coordinates.py Resolved
cmdupuis3 and others added 2 commits October 9, 2026 12:52
Every Dataset assignment shallow-copies all Variables, so memoizing the
wrap on Variable identity missed as soon as a second longitude was
wrapped. Each edge_lat access then re-wrapped all three, growing the
dask graph without bound. Key the memo on the backing array instead,
call the wrap only when edge_lat is first populated, and drop the
getattr fallback now that Grid always sets _wrapped_lon_vars.

Revert _xyz_to_lonlat_deg to main's version. The arctan2 form exposed
~1e-16 noise in outCSne8.g nodes as -2.3e-14 longitudes, which gave
four faces [-180, 180] bounds and changed constant_longitude and
bounding_box results. Restore _assert_lon_close in test_io to match.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cmdupuis3

Copy link
Copy Markdown
Collaborator Author

@dylannelson The slowdown is related to caching, if you run each of them twice and compare the second-round timings, they should be similar. The difference in face counts is a divergence which was introduced in the fix for the .max() call, it should be fixed now.

@dylannelson dylannelson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Checked the previously discussed items again and everything looks to be fixed and working as expected. Thanks!

@Sevans711 Sevans711 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good and all of my previous comments have been addressed. Approving!

@cmdupuis3
cmdupuis3 merged commit db33c50 into UXARRAY:main Oct 26, 2026
35 of 36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-benchmark Run ASV benchmark workflow scalability Related to scalability & performance efforts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_set_desired_longitude_range forces materialization in open_grid

3 participants