Skip to content

Avoid tiny numpy arrays in numba: optimize arcs.py - #1895

Merged
cmdupuis3 merged 8 commits into
mainfrom
sevans/tinynumpynumba-optimize-arcs.py
Nov 23, 2026
Merged

cmdupuis3 merged 8 commits into
mainfrom
sevans/tinynumpynumba-optimize-arcs.py

Conversation

@Sevans711

@Sevans711 Sevans711 commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1894

Overview

Optimizes numba routines in uxarray/grid/arcs.py to avoid constructing many tiny numpy arrays inside numba routines, as discussed in #1648. Also updated call sites (in bounds.py) of extreme_gca_latitude (which is defined in arcs.py) to utilize tuples instead of constructing tiny numpy arrays. Locally seeing ~5-20% speedup when constructing grid.bounds (excluding any cold-start numba compile time).

Small expansions of scope:

  • Removed the _to_list helper function which was not being used anywhere.
  • Removed the internal max3 and min3 helper functions; just use Python's max and min instead.
  • Removed _orient3d_on_sphere_value_xyz; just use _orient3d_on_sphere_value instead.
  • EDIT: (as per comment below), removed unnecessary if isclose...: clip_scalar(...) code blocks.

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
  • [N/A] User (public) functions added to docs/api.rst; internal (private) function names start with an underscore (_)
  • [N/A] If touched any notebook files, cleared the output of all cells before committing
  • [N/A] If added new notebook files, put into appropriate directories and referenced in appropriate files

AI Disclosure

AI Usage: mostly just GitHub Copilot's inline code suggestions; one tiny sanity check (about the max3 helper function) with Claude.

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

@Sevans711 Sevans711 added scalability Related to scalability & performance efforts run-benchmark Run ASV benchmark workflow labels Nov 12, 2026
Locally I'm seeing grid.bounds speedup of roughly 5-20% (~30ms became ~25ms for a 28k faces example; ~1.72s became ~1.59s for a 2.6M faces examples). This is enough to confirm the changes in arcs.py helped with something at least a little bit! Not surprising that the speedup isn't huge though, there are still many more tiny numpy allocations occurring in bounds.py.
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

ASV Benchmarking

Benchmark Comparison Results

Benchmarks that have improved:

Change Before [755ce6e] After [02a59d3] Ratio Benchmark (Parameter)
- 2.05M 1.78M 0.87 face_bounds.FaceBounds.track_peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/mpas/QU/oQU480.231010.nc'))
- 2.03M 1.76M 0.87 face_bounds.FaceBounds.track_peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/scrip/outCSne8/outCSne8.nc'))
- 2.19M 1.92M 0.88 face_bounds.FaceBounds.track_peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/geoflow-small/grid.nc'))
- 754k 609k 0.81 mpas_ocean.NeighborhoodDask.track_peakmem_mean('480km', 'grid_chunks')
- 2.38M 2.12M 0.89 subset.BoundingBoxHealpix.track_peakmem_bounding_box(6)

Benchmarks that have stayed the same:

Change Before [755ce6e] After [02a59d3] Ratio Benchmark (Parameter)
1.02±0.02ms 1.01±0.02ms 0.99 connectivity.Connectivity.time_edge_face('120km')
329±10μs 335±10μs 1.02 connectivity.Connectivity.time_edge_face('480km')
3.74±0.06ms 3.76±0.09ms 1.01 connectivity.Connectivity.time_edge_node('120km')
774±30μs 773±40μs 1.00 connectivity.Connectivity.time_edge_node('480km')
46.7±4μs 45.1±4μs 0.97 connectivity.Connectivity.time_face_edge('120km')
42.4±7μs 38.6±9μs 0.91 connectivity.Connectivity.time_face_edge('480km')
809±20μs 780±30μs 0.97 connectivity.Connectivity.time_face_face('120km')
311±8μs 303±8μs 0.98 connectivity.Connectivity.time_face_face('480km')
54.3±4μs 48.5±10μs ~0.89 connectivity.Connectivity.time_face_node('120km')
44.0±3μs 42.4±3μs 0.97 connectivity.Connectivity.time_face_node('480km')
527±20μs 486±30μs 0.92 connectivity.Connectivity.time_n_nodes_per_face('120km')
286±20μs 276±30μs 0.97 connectivity.Connectivity.time_n_nodes_per_face('480km')
1.08±0.05ms 1.12±0.06ms 1.04 connectivity.Connectivity.time_node_edge('120km')
300±7μs 309±6μs 1.03 connectivity.Connectivity.time_node_edge('480km')
55.0±0.4ms 53.8±0.4ms 0.98 connectivity.Connectivity.time_node_face('120km')
3.47±0.02ms 3.42±0.03ms 0.99 connectivity.Connectivity.time_node_face('480km')
1.42M 1.42M 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_edge_face('120km')
105k 105k 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.05k 2.05k 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_face_edge('120km')
2.05k 2.05k 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.18k 2.18k 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_face_node('120km')
2.18k 2.18k 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_face_node('480km')
240k 240k 1.00 connectivity.ConnectivityTracemalloc.track_peakmem_n_nodes_per_face('120km')
26k 26k 1.00 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.17±1ms 4.78±1ms 0.93 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/mpas/QU/oQU480.231010.nc'))
1.41±0.1ms 1.56±0.4ms ~1.11 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/scrip/outCSne8/outCSne8.nc'))
5.68±0.5ms 5.89±0.6ms 1.04 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/geoflow-small/grid.nc'))
802±70μs 736±50μs 0.92 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'))
516k 516k 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'))
35.3k 35.3k 1.00 face_bounds.FaceBounds.track_peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/quad-hexagon/grid.nc'))
916±30ns 803±60ns ~0.88 geometry_kernels.AccucrossKernels.time_accucross
1.85±0.04μs 1.77±0.04μs 0.96 geometry_kernels.AccucrossKernels.time_accucross_pair
342±40ns 374±30ns 1.10 geometry_kernels.EFTPrimitives.time_acc_sqrt_re
335±30ns 376±40ns ~1.13 geometry_kernels.EFTPrimitives.time_diff_of_products
297±40ns 295±20ns 0.99 geometry_kernels.EFTPrimitives.time_two_prod
323±60ns 314±40ns 0.97 geometry_kernels.EFTPrimitives.time_two_sum
345±9ns 361±10ns 1.04 geometry_kernels.GCAConstLatIntersection.time_accux_constlat_kernel
430±10ns 421±5ns 0.98 geometry_kernels.GCAConstLatIntersection.time_gca_const_lat_intersection
435±20ns 411±30ns 0.94 geometry_kernels.GCAConstLatIntersection.time_try_gca_const_lat_intersection
595±30ns 641±30ns 1.08 geometry_kernels.GCAGCAIntersection.time_accux_gca_kernel
836±20ns 781±10ns 0.93 geometry_kernels.GCAGCAIntersection.time_gca_gca_intersection
835±20ns 838±70ns 1.00 geometry_kernels.GCAGCAIntersection.time_try_gca_gca_intersection
20.7±1μs 21.5±1μs 1.04 geometry_kernels.OrientPredicates.time_on_minor_arc
20.2±0.6μs 25.9±30μs ~1.28 geometry_kernels.OrientPredicates.time_orient3d_on_sphere
2.18±0.05ms 2.16±0.01ms 0.99 geometry_samebody.SameBodyConstLat.time_accux_dispatch
650±6μs 665±10μs 1.02 geometry_samebody.SameBodyConstLat.time_accux_kernel
2.16±0.07ms 2.22±0.1ms 1.03 geometry_samebody.SameBodyConstLat.time_fp64_dispatch
79.2±0.2μs 79.3±0.07μs 1.00 geometry_samebody.SameBodyConstLat.time_fp64_kernel
25.6±0.3ms 26.1±0.07ms 1.02 geometry_samebody_gcagca.SameBodyGcaGca.time_accux_dispatch
5.53±0.01ms 5.55±0ms 1.00 geometry_samebody_gcagca.SameBodyGcaGca.time_accux_kernel
20.1±0.07ms 20.5±0.05ms 1.02 geometry_samebody_gcagca.SameBodyGcaGca.time_fp64_dispatch
695±3μs 697±4μs 1.00 geometry_samebody_gcagca.SameBodyGcaGca.time_fp64_kernel
713±10ms 721±9ms 1.01 import.Imports.timeraw_import_uxarray
1 1 1.00 lazy_grid_construction.LazyGridConstruction.track_computes_isel
2 2 1.00 lazy_grid_construction.LazyGridConstruction.track_computes_open_grid_chunked
95.2±0.7ms 96.2±0.4ms 1.01 lazy_grid_construction.OpenGridChunked.time_open_grid('120km')
134±2ms 134±1ms 1.00 lazy_grid_construction.OpenGridChunked.time_open_grid('480km')
2.01M 2.01M 1.00 lazy_grid_construction.OpenGridChunked.track_peakmem_open_grid('120km')
2.13M 2.32M 1.09 lazy_grid_construction.OpenGridChunked.track_peakmem_open_grid('480km')
1.84±0.03ms 1.84±0.01ms 1.00 mpas_ocean.AzimuthalAverage.time_azimuthal_average('120km')
740±10μs 736±9μs 1.00 mpas_ocean.AzimuthalAverage.time_azimuthal_average('480km')
1.56±0.02ms 1.56±0ms 1.00 mpas_ocean.CheckNorm.time_check_norm('120km')
1.19±0.1ms 1.19±0.02ms 1.00 mpas_ocean.CheckNorm.time_check_norm('480km')
1.17±0.03ms 1.14±0.02ms 0.98 mpas_ocean.ConnectivityConstruction.time_face_face_connectivity('120km')
554±20μs 526±7μs 0.95 mpas_ocean.ConnectivityConstruction.time_face_face_connectivity('480km')
670±10μs 666±20μs 0.99 mpas_ocean.ConnectivityConstruction.time_n_nodes_per_face('120km')
594±10μs 589±10μs 0.99 mpas_ocean.ConnectivityConstruction.time_n_nodes_per_face('480km')
2.42±0.07ms 2.52±0.2ms 1.04 mpas_ocean.ConstructFaceLatLon.time_cartesian_averaging('120km')
1.91±0.06ms 1.93±0.04ms 1.01 mpas_ocean.ConstructFaceLatLon.time_cartesian_averaging('480km')
80.4±5ms 72.3±0.7ms ~0.90 mpas_ocean.ConstructFaceLatLon.time_welzl('120km')
6.40±0.7ms 6.35±0.1ms 0.99 mpas_ocean.ConstructFaceLatLon.time_welzl('480km')
12.5±0.05ms 12.7±0.2ms 1.01 mpas_ocean.ConstructTreeStructures.time_ball_tree('120km')
591±20μs 633±50μs 1.07 mpas_ocean.ConstructTreeStructures.time_ball_tree('480km')
6.29±0.08ms 6.30±0.1ms 1.00 mpas_ocean.ConstructTreeStructures.time_kd_tree('120km')
330±9μs 358±50μs 1.08 mpas_ocean.ConstructTreeStructures.time_kd_tree('480km')
646±5ms 658±7ms 1.02 mpas_ocean.CrossSections.time_const_lat('120km', 1)
333±9ms 333±0.9ms 1.00 mpas_ocean.CrossSections.time_const_lat('120km', 2)
171±4ms 168±3ms 0.98 mpas_ocean.CrossSections.time_const_lat('120km', 4)
585±4ms 590±2ms 1.01 mpas_ocean.CrossSections.time_const_lat('480km', 1)
291±2ms 288±3ms 0.99 mpas_ocean.CrossSections.time_const_lat('480km', 2)
148±0.7ms 151±2ms 1.02 mpas_ocean.CrossSections.time_const_lat('480km', 4)
20.9±0.02ms 21.7±0.1ms 1.04 mpas_ocean.DualMesh.time_dual_mesh_construction('120km')
2.52±0.01ms 2.63±0.03ms 1.04 mpas_ocean.DualMesh.time_dual_mesh_construction('480km')
7.36±1ms 5.84±0.1ms ~0.79 mpas_ocean.FaceAreas.time_face_areas('120km')
2.21±0.2ms 2.10±0.3ms 0.95 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')
687k 687k 1.00 mpas_ocean.FaceAreas.track_peakmem_face_areas('480km')
53.8±0.5ms 53.7±1ms 1.00 mpas_ocean.GeoDataFrame.time_to_geodataframe('120km', 'exclude')
157±2ms 153±2ms 0.97 mpas_ocean.GeoDataFrame.time_to_geodataframe('120km', 'split')
4.94±0.2ms 4.88±0.2ms 0.99 mpas_ocean.GeoDataFrame.time_to_geodataframe('480km', 'exclude')
29.7±0.1ms 29.9±0.6ms 1.01 mpas_ocean.GeoDataFrame.time_to_geodataframe('480km', 'split')
10.3±0.4ms 9.37±0.2ms 0.91 mpas_ocean.Gradient.time_gradient('120km')
1.63±0.01ms 1.64±0.02ms 1.00 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.44M 3.44M 1.00 mpas_ocean.Gradient.track_peakmem_gradient('120km')
226k 226k 1.00 mpas_ocean.Gradient.track_peakmem_gradient('480km')
327±10μs 314±6μs 0.96 mpas_ocean.HoleEdgeIndices.time_construct_hole_edge_indices('120km')
156±3μs 155±8μs 1.00 mpas_ocean.HoleEdgeIndices.time_construct_hole_edge_indices('480km')
193±10μs 181±5μs 0.94 mpas_ocean.Integrate.time_integrate('120km')
169±2μs 160±8μs 0.95 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')
82.1±0.9ms 82.4±0.7ms 1.00 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('120km', 'exclude')
82.5±1ms 84.6±1ms 1.02 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('120km', 'include')
82.5±0.7ms 83.3±0.9ms 1.01 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('120km', 'split')
5.99±0.05ms 6.04±0.04ms 1.01 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('480km', 'exclude')
5.97±0.02ms 6.19±0.1ms 1.04 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('480km', 'include')
6.09±0.06ms 6.27±0.2ms 1.03 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('480km', 'split')
236±2ms 242±0.8ms 1.02 mpas_ocean.NeighborhoodBuild.time_build('120km', 1.0)
490±2ms 495±2ms 1.01 mpas_ocean.NeighborhoodBuild.time_build('120km', 5.0)
12.6±0.02ms 13.0±0.04ms 1.03 mpas_ocean.NeighborhoodBuild.time_build('480km', 1.0)
15.8±0.1ms 16.0±0.01ms 1.02 mpas_ocean.NeighborhoodBuild.time_build('480km', 5.0)
232±2ms 231±0.7ms 0.99 mpas_ocean.NeighborhoodBuild.time_query_radius('120km', 1.0)
483±3ms 487±0.5ms 1.01 mpas_ocean.NeighborhoodBuild.time_query_radius('120km', 5.0)
12.4±0.02ms 12.6±0.03ms 1.02 mpas_ocean.NeighborhoodBuild.time_query_radius('480km', 1.0)
15.4±0.1ms 15.5±0.2ms 1.01 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)
824k 824k 1.00 mpas_ocean.NeighborhoodBuild.track_peakmem_build('480km', 15.0)
384k 384k 1.00 mpas_ocean.NeighborhoodBuild.track_peakmem_build('480km', 5.0)
30.7±0.6ms 30.2±1ms 0.98 mpas_ocean.NeighborhoodDask.time_mean('120km', 'grid_chunks')
13.9±0.2ms 13.8±0.1ms 0.99 mpas_ocean.NeighborhoodDask.time_mean('120km', 'numpy')
28.2±0.6ms 27.4±0.8ms 0.97 mpas_ocean.NeighborhoodDask.time_mean('120km', 'time_chunks')
7.52±0.1ms 7.49±0.08ms 1.00 mpas_ocean.NeighborhoodDask.time_mean('480km', 'grid_chunks')
448±3μs 454±20μs 1.01 mpas_ocean.NeighborhoodDask.time_mean('480km', 'numpy')
5.32±0.1ms 5.23±0.1ms 0.98 mpas_ocean.NeighborhoodDask.time_mean('480km', 'time_chunks')
5.82M 5.75M 0.99 mpas_ocean.NeighborhoodDask.track_peakmem_mean('120km', 'grid_chunks')
2.75M 2.75M 1.00 mpas_ocean.NeighborhoodDask.track_peakmem_mean('120km', 'numpy')
5.67M 5.67M 1.00 mpas_ocean.NeighborhoodDask.track_peakmem_mean('120km', 'time_chunks')
177k 177k 1.00 mpas_ocean.NeighborhoodDask.track_peakmem_mean('480km', 'numpy')
528k 528k 1.00 mpas_ocean.NeighborhoodDask.track_peakmem_mean('480km', 'time_chunks')
2.96±0.01s 2.96±0.01s 1.00 mpas_ocean.NeighborhoodReduce.time_dataset_reduce('120km', 'mean')
3.05±0.01s 3.06±0.05s 1.00 mpas_ocean.NeighborhoodReduce.time_dataset_reduce('120km', 'median')
77.0±1ms 80.4±4ms 1.04 mpas_ocean.NeighborhoodReduce.time_dataset_reduce('480km', 'mean')
83.9±2ms 83.8±1ms 1.00 mpas_ocean.NeighborhoodReduce.time_dataset_reduce('480km', 'median')
336±2ms 335±0.9ms 1.00 mpas_ocean.NeighborhoodReduce.time_neighborhood_reduce('120km', 'mean')
356±0.3ms 360±5ms 1.01 mpas_ocean.NeighborhoodReduce.time_neighborhood_reduce('120km', 'median')
10.7±0.04ms 11.3±0.9ms 1.05 mpas_ocean.NeighborhoodReduce.time_neighborhood_reduce('480km', 'mean')
11.1±0.4ms 11.4±0.4ms 1.03 mpas_ocean.NeighborhoodReduce.time_neighborhood_reduce('480km', 'median')
2.34±0.01ms 2.41±0.05ms 1.03 mpas_ocean.NeighborhoodReduce.time_reduce('120km', 'mean')
27.1±0.4ms 26.0±0.7ms 0.96 mpas_ocean.NeighborhoodReduce.time_reduce('120km', 'median')
107±0.9μs 111±0.5μs 1.03 mpas_ocean.NeighborhoodReduce.time_reduce('480km', 'mean')
318±4μs 323±7μs 1.02 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.6k 19.6k 1.00 mpas_ocean.NeighborhoodReduce.track_peakmem_reduce('480km', 'median')
278±4μs 259±5μs 0.93 mpas_ocean.PointInPolygon.time_face_search_lonlat('120km')
237±3μs 234±4μs 0.99 mpas_ocean.PointInPolygon.time_face_search_lonlat('480km')
254±10μs 244±5μs 0.96 mpas_ocean.PointInPolygon.time_face_search_xyz('120km')
226±7μs 219±5μs 0.97 mpas_ocean.PointInPolygon.time_face_search_xyz('480km')
66.0±2ms 69.2±5ms 1.05 mpas_ocean.RemapDownsample.time_bilinear_remapping
11.1±0.3ms 11.8±0.3ms 1.06 mpas_ocean.RemapDownsample.time_inverse_distance_weighted_remapping
10.0±0.3ms 10.4±0.8ms 1.04 mpas_ocean.RemapDownsample.time_nearest_neighbor_remapping
1.09±0s 1.07±0s 0.98 mpas_ocean.RemapUpsample.time_bilinear_remapping
24.0±0.07ms 22.4±0.1ms 0.93 mpas_ocean.RemapUpsample.time_inverse_distance_weighted_remapping
11.3±0.2ms 11.6±0.4ms 1.03 mpas_ocean.RemapUpsample.time_nearest_neighbor_remapping
5.76±0.07ms 5.81±0.07ms 1.01 mpas_ocean.ZonalAverage.time_zonal_average('120km')
3.23±0.01ms 3.26±0.04ms 1.01 mpas_ocean.ZonalAverage.time_zonal_average('480km')
0.9961246965458787 1.00189512220295 1.01 nogil_scaling.GILScaling.track_gil_scaling
967±10μs 966±10μs 1.00 open_dataset.OpenDataset.time_open_dataset('dataset', 'dataset')
1.81±0.02ms 1.82±0.02ms 1.00 open_dataset.OpenDataset.time_open_dataset('dataset', 'file')
3.71±0.03ms 3.72±0.01ms 1.00 open_dataset.OpenDataset.time_open_dataset('file', 'dataset')
4.60±0.02ms 4.50±0.03ms 0.98 open_dataset.OpenDataset.time_open_dataset('file', 'file')
149±4μs 152±4μs 1.02 open_dataset.OpenDataset.time_open_dataset('grid', 'dataset')
927±9μs 916±20μs 0.99 open_dataset.OpenDataset.time_open_dataset('grid', 'file')
1.34±0.01s 1.32±0.01s 0.99 plots.PlotContour.time_contour_projected
230±2ms 231±1ms 1.00 plots.PlotHealpix.time_default_plot(6)
2.20±0.01s 2.18±0.01s 0.99 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.64±0.1ms 4.55±0.2ms 0.98 quad_hexagon.QuadHexagon.time_open_dataset
3.66±0.2ms 3.80±0.2ms 1.04 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
71.8k 71.8k 1.00 quad_hexagon.QuadHexagon.track_peakmem_open_dataset
71.5k 71.5k 1.00 quad_hexagon.QuadHexagon.track_peakmem_open_grid
6.22±0.3ms 7.71±1ms ~1.24 subset.BoundingBox.time_bounding_box('120km')
3.50±0.2ms 3.52±0.6ms 1.01 subset.BoundingBox.time_bounding_box('480km')
2.38M 2.38M 1.00 subset.BoundingBox.track_peakmem_bounding_box('120km')
203k 203k 1.00 subset.BoundingBox.track_peakmem_bounding_box('480km')
3.97±0.06ms 3.98±0.07ms 1.00 subset.BoundingBoxHealpix.time_bounding_box(6)
15.3±0.4ms 14.6±0.3ms 0.95 subset.BoundingBoxHealpix.time_bounding_box(8)
13.2M 13.2M 1.00 subset.BoundingBoxHealpix.track_peakmem_bounding_box(8)
597±3ms 604±2ms 1.01 to_raster.ToRaster.time_to_raster((10000.0, 10.0))
1.29±0.01s 1.29±0.01s 1.01 to_raster.ToRaster.time_to_raster((10000.0, 100.0))
1.51±0.01s 1.52±0.01s 1.01 to_raster.ToRaster.time_to_raster((100000.0, 100.0))
270M 270M 1.00 to_raster.ToRaster.track_peakmem((10000.0, 10.0))
680M 680M 1.00 to_raster.ToRaster.track_peakmem((10000.0, 100.0))
765M 765M 1.00 to_raster.ToRaster.track_peakmem((100000.0, 100.0))
70.9±0.9μs 70.9±1μs 1.00 to_xarray.UxDataArrayToXarray.time_to_xarray(10)
234±4μs 232±5μs 0.99 to_xarray.UxDatasetToXarray.time_to_xarray((100, 10))
2.11±0.01ms 2.13±0.02ms 1.01 to_xarray.UxDatasetToXarray.time_to_xarray((1000, 100))
508±10μs 500±6μs 0.98 to_xarray.UxDatasetToXarray.time_to_xarray_healpix((100, 10))
4.59±0.03ms 4.56±0.04ms 0.99 to_xarray.UxDatasetToXarray.time_to_xarray_healpix((1000, 100))

Benchmarks that have got worse:

Change Before [755ce6e] After [02a59d3] Ratio Benchmark (Parameter)
+ 306M 391M 1.28 face_bounds.FaceBoundsColdStartRss.track_peakmem_open_and_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/mpas/QU/oQU480.231010.nc'))
+ 307M 391M 1.27 face_bounds.FaceBoundsColdStartRss.track_peakmem_open_and_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/scrip/outCSne8/outCSne8.nc'))
+ 309M 391M 1.27 face_bounds.FaceBoundsColdStartRss.track_peakmem_open_and_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/geoflow-small/grid.nc'))
+ 308M 391M 1.27 face_bounds.FaceBoundsColdStartRss.track_peakmem_open_and_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/quad-hexagon/grid.nc'))
+ 284M 391M 1.38 import.Imports.track_peakmem_import_uxarray
+ 327M 391M 1.19 mpas_ocean.CrossSectionsPeakMem.track_peakmem_const_lat('120km', 1)
+ 327M 391M 1.19 mpas_ocean.CrossSectionsPeakMem.track_peakmem_const_lat('120km', 2)
+ 327M 391M 1.19 mpas_ocean.CrossSectionsPeakMem.track_peakmem_const_lat('120km', 4)
+ 308M 391M 1.27 mpas_ocean.CrossSectionsPeakMem.track_peakmem_const_lat('480km', 1)
+ 308M 391M 1.27 mpas_ocean.CrossSectionsPeakMem.track_peakmem_const_lat('480km', 2)
+ 308M 391M 1.27 mpas_ocean.CrossSectionsPeakMem.track_peakmem_const_lat('480km', 4)
+ 328M 391M 1.19 mpas_ocean.GradientColdStartRss.track_peakmem_gradient('120km')
+ 301M 391M 1.3 mpas_ocean.GradientColdStartRss.track_peakmem_gradient('480km')
+ 328M 391M 1.19 mpas_ocean.ZonalAveragePeakMem.track_peakmem_zonal_average('120km')
+ 309M 391M 1.26 mpas_ocean.ZonalAveragePeakMem.track_peakmem_zonal_average('480km')

@Sevans711

Copy link
Copy Markdown
Collaborator Author

Marking as ready for review - tests all passing and ASV suite roughly agrees with my expectations, i.e. maybe some small improvements to some bounds methods, not really any huge improvements, but also no notable degradations (aside from examples covered by #1891).

@Sevans711
Sevans711 marked this pull request as ready for review November 16, 2026 17:20
@Sevans711
Sevans711 requested review from cmdupuis3 and rajeeja and removed request for cmdupuis3 November 16, 2026 17:22

@cmdupuis3 cmdupuis3 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.

  1. Using np.max instead of max3 results in different NaN handling, is that intentional?

  2. extreme_gca_latitude and extreme_gca_z still call np.isclose on scalars. Under numba this is about 3x slower than an explicit abs(d - x) <= atol comparison

@Sevans711

Copy link
Copy Markdown
Collaborator Author
  1. Using np.max instead of max3 results in different NaN handling, is that intentional?

I was aware that Python's max and the helper function max3 treat NaNs differently (and similarly for min and min3), but I believe it doesn't matter. The lack of comments and tests about NaN handling for the max3 and min3 methods makes me think that the existing quirky NaN handling was not intentional. Also, at the call sites where it was being used I would expect only finite values, unless the input lat/lon values are nonfinite, in which case you likely have bigger problems than these routines!

  1. extreme_gca_latitude and extreme_gca_z still call np.isclose on scalars. Under numba this is about 3x slower than an explicit abs(d - x) <= atol comparison

A few thoughts on this:

  • Is this 3x improvement relevant when compared to the rest of the method? I would guess that the multiplications, divisions, and sometimes trig functions are relatively much more expensive than 3x the operations above (a single absolute value, a subtraction, and comparison).
  • The formula you provided is not mathematically equivalent to np.isclose; for finite values np.isclose(a, b, rtol, atol) is equivalent to abs(a - b) <= (atol + rtol * abs(b)). The default rtol is 1e-5, so a call like np.isclose(d_a_max, 0.0, atol=ERROR_TOLERANCE) is using rtol=1e-5, not 0.
  • If replacing np.isclose would make a noticeable impact on efficiency anywhere, I would probably prefer to scope "replace np.isclose(scalar inputs) with explicit math equation" to a different PR, where it can be the main focus, in order to properly consider any subtleties (like including rtol in the equation, and any infinite or NaN values).

@Sevans711
Sevans711 requested a review from cmdupuis3 November 16, 2026 23:37
@Sevans711 Sevans711 changed the title Avoid tiny numba arrays in numba: optimize arcs.py Avoid tiny numpy arrays in numba: optimize arcs.py Nov 17, 2026
@cmdupuis3

Copy link
Copy Markdown
Collaborator
  1. Actually yours is better, but I think there's still some weirdness about how NaNs are handled. I'll leave that for another time, any improvement is fine.

  2. Yeah, nvm. On looking further, the isclose/clip_scalar block in extreme_gca_latitude/extreme_gca_z doesn’t affect the result, because clipping to [0,1] is followed by a strict 0 < d < 1 test. Removing it gives bit-identical output on 400k random arcs, so it can just be deleted rather than rewritten.

@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.

Compared to main and observed 0 bitwise differences in this update. The speedup was observed and not seeing any changes in functionality that break existing methods/usage. Looks good, thanks!

These were never affecting the result, since they were doing A = clip_scalar(A, 0, 1) followed by using the altered A if and only if 0 < A < 1. The clip only affects A when NOT 0 < A < 1.
@Sevans711

Copy link
Copy Markdown
Collaborator Author
  1. Yeah, nvm. On looking further, the isclose/clip_scalar block in extreme_gca_latitude/extreme_gca_z doesn’t affect the result, because clipping to [0,1] is followed by a strict 0 < d < 1 test. Removing it gives bit-identical output on 400k random arcs, so it can just be deleted rather than rewritten.

Good point, I remember having a feeling before that those lines might not be doing much, and upon taking a closer look I agree with your assessment here. I removed the relevant lines and updated the PR overview message to mention this small expansion of scope.

@cmdupuis3 cmdupuis3 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.

I'm seeing that replacing np.dot with _numba_dot3 results in a one ulp difference, so it doesn't reproduce main, but that's minor enough that I'm approving and merging

@cmdupuis3
cmdupuis3 merged commit 8423b24 into main Nov 23, 2026
22 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.

Avoid tiny numpy arrays in numba: optimize arcs.py

3 participants