Recompilation triggered when re-running function from module

Rbthn
Tue, 01 Oct 2024 07:14:33 +0000
New to Julia

Hello! I’m relatively new to Julia and have encountered some behavior I cannot explain.

In a MWE, I have a function that takes an ODEProblem and algorithm and just calls DiffEq.solve() with these arguments.

function myfun(prob, alg)
    sol = solve(prob, alg)
end

When I put this function in a script (that I include in my main script), @time reports compilation time on the first run, but not when I paste the relevant line @time myscript(prob, alg) into the REPL or include my main script again. So far, so good.

But when I include the same function in a module (with Project.toml and Manifest.toml) and do using MyModule in my main script, something weird happens.

including main.jl in new Julia session: @time reports >99% compilation time, as expected

re-including main.jl from the same session: @time reports >99% compilation time, 100% of which was recompilation

if instead of re-including main.jl, I just paste the relevant line @time myfun(prob, alg) in the REPL, @time reports no compilation time (as I would expect)

So, why is re-including the main script different from just running the relevant line from the REPL? And why does this happen only when calling a function from a local module, and not when calling a function from a script?
Any help is appreciated!

(main.jl for reference, as I cannot upload files)

using DifferentialEquations
using MyModule
include("script.jl")

function simple_deriv!(du, u, p, t)
    du[:] = -1.0 .* u
    return nothing
end

t_0 = 0.0
t_end = 1.0
u_0 = [1.0]
alg = ImplicitEuler()
args = (; adaptive=false, dt=1 // 20)
prob = ODEProblem(simple_deriv!, u_0, (t_0, t_end); args...)

print("running from module:")
@time myfun(prob, alg)

print("running from script:")
@time myscript(prob, alg)

6 posts - 2 participants