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

@inline with traits #79

Open
timholy opened this issue Oct 2, 2021 · 3 comments
Open

@inline with traits #79

timholy opened this issue Oct 2, 2021 · 3 comments

Comments

@timholy
Copy link
Collaborator

timholy commented Oct 2, 2021

In JuliaArrays/GetindexArrays.jl#3 (comment) I identified an issue in which SimpleTraits caused a nearly 5x performance hit. SimpleTraits was confirmed to be the source because changes like

@@ -414,9 +416,9 @@ StreamIndexStyle(S::StreamingContainer) = StreamIndexStyle(typeof(S))
 
 filter_space_axes(axes::NTuple{N,Axis}, items::NTuple{N,Any}) where {N} =
     _filter_space_axes(axes, items)
-@inline @traitfn _filter_space_axes(axes::Tuple{Ax,Vararg{Any}}, items) where {Ax<:Axis;  TimeAxis{Ax}} =
+@inline _filter_space_axes(axes::Tuple{<:Axis{:time},Vararg{Any}}, items) =
     _filter_space_axes(tail(axes), tail(items))
-@inline @traitfn _filter_space_axes(axes::Tuple{Ax,Vararg{Any}}, items) where {Ax<:Axis; !TimeAxis{Ax}} =
+@inline _filter_space_axes(axes::Tuple{<:Axis,Vararg{Any}}, items) =
     (items[1], _filter_space_axes(tail(axes), tail(items))...)
 _filter_space_axes(::Tuple{}, ::Tuple{}) = ()
 @inline _filter_space_axes(axes::Tuple{Ax,Vararg{Any}}, items) where {Ax<:Axis{:color}} =

to ImageAxes.jl (and several more similar changes) give the faster performance. I assume the problem is that the @inline annotation is not being applied, but I am unsure. I tried swapping the orders of @traitfn and @inline but it didn't seem to help.

@timholy timholy changed the title @inbounds with traits @inline with traits Oct 2, 2021
@mauro3
Copy link
Owner

mauro3 commented Oct 2, 2021

Thanks for the report! I assume you don't have an idea on how to fix this? From the macroexpand it looks like with either order the inline is applied:

julia> @macroexpand @inline @traitfn f(x::::TT) = 1
quote
    begin
        #= /home/mauro/julia/dot-julia-dev/SimpleTraits/src/SimpleTraits.jl:331 =#
        (f(x::var"##261") where var"##261") = begin
                #= /home/mauro/julia/dot-julia-dev/SimpleTraits/src/SimpleTraits.jl:331 =#
                $(Expr(:meta, :inline))
                #= /home/mauro/julia/dot-julia-dev/SimpleTraits/src/SimpleTraits.jl:331 =#
                f(SimpleTraits.trait(TT{var"##261"}), x)
            end
    end
    begin
        $(Expr(:meta, :doc))
        (f(::Type{TT{var"##261"}}, x::var"##261"; ) where var"##261") = begin
                $(Expr(:meta, :inline))
                #= REPL[5]:1 =#
                1
            end
    end
end

julia> @macroexpand @traitfn @inline f(x::::TT) = 1                                                                                                                                             
quote                                                                                                                                                                                           
    begin                                                                                                                                                                                       
        #= /home/mauro/julia/dot-julia-dev/SimpleTraits/src/SimpleTraits.jl:331 =#                                                                                                              
        (f(x::var"##259") where var"##259") = begin                                                                                                                                             
                #= /home/mauro/julia/dot-julia-dev/SimpleTraits/src/SimpleTraits.jl:331 =#                                                                                                      
                $(Expr(:meta, :inline))                                                                                                                                                         
                #= /home/mauro/julia/dot-julia-dev/SimpleTraits/src/SimpleTraits.jl:331 =#
                f(SimpleTraits.trait(TT{var"##259"}), x)
            end
    end
    begin
        $(Expr(:meta, :doc))
        (f(::Type{TT{var"##259"}}, x::var"##259"; ) where var"##259") = begin
                $(Expr(:meta, :inline))
                #= REPL[4]:1 =#
                1
            end
    end
end

For your example the $(Expr(:meta, :inline)) is also added. So, I'm not sure what's up.

@timholy
Copy link
Collaborator Author

timholy commented Oct 2, 2021

I haven't looked. Thanks for checking! Profiling the test in that GetindexArrays issue I linked does reveal at least something about it (red bars in ProfileView, which indicate runtime dispatch), but I didn't really check further than that.

@mauro3
Copy link
Owner

mauro3 commented Oct 2, 2021

Hm, runtime dispatch, that is even worse than not inlining!

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

No branches or pull requests

2 participants