Repository navigation
Conversation
Locally seeing a small speedup from this, in grid.bounds: 140±5 ms --> 127±1 ms, for a grid with 150k faces. Also removes small only-used-once helper functions `any_close_lat` and `all_elements_nan`, inlining the relevant logic instead.
No noticeable effect on speed from this particular commit, which is unsurprising because it only affects the ``if has_north_pole or has_south_pole`` path.
Seeing a pretty significant speedup locally now; grid.bounds for the same grid as in d75fed0 is down to 89 ± 2ms (from 140 ± 5 ms on main). One (significant) improvement remains possible: refactoring pole_point_in_polygon to use nodes_idx instead of face_edges would allow _construct_face_bounds to entirely avoid tiny numpy arrays. For "nodes_idx versus face_edges" explanation, see PR #1809. Still need to update test suite appropriately for the new syntax here.
cherry picked directly from older geometry.py optimizations attempt (see #1790). This commit is just the cherry pick, plus resolving some conflicts (with imports) and applying updated ruff formatting. More speedup: grid.bounds for the same grid as in d75fed0 is down to 78 ± 2ms (from 140 ± 5 ms on main, and 89 ± 2ms on f5ca4a4), and still bit-for-bit identical. (cherry picked from commit f3c4e22)
After the latest commit, face_edges_lonlat is now unused in _construct_face_bounds because it is unused in pole_point_inside_polygon. Removing it makes a tiny speedup compared to previous commit; 78 ± 2ms --> 75 ± 2ms on the example grid I've been testing.
This makes a small speedup compared to the previous commit, 75 ± 2ms --> 71 ± 1ms on the example grid I've been testing.
tiny speedup, only ~1-2 ms faster than previous commit's 71 ± 1ms, on the example grid I've been using. My example grid has no pole points, so as a sanity check I also tried having pole_point_inside_polygon just return False. That showed 48 ± 1ms, i.e. pole_point_inside_polygon optimizations alone cannot bring the speed lower than that.
The bug was introduced recently, in commit 3cd6a5e.
ASV BenchmarkingBenchmark Comparison ResultsBenchmarks that have improved:
Benchmarks that have stayed the same:
Benchmarks that have got worse:
|
Sevans711
marked this pull request as ready for review
December 1, 2026 22:13
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.
Closes #1896
Overview
Optimizes numba routines in
uxarray/grid/bounds.pyto avoid constructing many tiny numpy arrays inside numba routines, as discussed in #1648. I expect results should be bit-for-bit identical even after these changes.Similarly, optimizes
pole_point_inside_polygon(from geometry.py) by avoiding many tiny numpy arrays, because it is used inside bounds.py methods. This is a large expansion of scope, but it makes a noticeable difference. In my 150k faces example, I was seeing ~140ms for grid.bounds on main, ~90ms for grid.bounds after the bounds.py optimizations alone, and ~70ms after incorporating thepole_point_inside_polygonoptimizations as well. Many of thepole_point_inside_polygonchanges were cherry-picked from commits in #1790, which sought to similarly optimize geometry.py but isn't yet finished/merged.Performance improvements:
Nontrivial / "slightly surprising" changes (but, all within the original scope):
_face_contains_pointwith input face_edges to_point_in_facewith input nodes_idx, node_x, node_y, and node_z. That PR writeup explains in more detail, but the basic idea is just that rewriting the relevant indexing logic enables avoiding the construction of many tiny face_edges arrays. This affects:_construct_face_bounds_get_cartesian_face_edge_nodesand_get_spherical_face_edge_nodeshelper methods.pole_point_inside_polygon_check_intersection_check_pole_ref_edge_intersection._check_pole_ref_edge_intersectionto usehemisphere_filterinternally, to avoid building the tiny arrays north_edges_xyz and south_edges_xyz insidepole_point_inside_polygon.extreme_gca_latitudeto include a few np.float64() conversions. Inputs at all internal call sites previously were already float64, but the refactor above led to float32 inputs sometimes (whenever node_x, etc were stored in float32, which can occur by default). Upcasting to float64 ensures higher precision math (and allows bit-for-bit match to results onmain).Small expansions of scope:
any_close_latandall_elements_nanhelper functions which were each only being used in one location._point_in_facedocstring to avoid incorrect implication thatnodes_idxhas same shape as node_x, node_y, and node_z.Misc. note:
intersection_pointsarray while keeping exactly equivalent uniqueness tests during_unique_points(), inside_check_pole_ref_edge_intersection. However, when checking on a 150k faces grid with intersection_points == 0 always, even when I commented out all pieces of the code relevant to these allocations and the uniqueness checks, the speedup was only ~1%. This suggests that keeping the allocation inside the logical branch where it is now is already very close to optimal, it doesn't prevent any significant numba LLVM optimizations, and it's probably not worthwhile to optimize further.PR Checklist
General
Testing & Benchmarking
Documentation and Examples
docs/api.rst; internal (private) function names start with an underscore (_)AI Disclosure
AI Usage: Claude for some debugging, test suite updates, and later rounds of optimizations, but initial rounds of optimizations here were done without Claude. Also used Github Copilot inline coding suggestions.