I have a large ModelingToolkit model. mtkcompile inlines ~32k observed equations into a single big RHS function in around 60 seconds, and first-call compilation of that one function takes >1000 s (dominated by type inference of the single MethodInstance, per @snoop_inference). CSE is working and it’s not a type-instability issue, it’s purely the size of one function.
Because it’s a single function / single MethodInstance:
- it compiles on one thread (~100% CPU on a 16-core machine), so more cores don’t help;
- it can’t be cached in pieces, and even image-building parallelism (
JULIA_IMAGE_THREADS) can’t split one function; - compile cost looks roughly linear in the number of inlined equations (I haven’t measured the exponent), so the problem isn’t total work, it’s that one function can’t be cached or compiled in parallel.
Would it be feasible for build_function / MTK codegen to emit the RHS as N smaller sub-functions plus a driver (e.g. a chunks=N option)? Split into pieces, they could be precompiled/cached and compiled in parallel during image building (JULIA_IMAGE_THREADS), instead of paying a single serial >1000 s JIT every session.
I can share a reproducing model and @snoop_inference output.