Keyword arguments is causing allocations

Hi!

I am getting an allocation when using a keyword in the following MWE:

julia> function teste(a::AbstractVector{T}; b::Integer = 0, c::Integer = 0) where T
       return a[1] + b + c
       end
teste (generic function with 1 method)

julia> data = [1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

julia> teste(data); @time teste(data)
  0.000003 seconds
1

julia> teste(data, b = 1); @time teste(data, b = 1)
  0.000002 seconds (1 allocation: 16 bytes)
2

julia> teste(data, c = 1); @time teste(data, c = 1)
  0.000002 seconds (1 allocation: 16 bytes)
2

julia> teste(data, b = 1, c = 1); @time teste(data, b = 1, c = 1)
  0.000002 seconds (1 allocation: 32 bytes)
3

Is it expected? Why calling a function using a keyword argument do an allocation?

By the way, if a is an Interger instead of a Vector, then I see no allocation at all.

I guess it is calling a specialised functions and for the case of only parameter a there is no operation to be performed.

Maybe check the generated code with @code_native

I am seeing this very same problem in a more complex function. If keywords are passed, then I have 1 allocation. Otherwise, I have no allocation.

Use BenchmarkTools.jl for this kind of microbenchmark.
You may just be seeing the result of accessing the global object data.

Nice! BenchmarkTools.jl shows that no allocation is performed in any of the cases. Thanks :smiley: