JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.65k stars 5.48k forks source link

Suboptimal performance of captured small unions #31909

Open bramtayl opened 5 years ago

bramtayl commented 5 years ago
function test(ys)
    x = if rand(Bool)
        1
    else
        missing
    end
    map(y -> x + y, ys)
end
@code_warntype test((1, 2))
Keno commented 5 years ago

This corresponds to a TODO in the apply_type tfunc: https://github.com/JuliaLang/julia/blob/master/base/compiler/tfuncs.jl#L998. However, I think there is a larger question of whether putting the union on the outside of the closure is really the right thing to do. Perhaps there is a lowering that would allow small unions as the field type for closures.

bramtayl commented 5 years ago

I used this to get around it:

partial_map(f, fixed, variables::Tuple{}) = ()
partial_map(f, fixed, variables::Tuple) =
    f(fixed, variables[1]), partial_map(f, fixed, tail(variables))...
partial_map(f, fixed, ::Tuple{}, ::Tuple{}) = ()
partial_map(f, fixed, variables1::Tuple, variables2::Tuple) =
    f(fixed, variables1[1], variables2[1]),
    partial_map(f, fixed, tail(variables1), tail(variables2))...
bramtayl commented 3 years ago

I watched the JuliaCon talk where @StefanKarpinski mentioned prioritizing making Julia faster for tabular data and I think solving this would be an important step