Skip to content

Commit 7e0e931

Browse files
committed
Do full despecialization on components and connectors
These dont do any real work but can take a signficant time to compile, as an example, for a large model, as a baseline, I am seeing: ``` create model: 62.041103 seconds (193.16 M allocations: 9.779 GiB, 6.39% gc time, 99.79% compilation time: <1% of which was recompilation) ``` but with this PR active: ``` create model: 24.796934 seconds (67.98 M allocations: 3.496 GiB, 8.33% gc time, 98.84% compilation time: <1% of which was recompilation) ``` Note, this requires the not yet merged JuliaLang/julia#61746 to have any effect
1 parent 6b5d6c6 commit 7e0e931

1 file changed

Lines changed: 35 additions & 17 deletions

File tree

lib/ModelingToolkitBase/src/systems/abstractsystem.jl

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2836,6 +2836,8 @@ macro namespace(expr)
28362836
return esc(_config(expr, true))
28372837
end
28382838

2839+
const _HAS_PER_METHOD_COMPILER_OPTIONS = false # isdefined(Base.Experimental, :set_compile!)
2840+
28392841
function component_post_processing(__source__, expr, isconnector)
28402842
@assert expr isa Expr && (
28412843
expr.head == :function || (
@@ -2851,10 +2853,20 @@ function component_post_processing(__source__, expr, isconnector)
28512853
fname = sig.args[1]
28522854
args = sig.args[2:end]
28532855

2854-
out = quote
2855-
$Base.@__doc__ function $fname($(args...))
2856+
# On Julia with per-method `@compiler_options` support, wrap both the
2857+
# outer constructor and the inner closure (where the symbolic build
2858+
# actually happens) so only these methods get cheap-compile, leaving
2859+
# the rest of the user module at full optimization.
2860+
closure_call = if _HAS_PER_METHOD_COMPILER_OPTIONS
2861+
:($Base.Experimental.@compiler_options(compile = min, optimize = 0, infer = false, () -> $body)())
2862+
else
2863+
:((() -> $body)())
2864+
end
2865+
2866+
fexpr_block = quote
2867+
function $fname($(args...))
28562868
# we need to create a closure to escape explicit return in `body`.
2857-
res = (() -> $body)()
2869+
res = $closure_call
28582870
if $isdefined(res, :gui_metadata) && $getfield(res, :gui_metadata) === nothing
28592871
name = $(Meta.quot(fname))
28602872
if $isconnector
@@ -2872,24 +2884,30 @@ function component_post_processing(__source__, expr, isconnector)
28722884
end
28732885
end
28742886
end
2887+
func_expr = fexpr_block.args[end]
2888+
if _HAS_PER_METHOD_COMPILER_OPTIONS
2889+
func_expr = :($Base.Experimental.@compiler_options compile = min optimize = 0 infer = false $func_expr)
2890+
end
2891+
2892+
out = quote
2893+
$Base.@__doc__ $func_expr
2894+
end
28752895
return set_component_line_number!(__source__, out)
28762896
end
28772897

28782898
function set_component_line_number!(__source__::LineNumberNode, expr::Expr)
2879-
# To set the LineNumberNode correctly (and thus fix Go-to-definition), we modify the
2880-
# LineNumberNode in the `function` body. `dump(expr)` looks like this:
2881-
# head: Symbol block
2882-
# 2: Expr
2883-
# head: Symbol macrocall
2884-
# 3: Expr
2885-
# head: Symbol function
2886-
# args: Array{Any}((2,))
2887-
# 2: Expr
2888-
# head: Symbol block
2889-
# args: Array{Any}((5,))
2890-
# 1: LineNumberNode
2891-
@assert expr.args[2].args[3].args[2].args[1] isa LineNumberNode
2892-
expr.args[2].args[3].args[2].args[1] = __source__
2899+
# `expr` is the :block returned by the outer `quote`. `expr.args[2]` is
2900+
# the outermost macrocall (`@__doc__`, possibly wrapping
2901+
# `@compiler_options`, which wraps the function definition). Walk
2902+
# through the macrocall chain to find the function, then update the
2903+
# LineNumberNode at the top of its body so Go-to-definition works.
2904+
fexpr = expr.args[2]
2905+
while fexpr isa Expr && fexpr.head === :macrocall
2906+
fexpr = fexpr.args[end]
2907+
end
2908+
@assert fexpr isa Expr && fexpr.head === :function
2909+
@assert fexpr.args[2].args[1] isa LineNumberNode
2910+
fexpr.args[2].args[1] = __source__
28932911
return expr
28942912
end
28952913

0 commit comments

Comments
 (0)