Performance improvements: type stability and reduced allocations #486
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 improves performance by fixing type instabilities and reducing allocations in hot code paths:
extract_coefficients(util.jl): Fixed type instability wherefindfirstreturnsUnion{Nothing, Int}. Now uses explicit type-stable loop with::Intassertion. Pre-computes array lengths outside loops.monomial_compress(wronskian.jl): ReplacedArray{Any, 1}with typedVector{Tuple{P, T}}for type stability. Pre-computes parameter names in a Set for O(1) lookup instead of repeated map operations.massive_eval(wronskian.jl): Uses typed containers (Set{Vector{Int}},Dict{Vector{Int}, T}) instead of untyped. Pre-allocates working arrays and uses in-place operations with@inbounds. Pre-sizes result array.det_minor_expansion_inner(elimination.jl): Replacedin keys(cache)withhaskey(cache)for better performance. Pre-allocates Sets for discarded rows/cols. Usessort!on mutable arrays instead of allocating.Benchmarks
Tested on Lotka-Volterra and SIWR models - all tests pass with identical results. The optimizations particularly benefit larger ODE systems where these hot paths are called many times.
Test plan
extract_coefficientsunit test passesmonomial_compressreturns correct number of termscc @ChrisRackauckas
🤖 Generated with Claude Code