Large memory allocation in ForwardDiff

Note also that ForwardDiff seems to do some recompilation when you call a function with the same signature with an array of different length:

julia> using ForwardDiff

julia> @time ForwardDiff.gradient(sum, [1., 2., 3., 4.]);
  0.106838 seconds (301.52 k allocations: 15.820 MiB)

julia> @time ForwardDiff.gradient(sum, [1., 2., 3., 4.]);
  0.000014 seconds (4 allocations: 608 bytes)

julia> @time ForwardDiff.gradient(sum, [1., 2., 3., 4., 5.]);
  0.119015 seconds (308.93 k allocations: 16.164 MiB)

julia> @time ForwardDiff.gradient(sum, [1., 2., 3., 4., 5.]);
  0.000020 seconds (4 allocations: 816 bytes)

Related: Another post on package compilation time - #15 by s-broda

1 Like