Does specifying the element type of a Channel impact performance?

julia> a = ones(2,2);

julia> ct = Channel{Matrix{Float64}}(1)
Channel{Array{Float64,2}}(sz_max:1,sz_curr:0)

julia> @btime (put!($ct, $a); take!($ct););
  283.612 ns (0 allocations: 0 bytes)

julia> c = Channel{Any}(1);

julia> @btime (put!($c, $a); take!($c););
  286.102 ns (0 allocations: 0 bytes)

The performance and seems identical in either case, with no extra allocation. Is there any benefit to specifying the element type (other than dispatch)?

Yes, I assume you want to use the element you take from the channel, if the compiler does not know the type of the element, you’ll have type unstable code with potentially enormous consequences for performance.

Just to elaborate on @baggepinnen’s answer, you can see the compiler isn’t able to infer the output of take!(c), while it can take!(ct):

julia> @code_warntype take!(c)
Variables
  #self#::Core.Const(take!)
  c::Channel{Any}

Body::Any
1 ─ %1 = Base.isbuffered(c)::Bool
└──      goto #3 if not %1
2 ─ %3 = Base.take_buffered(c)::Any
└──      return %3
3 ─ %5 = Base.take_unbuffered(c)::Any
└──      return %5

julia> @code_warntype take!(ct)
Variables
  #self#::Core.Const(take!)
  c::Channel{Matrix{Float64}}

Body::Matrix{Float64}
1 ─ %1 = Base.isbuffered(c)::Bool
└──      goto #3 if not %1
2 ─ %3 = Base.take_buffered(c)::Matrix{Float64}
└──      return %3
3 ─ %5 = Base.take_unbuffered(c)::Matrix{Float64}
└──      return %5

And the benchmark does slow down for c relative to ct if you do something with the result, e.g. adding a sum:

julia> @benchmark (put!($ct, $a); rt=take!($ct); sum(rt))
BenchmarkTools.Trial:
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     208.182 ns (0.00% GC)
  median time:      211.186 ns (0.00% GC)
  mean time:        213.535 ns (0.00% GC)
  maximum time:     381.306 ns (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     555

julia> @benchmark (put!($c, $a); r=take!($c); sum(r))
BenchmarkTools.Trial:
  memory estimate:  16 bytes
  allocs estimate:  1
  --------------
  minimum time:     232.654 ns (0.00% GC)
  median time:      237.932 ns (0.00% GC)
  mean time:        243.619 ns (0.14% GC)
  maximum time:     3.551 μs (93.20% GC)
  --------------
  samples:          10000
  evals/sample:     442

(though not by that much in this example, except when garbage collection runs).