How can I eliminate allocations here?

It is something related to the fact that the function does not specialize to keyword arguments, and neither to the input function. For example:

julia> function ces(x, shares, shares1, r, normfun::F) where F
           n = length(x)
           #shares1 = copy(shares)
           sharessum = zero(eltype(shares))
           for i in 1:n
               shares1[i] = normfun(shares1[i]; max=1-sharessum)
               sharessum += shares1[i]
           end
           return _cessum(x, shares1, r)^(1/r)
       end
ces (generic function with 3 methods)

julia> @benchmark ces($x, $shares, $shares1, $r, $logit)
BenchmarkTools.Trial: 10000 samples with 196 evaluations.
 Range (min … max):  476.944 ns …  1.468 μs  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     477.663 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   488.912 ns ± 47.475 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  █ ▂    ▂  ▂▁                                      ▁          ▁
  █▃█▆▅▅▅█▁▄██▁▄▅▆▁▃▄▆▅▆▆█▄▄▅▄▇▆▄▅▃█▄▄▃▃▅▃▄▄▃▄▇▃▅▆▄▄█▄▁▄▄▅▅█▄▅ █
  477 ns        Histogram: log(frequency) by time       692 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

(I moved shares1 to a parameter to eliminate all allocations).

Probably related to this: Performance Tips · The Julia Language

5 Likes