# Can Symbolics/MTK split the generated RHS into multiple smaller functions?

**URL:** https://discourse.julialang.org/t/can-symbolics-mtk-split-the-generated-rhs-into-multiple-smaller-functions/138469
**Category:** Modelling & Simulations
**Tags:** inference, compilation, modelingtoolkit, symbolics
**Created:** [July 25, 2026, 3:22pm UTC](https://discourse.julialang.org/t/can-symbolics-mtk-split-the-generated-rhs-into-multiple-smaller-functions/138469 "2026-07-25T15:22:02Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Bart\_van\_de\_Lint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bart_van_de_lint/32/212161_2.png) [@Bart\_van\_de\_Lint](https://discourse.julialang.org/u/Bart_van_de_Lint)
#### Post date: [July 25, 2026, 3:22pm UTC](https://discourse.julialang.org/t/can-symbolics-mtk-split-the-generated-rhs-into-multiple-smaller-functions/138469/1 "2026-07-25T15:22:02Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![drandran12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drandran12/32/220647_2.png) [@drandran12](https://discourse.julialang.org/u/drandran12)
#### Post date: [July 26, 2026, 7:39pm UTC](https://discourse.julialang.org/t/can-symbolics-mtk-split-the-generated-rhs-into-multiple-smaller-functions/138469/2 "2026-07-26T19:39:39Z")

</div>

I have had a recent discussion with the MTK maintainer about this in [ShardedForm codegen ~6.7× faster · Issue #294 · qojulia/QuantumCumulants.jl · GitHub](https://github.com/qojulia/QuantumCumulants.jl/issues/294).

I let Claude play with the ideas and had some great results:

> <https://github.com/qojulia/QuantumCumulants.jl/pull/316>
