Dispatching by values is very slow - Feature or Problem?

BenchmarkTools is not very well suited to compare benchmarks as discussed in Identical functions benchmark differently but that’s another story. Actually benchmarks with those settings only makes differences wider, and using a dispatching function instead dispatching by value makes sortn! ~300% faster than default Base.sort!

using SortingNetworks
n = 4

#206.279 μs ±  10.895 μs
@benchmark for i in 1:10000 sort!(x)  end  setup=(x=rand(n)) evals=1

#64.406 μs ±   5.169 μs
@benchmark for i in 1:10000 sortn!(x) end  setup=(x=rand(n)) evals=1

and for sort vs swapsort

# 565.565 μs ± 142.188 μs
@benchmark for i in 1:10000 sort(x)  end  setup=(x=rand(n)) evals=1

#1.449 ms ± 205.297 μs
@benchmark for i in 1:10000 swapsort(x) end  setup=(x=rand(n)) evals=1