Performance improvements: reduce allocations in discretization hot paths #501
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
This PR reduces memory allocations in the core discretization hot paths, improving performance by approximately 5-6% on typical 1D problems.
Key optimizations:
reduce(safe_vcat, ...)patterns with pre-allocated arrays andpush!/append!sizehint!to pre-allocate result arrays with estimated sizes_append_vec!helper for efficient vector/matrix appendingBenchmark Results
Before (1D diffusion, N=30):
After (1D diffusion, N=30):
The improvements are modest because most of the allocation overhead comes from symbolic manipulation in ModelingToolkit/Symbolics, which is outside the scope of this package. The changes here focus on the discretization pipeline itself.
Test Plan
Files Changed
src/discretization/generate_finite_difference_rules.jl- Optimized rule generationsrc/discretization/schemes/centered_difference/centered_difference.jl- Optimized cartesian rulessrc/scalar_discretization.jl- Optimized equation discretizationtest/runtests.jl- Added allocation test grouptest/alloc_tests.jl- New allocation regression testscc @ChrisRackauckas
🤖 Generated with Claude Code