Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange @allocated numbers for function calls to interpolation objects #2844

Open
chunjiw opened this issue Mar 8, 2024 · 1 comment
Open
Labels
macro Julia macros: @oops

Comments

@chunjiw
Copy link

chunjiw commented Mar 8, 2024

I'm trying to reduce allocations during function calls to Interpolation objects in Interpolations.jl package. But I get this strange result:
image

But then I put the same code in a file, they both return zero:

using Interpolations
let
	t = interpolate((1:10,), rand(10), Gridded(Linear()))
	println(@allocated t(5))
end
let
	t = interpolate((1:10,), rand(10), Gridded(Linear()))
	fieldnames(typeof(t))
	println(@allocated t(5))
end

Output is

0
0

I'm truly perplexed. Greatly appreciate it if someone can shed some light on this.

Pluto.jl is v0.19.40; and

julia> versioninfo()
Julia Version 1.10.2
Commit bd47eca2c8a (2024-03-01 10:14 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 12 × Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, skylake)
Threads: 1 default, 0 interactive, 1 GC (on 12 virtual cores)

@fonsp fonsp added the macro Julia macros: @oops label Mar 28, 2024
@fonsp
Copy link
Owner

fonsp commented Mar 28, 2024

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 JuliaLang/julia#38880 (comment)

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
macro Julia macros: @oops
Projects
None yet
Development

No branches or pull requests

2 participants