Open kaandocal opened 1 month ago
@ysfoo just pointed out to me that chol_func3
with map
doesn't actually run without Ref
.
I believe this is a duplicate of #43333
Yeah, this is all about constant propagation through higher order functions. A simple workaround is to place the constant inside a function — "closer" to the getproperty
call. These flavors are type stable:
julia> function chol_func2a(mats::Vector{<:Matrix})
choleskies = cholesky.(mats)
return (x->getproperty(x, :L)).(choleskies)
end
chol_func2a (generic function with 2 methods)
julia> function chol_func3a(mats::Vector{<:Matrix})
choleskies = cholesky.(mats)
return map(x->getproperty(x,:L), choleskies)
end
chol_func3a (generic function with 3 methods)
The following code (thanks @ysfoo) shows that broadcasting can fail to be type stable, even when equivalent map calls/list comprehensions are:
Output:
Version info:
Changing
:L
toRef(:L)
doesn't make a difference for the first two examples. For the third (with map), usingRef(:L)
loses type stability: