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

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

Closed
mabokhamis opened this issue May 19, 2020 · 1 comment
Closed

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

mabokhamis opened this issue May 19, 2020 · 1 comment

Comments

@mabokhamis
Copy link

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

@KristofferC
Copy link
Member

Dup of #29393.

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