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

Method definition can take quadratic time #45424

Closed
aviatesk opened this issue May 23, 2022 · 7 comments
Closed

Method definition can take quadratic time #45424

aviatesk opened this issue May 23, 2022 · 7 comments

Comments

@aviatesk
Copy link
Member

aviatesk commented May 23, 2022

MRE:

julia> function time_definition(n)
           ex = Expr(:block)
           var = gensym()
           push!(ex.args, :($var = x))
           for _ = 1:n
               newvar = gensym()
               push!(ex.args, :($newvar = $var + 1))
               var = newvar
           end
           @time @eval global function quadratic(x)
               $ex
           end
       end;

julia> function time_execution()
           s = time_ns()
           @eval quadratic(42)
           e = time_ns()
           Base.time_print(e - s)
       end;

julia> time_definition(1000)
  0.044844 seconds (11.15 k allocations: 628.467 KiB)
quadratic (generic function with 1 method)

julia> time_execution()
  0.027912 seconds

julia> time_definition(10000)
  1.738030 seconds (118.12 k allocations: 6.034 MiB)
quadratic (generic function with 1 method)

julia> time_execution()
  0.400399 seconds

julia> time_definition(20000)
  6.889014 seconds (238.12 k allocations: 12.108 MiB, 0.23% gc time)
quadratic (generic function with 1 method)

julia> time_execution()
  1.125169 seconds

It's nice to have this improved as we are going to fix the quadratic behavior of inference soonish (#45276 )

@oscardssmith
Copy link
Member

does this have significant TTFP implications?

@aviatesk
Copy link
Member Author

aviatesk commented May 23, 2022

Probably not, this extreme pattern won't be seen that often.

@jmichel7
Copy link

see #45395

@KristofferC
Copy link
Member

Are you saying that issue is related to this in some way?

@jmichel7
Copy link

jmichel7 commented May 23, 2022

I do not know, but it is (another?) quadratic or exponential thing happening while compiling a method

@aviatesk
Copy link
Member Author

Now we merged #45276 and it seems to be the lowering time is a dominant factor of the whole compilation for this kind of case:

julia> function time_definition(n)
           ex = Expr(:block)
           var = gensym()
           push!(ex.args, :($var = x))
           for _ = 1:n
               newvar = gensym()
               push!(ex.args, :($newvar = $var + 1))
               var = newvar
           end
           @time @eval global function quadratic(x)
               $ex
           end
       end
time_definition (generic function with 1 method)

julia> function time_execution()
           s = time_ns()
           @eval quadratic(42)
           e = time_ns()
           Base.time_print(e - s)
       end
time_execution (generic function with 1 method)

julia> time_definition(1000)
  0.044844 seconds (11.15 k allocations: 628.467 KiB)
quadratic (generic function with 1 method)

julia> time_execution()
  0.027912 seconds
julia> time_definition(10000)
  1.738030 seconds (118.12 k allocations: 6.034 MiB)
quadratic (generic function with 1 method)

julia> time_execution()
  0.400399 seconds
julia> time_definition(20000)
  6.889014 seconds (238.12 k allocations: 12.108 MiB, 0.23% gc time)
quadratic (generic function with 1 method)

julia> time_execution()
  1.125169 seconds

@aviatesk
Copy link
Member Author

aviatesk commented Aug 2, 2023

This seems to have been fixed on v1.10.

@aviatesk aviatesk closed this as completed Aug 2, 2023
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

4 participants