# Keyword arguments is causing allocations

**URL:** <https://discourse.julialang.org/t/keyword-arguments-is-causing-allocations/41798>\
**Category:** General Usage\
**Created:** [June 20, 2020, 11:00pm UTC](https://discourse.julialang.org/t/keyword-arguments-is-causing-allocations/41798 "2020-06-20T23:00:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 20, 2020, 11:00pm UTC](https://discourse.julialang.org/t/keyword-arguments-is-causing-allocations/41798/1 "2020-06-20T23:00:23Z")

</div>

Hi!

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [June 20, 2020, 11:10pm UTC](https://discourse.julialang.org/t/keyword-arguments-is-causing-allocations/41798/2 "2020-06-20T23:10:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 20, 2020, 11:13pm UTC](https://discourse.julialang.org/t/keyword-arguments-is-causing-allocations/41798/3 "2020-06-20T23:13:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 20, 2020, 11:20pm UTC](https://discourse.julialang.org/t/keyword-arguments-is-causing-allocations/41798/4 "2020-06-20T23:20:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 21, 2020, 12:03am UTC](https://discourse.julialang.org/t/keyword-arguments-is-causing-allocations/41798/5 "2020-06-21T00:03:23Z")

</div>

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