JuliaAstro / Transits.jl

Flexible photometric transit curves with limb darkening
https://juliaastro.github.io/Transits.jl/dev/
MIT License
20 stars 6 forks source link

`KeywordCalls.jl` performance regression? #34

Closed icweaver closed 2 years ago

icweaver commented 2 years ago

Ok, now that #23 is in, I guess it's time to start looking at those performance regressions. I have basically zero experience with profiling, so sorry if the rest of this is a bit incoherent!

To start tackling this, I just did a malloc analysis for this simple function on julia v1.6.5 and v1.7.1:

make_orbit() = b_rho_star = KeplerianOrbit(
   rho_star = 2.0,
   R_star = 0.5,
   period = 2.0,
   ecc = 0.0,
   t0 = 0.0,
   incl = π / 2.0,
   Omega = 0.0,
   omega = 0.0,
)

On v1.6.5, this was nice and allocation free, and on v1.7.1... not so much, so this is my attempt to start the hunt for what the cause might be. Going line by line, it looks like the following bits of code from KeywordCalls.jl are allocating in v1.7.1 now (shown by the arrows):

1) this line

function alias(f, nt::NamedTuple{K}) where {K} 
    newnames = alias(f, Val.(K))
    NamedTuple{newnames}(values(nt)) <---
end

2) and that line

kwmethod = quote
    $f_esc(;kw...) = $f_esc(NamedTuple(kw)) <---
    KeywordCalls.has_kwargs(::$inst) = true
end

Below is the Coverage.jl report for each julia version and script that I used to generate the .mem files. Is this already looking like a wild goose chase, or do you think these are the places that we should start looking into?

v1.6.5 mallocs ```shell 26-element Vector{CoverageTools.MallocInfo}: CoverageTools.MallocInfo(0, "./packages/Bijectors/BrHk4/src/Bijectors.jl.8096.mem", 270) CoverageTools.MallocInfo(0, "./packages/Bijectors/BrHk4/src/Bijectors.jl.8096.mem", 281) CoverageTools.MallocInfo(0, "./packages/Bijectors/BrHk4/src/Bijectors.jl.8096.mem", 282) CoverageTools.MallocInfo(0, "./packages/Bijectors/BrHk4/src/Bijectors.jl.8096.mem", 283) CoverageTools.MallocInfo(0, "./packages/Bijectors/BrHk4/src/Bijectors.jl.8096.mem", 284) CoverageTools.MallocInfo(0, "./packages/Bijectors/BrHk4/src/Bijectors.jl.8096.mem", 285) CoverageTools.MallocInfo(0, "./packages/DocStringExtensions/iscC8/src/DocStringExtensions.jl.8096.mem", 112) CoverageTools.MallocInfo(0, "./packages/Requires/Z8rfN/src/require.jl.8096.mem", 11) CoverageTools.MallocInfo(0, "./packages/Requires/Z8rfN/src/require.jl.8096.mem", 99) CoverageTools.MallocInfo(0, "./packages/Requires/Z8rfN/src/require.jl.8096.mem", 100) CoverageTools.MallocInfo(0, "./packages/Requires/Z8rfN/src/require.jl.8096.mem", 101) CoverageTools.MallocInfo(0, "./packages/Requires/Z8rfN/src/require.jl.8096.mem", 106) CoverageTools.MallocInfo(0, "./packages/Rmath/jzKQk/src/Rmath.jl.8096.mem", 37) CoverageTools.MallocInfo(0, "./packages/Rmath/jzKQk/src/Rmath.jl.8096.mem", 39) CoverageTools.MallocInfo(0, "./packages/Rmath/jzKQk/src/Rmath.jl.8096.mem", 41) CoverageTools.MallocInfo(0, "./packages/Setfield/9IaGB/src/Setfield.jl.8096.mem", 31) CoverageTools.MallocInfo(0, "./packages/Unitful/woO6b/src/Unitful.jl.8096.mem", 46) CoverageTools.MallocInfo(0, "./packages/Unitful/woO6b/src/Unitful.jl.8096.mem", 47) CoverageTools.MallocInfo(0, "./packages/Unitful/woO6b/src/Unitful.jl.8096.mem", 49) CoverageTools.MallocInfo(0, "./packages/Unitful/woO6b/src/user.jl.8096.mem", 22) CoverageTools.MallocInfo(0, "./packages/Unitful/woO6b/src/user.jl.8096.mem", 23) CoverageTools.MallocInfo(0, "./packages/Unitful/woO6b/src/user.jl.8096.mem", 24) CoverageTools.MallocInfo(0, "./packages/UnitfulAngles/n8zs1/src/UnitfulAngles.jl.8096.mem", 73) CoverageTools.MallocInfo(0, "./packages/UnitfulAngles/n8zs1/src/UnitfulAngles.jl.8096.mem", 74) CoverageTools.MallocInfo(0, "./packages/UnitfulAstro/1NGvU/src/UnitfulAstro.jl.8096.mem", 152) CoverageTools.MallocInfo(0, "./packages/UnitfulAstro/1NGvU/src/UnitfulAstro.jl.8096.mem", 153) ```
v1.7.1 mallocs ```shell 35-element Vector{CoverageTools.MallocInfo}: CoverageTools.MallocInfo(0, "./packages/Bijectors/BrHk4/src/Bijectors.jl.8291.mem", 270) CoverageTools.MallocInfo(0, "./packages/Bijectors/BrHk4/src/Bijectors.jl.8291.mem", 281) CoverageTools.MallocInfo(0, "./packages/Bijectors/BrHk4/src/Bijectors.jl.8291.mem", 282) CoverageTools.MallocInfo(0, "./packages/Bijectors/BrHk4/src/Bijectors.jl.8291.mem", 283) CoverageTools.MallocInfo(0, "./packages/Bijectors/BrHk4/src/Bijectors.jl.8291.mem", 284) CoverageTools.MallocInfo(0, "./packages/Bijectors/BrHk4/src/Bijectors.jl.8291.mem", 285) CoverageTools.MallocInfo(0, "./packages/DocStringExtensions/iscC8/src/DocStringExtensions.jl.8291.mem", 112) CoverageTools.MallocInfo(0, "./packages/KeywordCalls/QTeqo/src/KeywordCalls.jl.8291.mem", 8) CoverageTools.MallocInfo(0, "./packages/KeywordCalls/QTeqo/src/KeywordCalls.jl.8291.mem", 12) CoverageTools.MallocInfo(0, "./packages/KeywordCalls/QTeqo/src/KeywordCalls.jl.8291.mem", 15) CoverageTools.MallocInfo(0, "./packages/KeywordCalls/QTeqo/src/KeywordCalls.jl.8291.mem", 60) CoverageTools.MallocInfo(0, "./packages/Requires/Z8rfN/src/require.jl.8291.mem", 11) CoverageTools.MallocInfo(0, "./packages/Requires/Z8rfN/src/require.jl.8291.mem", 99) CoverageTools.MallocInfo(0, "./packages/Requires/Z8rfN/src/require.jl.8291.mem", 100) CoverageTools.MallocInfo(0, "./packages/Requires/Z8rfN/src/require.jl.8291.mem", 101) CoverageTools.MallocInfo(0, "./packages/Requires/Z8rfN/src/require.jl.8291.mem", 106) CoverageTools.MallocInfo(0, "./packages/Rmath/jzKQk/src/Rmath.jl.8291.mem", 37) CoverageTools.MallocInfo(0, "./packages/Rmath/jzKQk/src/Rmath.jl.8291.mem", 39) CoverageTools.MallocInfo(0, "./packages/Rmath/jzKQk/src/Rmath.jl.8291.mem", 41) CoverageTools.MallocInfo(0, "./packages/Setfield/9IaGB/src/Setfield.jl.8291.mem", 31) CoverageTools.MallocInfo(0, "./packages/Unitful/woO6b/src/Unitful.jl.8291.mem", 46) CoverageTools.MallocInfo(0, "./packages/Unitful/woO6b/src/Unitful.jl.8291.mem", 47) CoverageTools.MallocInfo(0, "./packages/Unitful/woO6b/src/Unitful.jl.8291.mem", 49) CoverageTools.MallocInfo(0, "./packages/Unitful/woO6b/src/dimensions.jl.8291.mem", 76) CoverageTools.MallocInfo(0, "./packages/Unitful/woO6b/src/types.jl.8291.mem", 20) CoverageTools.MallocInfo(0, "./packages/Unitful/woO6b/src/types.jl.8291.mem", 50) CoverageTools.MallocInfo(0, "./packages/Unitful/woO6b/src/user.jl.8291.mem", 22) CoverageTools.MallocInfo(0, "./packages/Unitful/woO6b/src/user.jl.8291.mem", 23) CoverageTools.MallocInfo(0, "./packages/Unitful/woO6b/src/user.jl.8291.mem", 24) CoverageTools.MallocInfo(0, "./packages/UnitfulAngles/n8zs1/src/UnitfulAngles.jl.8291.mem", 73) CoverageTools.MallocInfo(0, "./packages/UnitfulAngles/n8zs1/src/UnitfulAngles.jl.8291.mem", 74) CoverageTools.MallocInfo(0, "./packages/UnitfulAstro/1NGvU/src/UnitfulAstro.jl.8291.mem", 152) CoverageTools.MallocInfo(0, "./packages/UnitfulAstro/1NGvU/src/UnitfulAstro.jl.8291.mem", 153) CoverageTools.MallocInfo(304, "./packages/KeywordCalls/QTeqo/src/KeywordCalls.jl.8291.mem", 16) CoverageTools.MallocInfo(480, "./packages/KeywordCalls/QTeqo/src/KeywordCalls.jl.8291.mem", 77) ```
malloc script ```julia using Transits, Profile make_orbit() = b_rho_star = KeplerianOrbit( rho_star = 2.0, R_star = 0.5, period = 2.0, ecc = 0.0, t0 = 0.0, incl = π / 2.0, Omega = 0.0, omega = 0.0, ) make_orbit() # Run once to compile print(@allocated make_orbit()) Profile.clear_malloc_data() # Restart the counter make_orbit() ```
mileslucas commented 2 years ago

This looks like great detective work! CC @cscherrer

cscherrer commented 2 years ago

Thanks @mileslucas for the ping. And yeah, this looks like the right direction. Great work @icweaver :)

I'll start a KeywordCalls issue and link to this.

mileslucas commented 2 years ago

On Julia 1.8 with the new KeywordCalls release (0.2.5) the allocation is gone!