Skip to content

Factor out assign_metadata! from @components#4222

Open
cstjean wants to merge 1 commit into
SciML:masterfrom
cstjean:assign-metadata
Open

Factor out assign_metadata! from @components#4222
cstjean wants to merge 1 commit into
SciML:masterfrom
cstjean:assign-metadata

Conversation

@cstjean

@cstjean cstjean commented Jan 27, 2026

Copy link
Copy Markdown
Contributor

@components is used a lot, and its macroexpansion contains code that will be compiled over and over again. This PR pulls that code into a helper function, such that it will be compiled only once. It should help (ever so slightly) with TTFX and compiled code size.

The macroexpansion is also cleaner:

julia> @macroexpand1(@component function decay(; name)
           @parameters a
           @variables x(t) f(t)
           System([
                   D(x) ~ -a * x + f
               ], t;
               name = name)
       end) |> striplines
quote
    (Base).@__doc__ function decay(; name)
            function var"##create_component#3103"()
                begin
                    @parameters a
                    @variables x(t) f(t)
                    System([D(x) ~ -a * x + f], t; name = name)
                end
            end
            return (ModelingToolkitBase.assign_metadata!)(var"##create_component#3103"(), false, :decay, @__MODULE__())
        end
end

vs

quote
    (Base).@__doc__ function decay(; name)
            res = ((()->begin
                        begin
                            @parameters a
                            @variables x(t) f(t)
                            System([D(x) ~ -a * x + f], t; name = name)
                        end
                    end))()
            if (isdefined)(res, :gui_metadata) && (getfield)(res, :gui_metadata) === nothing
                name = :decay
                if false
                    (Setfield).@set! res.connector_type = (ModelingToolkitBase.connector_type)(res)
                end
                (Setfield).@set! res.gui_metadata = (ModelingToolkitBase.GUIMetadata)((GlobalRef)(@__MODULE__(), name))
            else
                res
            end
        end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant