perf: optimize meshgrid for reduced allocations and improved type stability#87
Closed
ChrisRackauckas-Claude wants to merge 1 commit intoSciML:mainfrom
Closed
Conversation
…bility
The meshgrid function is used by GridEmbedding to generate coordinate grids.
This optimization provides significant performance improvements:
## Benchmarks
### meshgrid 1D (128 points):
- BEFORE: ~2.85 μs, 1.48 KiB allocated, 15 allocations
- AFTER: ~79 ns, 624 bytes allocated, 3 allocations
- Improvement: ~36x faster, 58% less memory, 80% fewer allocations
### meshgrid 2D (64x64 points):
- BEFORE: ~19.8 μs, 65.04 KiB allocated, 29 allocations
- AFTER: ~4.4 μs, 33 KiB allocated, 31 allocations
- Improvement: ~4.5x faster, 49% less memory
## Changes
- Pre-allocate output array with `similar()` instead of using `stack()`
- Use in-place broadcasting with `selectdim()` views instead of `repeat()`
- Add type parameter `T` for better type inference
- Use `ntuple()` instead of mutable array for shape computation
## Type Stability
The original implementation returned `Any` from @code_warntype.
The new implementation returns concrete types (Matrix{T}, Array{T,3}, etc.)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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.
Summary
Optimizes the
meshgridfunction used byGridEmbeddingto generate coordinate grids. The optimization provides significant performance improvements through:similar()instead of usingstack()selectdim()views instead ofrepeat()Tfor better type inferencentuple()instead of mutable array for shape computationBenchmarks
meshgrid 1D (128 points):
meshgrid 2D (64x64 points):
GridEmbedding 1D (128 points, 4 channels, batch=32):
Type Stability
The original implementation returned
Anyfrom@code_warntypedue to the use ofstack()with a closure. The new implementation returns concrete types (Matrix{T},Array{T,3}, etc.), improving type inference throughout the call chain.Test plan
@code_warntypeshows concrete return typescc @ChrisRackauckas
🤖 Generated with Claude Code