Skip to content

Force specialization on function arguments

Carsten Bauer requested to merge cb/specializefunc into master

By default, Julia doesn't always specialize on function arguments. This PR enforces it by using func(f::F) where F instead of just func(f). See https://docs.julialang.org/en/v1/manual/performance-tips/index.html#Be-aware-of-when-Julia-avoids-specializing-1 and https://discourse.julialang.org/t/10x-slowdown-when-passing-function-as-argument/40382/16.

Before PR:

julia> @benchmark submatrix_apply($(x->x.^2), $(sprandsymposdef(1000, 0.01)), $(Threaded(nthreads=5)))
BenchmarkTools.Trial: 2990 samples with 1 evaluation.
 Range (min … max):  637.670 μs … 67.554 ms  ┊ GC (min … max):  0.00% … 98.55%
 Time  (median):     942.294 μs              ┊ GC (median):     0.00%
 Time  (mean ± σ):     1.674 ms ±  6.720 ms  ┊ GC (mean ± σ):  46.51% ± 11.62%


  █▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▄▅▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃▁▁▁▁▅ █
  638 μs        Histogram: log(frequency) by time      60.4 ms <

 Memory estimate: 2.45 MiB, allocs estimate: 2069.

After PR:

julia> @benchmark submatrix_apply($(x->x.^2), $(sprandsymposdef(1000, 0.01)), $(Threaded(nthreads=5)))
BenchmarkTools.Trial: 3206 samples with 1 evaluation.
 Range (min … max):  594.943 μs … 73.403 ms  ┊ GC (min … max):  0.00% … 98.40%
 Time  (median):     869.237 μs              ┊ GC (median):     0.00%
 Time  (mean ± σ):     1.555 ms ±  6.564 ms  ┊ GC (mean ± σ):  46.28% ± 11.32%

  █▂                                                            
  ██▆▄▁▅▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃▅▁▁▁▃▃▃▃▁▄▃ █
  595 μs        Histogram: log(frequency) by time      25.9 ms <

 Memory estimate: 2.35 MiB, allocs estimate: 2072.

Merge request reports

Loading