aaowens / StaticOptim.jl

Other
23 stars 0 forks source link

Strange @btime behavior #18

Open aaowens opened 6 years ago

aaowens commented 6 years ago

Why does it allocate the first time? @btime is not supposed to need a warm up.

julia> @btime soptimize(rosenbrock, $sx)
  2.234 μs (2 allocations: 160 bytes)
Results of Static Optimization Algorithm
 * Minimizer: [0.9999999999990606,0.9999999999980389]
 * Minimum: [1.5610191722141176e-24]
 * Hf(x): [801.6874976886638,-399.8345645795701,-399.83456457957504,199.9124176978296]
 * Number of iterations: [31]
 * Number of function calls: [69]
 * Number of gradient calls: [31]
 * Converged: [true]

julia> @btime soptimize(rosenbrock, $sx)
  2.190 μs (0 allocations: 0 bytes)
Results of Static Optimization Algorithm
 * Minimizer: [0.9999999999990606,0.9999999999980389]
 * Minimum: [1.5610191722141176e-24]
 * Hf(x): [801.6874976886638,-399.8345645795701,-399.83456457957504,199.9124176978296]
 * Number of iterations: [31]
 * Number of function calls: [69]
 * Number of gradient calls: [31]
 * Converged: [true]
aaowens commented 6 years ago

Also this only started when I added some additional fields to StaticOptimizationResults

aaowens commented 6 years ago

In 1.0.1 it now allocates all the time. It's also faster than it was last I tried this.

Is there some limit to the amount of data you can put in a struct before it starts to allocate?

julia> @btime soptimize(rosenbrock, $sx)
  2.015 μs (2 allocations: 160 bytes)
Results of Static Optimization Algorithm
 * Initial guess: [3.2,3.2]
 * Minimizer: [0.9999999999990606,0.9999999999980389]
 * Minimum: [1.5610191722141176e-24]
 * Hf(x): [801.6874976886638,-399.8345645795701,-399.83456457957504,199.9124176978296]
 * Number of iterations: [31]
 * Number of function calls: [69]
 * Number of gradient calls: [31]
 * Converged: [true]

It does seem to be a real allocation

julia> function test(sx)
       for i = 1:10000
       soptimize(rosenbrock, sx)
       end
       end
test (generic function with 1 method)

julia> @btime test($sx)
  20.124 ms (20000 allocations: 1.53 MiB)
pkofod commented 5 years ago

I'm seing similar things with static arrays, but it's super erratic. Say, I can remove a branch that just prints a string, and then the allocations go away... I'm stumped tbh

aaowens commented 5 years ago

Are you interpolating the string? Reminds me of this comment: https://discourse.julialang.org/t/understanding-performance-using-btime-and-code-warntype-code-llvm-etc/16293/2?u=aaowens