Skip to content

Unused where T causes a function to become very slow #35935

@mabokhamis

Description

@mabokhamis

It seems that having an unused where T in a function declaration can cause the function to become much slower and to allocate a lot more memory. Consider for example the following two functions which are identical except that the second one has an unnecessary where T:

function f1(x::Int)
    return mod(2 * x + 1, 1000)
end
function f2(x::Int) where T # Note the unused `where T`
    return mod(2 * x + 1, 1000)
end

Comparing performance of the two functions reveals that the second is much slower:

function test(f::Function)
    x = 0
    for i = 1:100000000
        x = f(x)
    end
    x
end
test(f1)
@time test(f1) # 0.389844 seconds
test(f2)
@time test(f2) # 2.278735 seconds (96.00 M allocations: 1.431 GiB, 0.51% gc time)

Such unused where arguments can sometimes occur in large Julia codebases as leftovers from previous reversions, etc. In order to make sure they don't affect performance, perhaps we could consider either turning them into syntax errors or introduce an optimization pass that removes them automatically before compiling a function. See the following thread for more discussion https://discourse.julialang.org/t/unused-where-t-causes-a-function-to-become-very-slow/39727

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions