Open chunjiw opened 3 months ago
I think the first cell is being optimized more heavily than the second, and Julia is able to optimize away the actual t(5)
call because it is effect-free and the result is not used.
This reminds me of https://github.com/JuliaLang/julia/issues/38880#issuecomment-745058041
In the REPL, if you make sure that the call cannot be optimized away (by storing the result), you get 16 allocs in both cases:
julia> using Interpolations
julia> let
t = interpolate((1:10,), rand(10), Gridded(Linear()))
println(@allocated t(5))
end
0
julia> let
t = interpolate((1:10,), rand(10), Gridded(Linear()))
fieldnames(typeof(t))
println(@allocated t(5))
end
0
julia> result = Ref(0.0);
julia> let
t = interpolate((1:10,), rand(10), Gridded(Linear()))
fieldnames(typeof(t))
println(@allocated result[] = t(5))
end
16
julia> let
t = interpolate((1:10,), rand(10), Gridded(Linear()))
println(@allocated result[] = t(5))
end
16
The difference between the REPL and Pluto is in the way Pluto runs code: it will wrap your code in a try catch, a timer function, etc, and sometimes even rewrites your toplevel code into a function body and calls the function instead (#720). So the heuristics were not able to optimize as well in Pluto as in the REPL.
I'm trying to reduce allocations during function calls to Interpolation objects in Interpolations.jl package. But I get this strange result:
But then I put the same code in a file, they both return zero:
Output is
I'm truly perplexed. Greatly appreciate it if someone can shed some light on this.
Pluto.jl is v0.19.40; and