refactor: improve precompile-friendliness #4044
Draft
+38
−34
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.
This enables greatly improved precompilation of many common MTK workflows. On 1.11:
Systemconstructor:This is for a particular signature. Different combinations of kwargs and different input types will cause compilation and we can't foreseeably compile every signature. This signature is
System(::Vector{Equation}, ::Num, ::Vector{Num}, ::Vector{Num}; initial_conditions::Dict{SymbolicT, SymbolicT}, guesses::Dict{SymbolicT, SymbolicT}, name::Symbol). With some discussion, we can arrive at a common signature to precompile. It will likely involve unwrapping theNums.complete(sys):This is for a system that does not have callbacks. The callback handling code is pretty big and needs a lot of work to precompile, so it is left for a future PR.
MTKTearing.TearingState(sys)The compilation here is all related to printing the equations to lexicographically sort them. The printing code in SU needs an overhaul to support precompilation (specifically,
src/ordering.jl)mtkcompile(sys):This is the version of
mtkcompilewith ModelingToolkit.jl loaded. The printing fromTearingState(sys)is already compiled here, so that is not part of this timing. The signatures reported by--trace-compilefor this workload areAnd despite significant digging with Cthulhu I haven't been able to find where they are hit.
Script to reproduce these results:
Which should be run as
The
print(Val(..))are to have unique markers in theprecompilestatements inbad.jlso that it is possible to identify which sections of the script triggers the compilations. They typically show up asThis requires JuliaSymbolics/SymbolicUtils.jl#824, JuliaSymbolics/Symbolics.jl#1711 and JuliaComputing/StateSelection.jl#11