# Untyped keyword arguments

**URL:** https://discourse.julialang.org/t/untyped-keyword-arguments/24228
**Category:** General Usage
**Tags:** question
**Created:** [May 15, 2019, 8:39am UTC](https://discourse.julialang.org/t/untyped-keyword-arguments/24228 "2019-05-15T08:39:41Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [May 15, 2019, 8:39am UTC](https://discourse.julialang.org/t/untyped-keyword-arguments/24228/1 "2019-05-15T08:39:41Z")

</div>

**Summary: Is there any performance benefit to annotating keyword arguments with types?**

I understand that _typed_ keyword arguments are as efficient as positional arguments. Is that correct?  
But what are the performance characteristics of _untyped_ keyword arguments compared to typed keyword arguments?

My understanding is that keyword arguments do not participate in dispatch. But do method calls with different typed keyword arguments get compiled separately? (as is the case for differently typed positional arguments). If so then does this mean that explicitly typing keyword args in function definition does not help performance?

The following is my attempt as investigate this - very likely flawed.  
In any case, it seems all four invocations have the same benchmark time.  
@code\_warntype seems the same for f1, f2.

```julia
function f1(A; i::Int=1)
    return A[i]
end

function f2(A; i=1)
    return A[i]
end

using BenchmarkTools
B = [1,2,3]
@btime f1($B)
@btime f2($B)

@btime f1($B, i=2)
@btime f2($B, i=2)

```

---

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [May 15, 2019, 9:35am UTC](https://discourse.julialang.org/t/untyped-keyword-arguments/24228/2 "2019-05-15T09:35:30Z")

</div>

> [@greg\_plowman](#):
>
> Summary: Is there any performance benefit to annotating keyword arguments with types?

No. Keyword arguments do not participate in dispatch, but, like regular arguments, they get specialized on the _actual_ type of the values. I believe you could demonstrate by timing:

```julia
function f3(A; @nospecialize i=1)
    return A[i]
end

```

---

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [May 16, 2019, 12:02am UTC](https://discourse.julialang.org/t/untyped-keyword-arguments/24228/3 "2019-05-16T00:02:23Z")

</div>

OK thanks, that’s good to know.

However, the `@nospecialize` example didn’t show any timing difference for me.

On a related note, I naively wanted to use a “sentinel” value of the _same_ type for a keyword argument, thinking this would be type-stable (function g below)  
But now I realize that by using a different type, a branch could be elided at compile time, effectively giving static dispatch (function h below)

```julia
function g(A; i=0)
    if i == 0
        return 1.0
    else
        return A[i]
    end
end

function h(A; i=nothing)
    if isa(i, Nothing)
        return 1.0
    else
        return A[i]
    end
end

```

`@code_warntype` shows `h(A, i=2)` and `h(A, i=nothing)` are type-stable.

I think `i === nothing` could also work here, but `isa(i, Nothing)` produces cleaner output from `@code_warntype`
