Skip to content

Eliminate some invalidations from loading FixedPointNumbers [tweaked #35733] #35803

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

Closed
wants to merge 3 commits into from

Conversation

KristofferC
Copy link
Member

#35733 but with the controversial ::Union{} methods removed. Still provides a significant speedup in the test case posted in that PR.

No impact on sysimage size.

@KristofferC
Copy link
Member Author

KristofferC commented May 8, 2020

Why is Statistics testing

julia> mean(())
ERROR: MethodError: reduce_empty(::typeof(Base.add_sum), ::Type{Union{}}) is ambiguous. Candidates:
  reduce_empty(::typeof(Base.add_sum), ::Type{T}) where T<:Union{Int16, Int32, Int8} in Base at reduce.jl:314
  reduce_empty(::typeof(Base.add_sum), ::Type{T}) where T<:Union{UInt16, UInt32, UInt8} in Base at reduce.jl:315
Possible fix, define
  reduce_empty(::typeof(Base.add_sum), ::Type{Union{}})

?

PR to statistics: https://github.com/JuliaLang/Statistics.jl/pull/36

Copy link
Member

@timholy timholy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that for this particular PR we will be best waiting until Jeff or Jameson weighs in...but FWIW, here are some thoughts.

@@ -3,6 +3,8 @@
# promote Bool to any other numeric type
promote_rule(::Type{Bool}, ::Type{T}) where {T<:Number} = T

convert(::Type{Bool}, x::Bool) = x # prevent invalidation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one might not be necessary either, after the Union{} stuff is fixed. It's just a really common case that would otherwise fall back to convert(::Type{T}, x::T).

@@ -49,6 +49,7 @@ Char
(::Type{AbstractChar})(x::Number) = Char(x)
(::Type{T})(x::AbstractChar) where {T<:Union{Number,AbstractChar}} = T(codepoint(x))
(::Type{T})(x::T) where {T<:AbstractChar} = x
AbstractChar(x::AbstractChar) = x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly ditto.

@@ -179,6 +180,7 @@ convert(::Type{AbstractChar}, x::Number) = Char(x) # default to Char
convert(::Type{T}, x::Number) where {T<:AbstractChar} = T(x)
convert(::Type{T}, x::AbstractChar) where {T<:Number} = T(x)
convert(::Type{T}, c::AbstractChar) where {T<:AbstractChar} = T(c)
convert(::Type{AbstractChar}, c::AbstractChar) = c
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...possibly same for everything up to the reduce_empty stuff.

@@ -306,25 +306,29 @@ with reduction `op` over an empty array with element type of `T`.

If not defined, this will throw an `ArgumentError`.
"""
reduce_empty(op, T) = _empty_reduce_error()
reduce_empty(::typeof(+), T) = zero(T)
reduce_empty(op, ::Type{T}) where T = _empty_reduce_error()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT this is not a signature-based invalidation, this is one that came from jl_method_table_disable which seems to have Base.delete_method as its only caller. I don't have a very good handle on what is going on here, sorry.

One interesting thing is that the inferred return type is Union{} (even before loading FixedPointNumbers):

julia> code_typed(Base.reduce_empty, (Base.BottomRF{typeof(max)}, Type{VersionNumber}); optimize=false)
1-element Array{Any,1}:
 CodeInfo(
1%1 = Base.getproperty(op, :rf)::Core.Compiler.Const(max, false)
│        Base.reduce_empty(%1, T)::Union{}
└──      Core.Compiler.Const(:(return %2), false)::Union{}
) => Union{}

@KristofferC
Copy link
Member Author

I thought this PR would be more trivial than it was. Now when it starts generating discussion I think it is best to just close it to keep the focus in #35733.

@KristofferC KristofferC closed this May 9, 2020
@DilumAluthge DilumAluthge deleted the kc/invalidations_reduced branch March 25, 2021 21:57
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