-
Notifications
You must be signed in to change notification settings - Fork 150
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
Use Base.promote instead of promote_tuple_eltype to fik promotion #670
base: master
Are you sure you want to change the base?
Conversation
8d5940c
to
ec0edb8
Compare
Cool. Did you look into performance of Interesting corner cases to consider include |
This is confusing, right? The function generator doesn't currently call |
@generated function (::Type{SArray{S}})(x::T) where {S <: Tuple, T <: Tuple} | ||
return quote | ||
@_inline_meta | ||
SArray{S, $(promote_tuple_eltype(T)), $(tuple_length(S)), $(tuple_prod(S))}(x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andyferris ah hah; here's where we call promote_tuple_eltype
from within a generated function — looking through this PR I think we'd only need to change this one line (by removing the $
character :-) ) to fix the original problem. Is that right @andreasnoack?
Regardless of that it's always nice to remove some generated functions so I'm generally positive about doing these changes as a cleanup. Provided the original reason for needing promote_tuple_eltype
is gone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just tried it and it doesn't seem to be sufficient to fix the issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's weird, I was sure this would fix it. Locally removing the $
seems to work for me:
julia> struct MyFloat64 <: Real
x::Float64
end
julia> Base.promote_rule(::Type{Float64}, ::Type{MyFloat64}) = MyFloat64
julia> SArray{Tuple{2}}((2.0, MyFloat64(1.0)))
2-element SArray{Tuple{2},MyFloat64,1,2} with indices SOneTo(2):
MyFloat64(2.0)
MyFloat64(1.0)
(btw beware that you can't always rely on Revise
to work with this stuff... changes may require restarting Julia and reloading StaticArrays)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, @c42f.
I guess if we just change this one line, then it should unambiguously have no negative impact on generated code. If we merge the whole PR we should probably also stress test this with long Union
-type static arrays, etc, first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what went wrong but indeed it seems to be sufficient to fix the issue. I've opened #671. I still think we should consider geetting rid of the generated function here but it's not urgent.
issue for element types defined after StaticArrays.
I did some timings and didn't see any regressions but timing StaticArrays code is always a bit tricky. I guess the most important thing is that I didn't see any new allocations. I assumed that the old issue that made you |
ec0edb8
to
c72f42f
Compare
Doing some archeology, the origin of |
lol, nice archaeology. Constructors were done first - promotion behaving like other arrays obviously being pretty important. How time flies :) |
In any case we should double check some generated (native) code first but in general I am in favour of removing generated code. |
...issue for element types defined after StaticArrays.
Currently, we have this behavior
probably because the generated function
promote_tuple_eltype
can't handle types defined later than itself. With this PR, the example givesThe PR generally reduces the number of generated functions.
A remaining issue is
StaticArrays.jl/test/SHermitianCompact.jl
Line 228 in 4cfeb58
promote_rule
between the matrix valued elements. @tkoolen Any thoughts on the better way to fix this case?