Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple example of type unstable function in Julia 1.x with performance consequences

All examples that I found to illustrate performance bottlenecks of type instability in Julia < 0.6 are no longer valid in Julia >= 0.7, as they typically play with two or few possible types, but now the compiler is efficient also in the handling of Union{T1,T2,..} types, and so the differences with the corresponding type stable version disappear.

Can you indicate an example of a simple type-unstable function that still have large performance improvements when it is made type-stable ?

like image 873
Antonello Avatar asked Jun 13 '19 14:06

Antonello


1 Answers

Typically the most striking difference is when type instability is introduced by global variables. Here is an example of using barrier function to remove type instability:

x = 10

function f1()
    [i + x for i in 1:10^6]
end

function f2()
   h(x) = [i + x for i in 1:10^6]
   h(x)
end

And here is the benchmark:

julia> using BenchmarkTools

julia> @benchmark f1()
BenchmarkTools.Trial:
  memory estimate:  38.13 MiB
  allocs estimate:  1998992
  --------------
  minimum time:     30.133 ms (3.66% GC)
  median time:      33.713 ms (3.39% GC)
  mean time:        35.206 ms (5.35% GC)
  maximum time:     100.575 ms (50.58% GC)
  --------------
  samples:          142
  evals/sample:     1

julia> @benchmark f2()
BenchmarkTools.Trial:
  memory estimate:  7.63 MiB
  allocs estimate:  2
  --------------
  minimum time:     2.325 ms (0.00% GC)
  median time:      3.286 ms (0.00% GC)
  mean time:        3.725 ms (20.26% GC)
  maximum time:     52.838 ms (93.68% GC)
  --------------
  samples:          1342
  evals/sample:     1

Another common case of performance loss is when you use containers of abstract eltype (but I understand that you did not classify this as function type unstability, as it is rather data type instability).

like image 174
Bogumił Kamiński Avatar answered Nov 11 '22 20:11

Bogumił Kamiński