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

Ensure inference of Bool when array type is Any #45452

Merged
merged 1 commit into from
Jun 1, 2022

Conversation

Tokazama
Copy link
Contributor

While searching for invalidations with Static.jl I found that I was getting a handful on filter when the arrays eltype was Any. This fixed them.

Copy link
Sponsor Member

@vtjnash vtjnash left a comment

Choose a reason for hiding this comment

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

This should already be implied?

julia> methods(ifelse)
# 1 method for generic function "ifelse":
[1] ifelse(condition::Bool, x, y) in Base at essentials.jl:556

@Tokazama
Copy link
Contributor Author

Tokazama commented May 25, 2022

That's not inferred internally though and causes invalidations. Here's the relevant ones I found that went away once I changed this. The number of invalidations here varies depending on the version but these are from master.

Starts at
 inserting ifelse(::False, x, y) in Static at /Users/zchristensen/.julia/packages/Static/KC67x/src/bool.jl:125 invalidated:
   mt_backedges: 1: signature Tuple{typeof(ifelse), Any, AbstractFloat, AbstractFloat} triggered MethodInstance for Base._fast(::typeof(max), ::AbstractFloat, ::AbstractFloat) (0 children)
                 2: signature Tuple{typeof(ifelse), Any, AbstractFloat, AbstractFloat} triggered MethodInstance for Base._fast(::typeof(min), ::AbstractFloat, ::AbstractFloat) (0 children)
                 3: signature Tuple{typeof(ifelse), Any, Int64, Int64} triggered MethodInstance for filter!(::Function, ::Vector{Union{Nothing, Base.UUID}}) (0 children)
                 4: signature Tuple{typeof(ifelse), Any, DateTime, Any} triggered MethodInstance for max(::DateTime, ::Any) (0 children)
                 5: signature Tuple{typeof(ifelse), Any, Int64, Int64} triggered MethodInstance for filter!(::Function, ::Vector{Base.UUID}) (0 children)
                 6: signature Tuple{typeof(ifelse), Any, Int64, Int64} triggered MethodInstance for filter!(::Function, ::Vector{Symbol}) (0 children)
                 7: signature Tuple{typeof(ifelse), Any, Int64, Int64} triggered MethodInstance for filter!(::Base.var"#40#42", ::Vector{Any}) (8 children)
   18 mt_cache
 inserting promote_rule(::Type{Bool}, ::Type{<:StaticBool}) in Static at /Users/zchristensen/.julia/packages/Static/KC67x/src/bool.jl:113 invalidated:
   backedges: 1: superseding promote_rule(::Type{Bool}, ::Type{T}) where T<:Number in Base at bool.jl:4 with MethodInstance for promote_rule(::Type{Bool}, ::Type{<:Integer}) (2 children)
   23 mt_cache

It also has a slight improvement on performance when it's hard to figure out what the return value of of f(::eltype(A)) is.

julia> v = Any[rand(Int, 1000)...];

julia> @btime filter(isodd, $(v));  # old
  10.828 μs (1 allocation: 7.94 KiB)

julia> @btime filter(isodd, $(v));  # new
  10.583 μs (1 allocation: 7.94 KiB)

@KristofferC KristofferC merged commit ab4c8cf into JuliaLang:master Jun 1, 2022
@Tokazama Tokazama deleted the filter-inference branch June 1, 2022 10:44
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.

None yet

3 participants