Skip to content

Slow performance on Tuple{Type1, Type2} #2

@timholy

Description

@timholy

Here's a fun one that's an issue both here and for JLD. Since this is under "deeper" development, I thought I'd report it here.

The issue is that tuples can lead to a big penalty due to code specialization. This was discovered in the course of JuliaGraphics/Colors.jl@690f56e, where initially I used tuples like (Vector{RGB},Vector{HSV}) to store the expected results of conversion from many pairs of color spaces.

Demo:

using JLD2

vals = [Val{i}() for i = 1:20]
fn = tempname()
file = jldopen(fn, "w")
write(file, "val0", Val{0}())
@time write(file, "vals", vals)

valsa = [Any[vals[i],vals[j]] for i = 1:length(vals), j= 1:length(vals)]
@time write(file, "valsa", valsa)

valst = [(vals[i],vals[j]) for i = 1:length(vals), j = 1:length(vals)]
@time write(file, "valst", valst)

close(file)

Output:

julia> include("jldtest.jl")
  3.534846 seconds (3.09 M allocations: 132.546 MB, 1.48% gc time)
  0.311439 seconds (297.17 k allocations: 12.700 MB, 2.75% gc time)
 64.548822 seconds (45.51 M allocations: 1.928 GB, 1.57% gc time)

and obviously the result is quadratic in length(vals). The second run is fast, demonstrating that it's a codegen issue:

julia> include("jldtest.jl")
  0.000606 seconds (745 allocations: 30.797 KB)
  0.000872 seconds (1.27 k allocations: 70.797 KB)
  0.011489 seconds (11.75 k allocations: 511.844 KB)

This seems to argue for some kind of "late binding of types" design wherever possible?

The serializer has the same issue, but the coefficient is smaller:

julia> serialize(io, Val{0}())

julia> @time serialize(io, vals)
  0.818252 seconds (775.42 k allocations: 31.418 MB, 3.82% gc time)

julia> @time serialize(io, valsa)
  0.124597 seconds (147.32 k allocations: 6.075 MB)

julia> @time serialize(io, valst)
  4.480248 seconds (2.99 M allocations: 118.578 MB, 1.69% gc time)

julia> @time serialize(io, valsa)
  0.001521 seconds (2.42 k allocations: 389.188 KB)

julia> @time serialize(io, valst)
  0.011649 seconds (2.41 k allocations: 301.281 KB)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions