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

Fix type instability in chebinterp #19

Merged
merged 3 commits into from
Jan 24, 2024
Merged

Conversation

lxvm
Copy link
Contributor

@lxvm lxvm commented Jan 11, 2024

I noticed a type instability in droptol due to a variable in a closure modified in a loop that could be fixed with a type annotation.

Copy link

codecov bot commented Jan 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (dc4902b) 96.20% compared to head (d5f000c) 96.20%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master      #19   +/-   ##
=======================================
  Coverage   96.20%   96.20%           
=======================================
  Files           5        5           
  Lines         237      237           
=======================================
  Hits          228      228           
  Misses          9        9           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@stevengj
Copy link
Member

Thanks!

I'm a bit unclear why ::Int is required, here. What is the inferred type if you don't have this declaration?

@lxvm
Copy link
Contributor Author

lxvm commented Jan 12, 2024

Without the annotation the inferred type becomes Any, from checking with @code_warntype

MWE

The last three lines present the type-instability

julia> using FastChebInterp

julia> @code_warntype FastChebInterp.droptol(rand(5,5), 0.0)
MethodInstance for FastChebInterp.droptol(::Matrix{Float64}, ::Float64)
  from droptol(coefs::Array{<:Any, N}, tol::Real) where N @ FastChebInterp ~/.julia/dev/FastChebInterp/src/interp.jl:74
Static Parameters
  N = 2
Arguments
  #self#::Core.Const(FastChebInterp.droptol)
  coefs::Matrix{Float64}
  tol::Float64
Locals
  #67::FastChebInterp.var"#67#72"
  #64::FastChebInterp.var"#64#69"{2, Matrix{Float64}, Float64}
  #63::FastChebInterp.var"#63#68"{Float64}
  newsize::Tuple{Any, Any}
  abstol::Float64
Body::Any
1 ─       Core.NewvarNode(:(#67))
│         Core.NewvarNode(:(#64))
│         Core.NewvarNode(:(newsize))
│   %4  = FastChebInterp.maximum(FastChebInterp.infnorm, coefs)::Float64
│         (abstol = %4 * tol)
│   %6  = FastChebInterp.:(var"#63#68")::Core.Const(FastChebInterp.var"#63#68")
│   %7  = Core.typeof(abstol)::Core.Const(Float64)
│   %8  = Core.apply_type(%6, %7)::Core.Const(FastChebInterp.var"#63#68"{Float64})
│         (#63 = %new(%8, abstol))
│   %10 = #63::FastChebInterp.var"#63#68"{Float64}
│   %11 = FastChebInterp.all(%10, coefs)::Bool
└──       goto #4 if not %11
2 ─       return coefs
3 ─       Core.Const(:(goto %15))
4 ┄ %15 = FastChebInterp.:(var"#64#69")::Core.Const(FastChebInterp.var"#64#69")
│   %16 = $(Expr(:static_parameter, 1))::Core.Const(2)
│   %17 = Core.typeof(coefs)::Core.Const(Matrix{Float64})
│   %18 = Core.typeof(abstol)::Core.Const(Float64)
│   %19 = Core.apply_type(%15, %16, %17, %18)::Core.Const(FastChebInterp.var"#64#69"{2, Matrix{Float64}, Float64})
│         (#64 = %new(%19, coefs, abstol))
│   %21 = #64::FastChebInterp.var"#64#69"{2, Matrix{Float64}, Float64}
│   %22 = Core.apply_type(FastChebInterp.Val, $(Expr(:static_parameter, 1)))::Core.Const(Val{2})
│   %23 = (%22)()::Core.Const(Val{2}())
│         (newsize = FastChebInterp.ntuple(%21, %23))
│         (#67 = %new(FastChebInterp.:(var"#67#72")))
│   %26 = #67::Core.Const(FastChebInterp.var"#67#72"())
│   %27 = FastChebInterp.map(%26, newsize)::Tuple{Any, Any}
│   %28 = FastChebInterp.CartesianIndices(%27)::CartesianIndices{2, <:Tuple{OrdinalRange{Int64, Int64}, OrdinalRange{Int64, Int64}}}
│   %29 = Base.getindex(coefs, %28)::Any
└──       return %29

Analyzing the closure with Cthulhu shows that n in the closure gets boxed, even though it is not captured from the surrounding scope. I'm confused why this is too.

@stevengj
Copy link
Member

This https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-captured

Maybe better to use a let block as noted there?

Co-authored-by: Steven G. Johnson <[email protected]>
@stevengj stevengj merged commit 35eb4f5 into JuliaMath:master Jan 24, 2024
@lxvm lxvm deleted the stability branch January 24, 2024 22:41
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.

2 participants