Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyword calls are still slow #29083

Closed
simonbyrne opened this issue Sep 7, 2018 · 2 comments
Closed

Keyword calls are still slow #29083

simonbyrne opened this issue Sep 7, 2018 · 2 comments
Assignees
Labels
compiler:optimizer Optimization passes (mostly in base/compiler/ssair/) keyword arguments f(x; keyword=arguments) performance Must go faster

Comments

@simonbyrne
Copy link
Contributor

It seems that keywords are still quite a bit slower than using positional arguments. Interestingly, the slowdown doesn't affect named tuples:

using BenchmarkTools

normrand(μ,σ) = μ + σ*randn()
normrand(;μ,σ) = normrand(μ,σ)
normrand((μ,σ)::NamedTuple{(:μ,:σ)}) = μ + σ*randn()

@btime normrand(2.0,0.1)
@btime normrand(μ=2.0,σ=0.1)
@btime normrand((μ=2.0,σ=0.1))

On current master I get:

julia> @btime normrand(2.0,0.1)
  6.809 ns (0 allocations: 0 bytes)
2.0950921781577936

julia> @btime normrand(μ=2.0,σ=0.1)
  10.101 ns (0 allocations: 0 bytes)
1.9112631196445133

julia> @btime normrand((μ=2.0,σ=0.1))
  7.015 ns (0 allocations: 0 bytes)
1.9163927942340138```
@simonbyrne simonbyrne added the performance Must go faster label Sep 7, 2018
@KristofferC
Copy link
Sponsor Member

KristofferC commented Sep 7, 2018

The difference in typed code between no kw and kw

julia> @code_typed f1()
CodeInfo(
1 1 ─ %1 = Main.randn::typeof(randn)                                                                     │╻  normrand
  │   %2 = invoke %1(Random.GLOBAL_RNG::Random.MersenneTwister)::Float64                                 ││╻  randn
  │   %3 = (Base.mul_float)(0.1, %2)::Float64                                                            ││╻  *
  │   %4 = (Base.add_float)(2.0, %3)::Float64                                                            ││╻  +
  └──      return %4                                                                                     │
) => Float64

julia> @code_typed f2()
CodeInfo(
1 1 ──       invoke Core.kwfunc(Main.normrand::Any)::Const(#kw##normrand(), false)                │
  │    %2  = (Base.sle_int)(1, 1)::Bool                                                           │╻╷╷╷╷  #normrand
  └───       goto #3 if not %2                                                                    ││┃│││   isempty
  2 ── %4  = (Base.sle_int)(1, 0)::Bool                                                           │││┃│││   iterate
  └───       goto #4                                                                              ││││┃│     iterate
  3 ──       nothing::Nothing                                                                     │
  4 ┄─ %7  = φ (#2 => %4, #3 => false)::Bool                                                      │││││┃      iterate
  └───       goto #6 if not %7                                                                    ││││││
  5 ──       invoke Base.getindex(()::Tuple{}, 1::Int64)::Union{}                                 ││││││
  └───       $(Expr(:unreachable))::Union{}                                                       ││││││
  6 ──       goto #8                                                                              ││││││
  7 ──       $(Expr(:unreachable))::Union{}                                                       ││││││
  8 ┄─       goto #9                                                                              │││││
  9 ──       goto #10                                                                             │││╻      iterate
  10 ─       goto #11                                                                             │││
  11 ─ %16 = Main.randn::typeof(randn)                                                            │││╻      normrand
  │    %17 = invoke %16(Random.GLOBAL_RNG::Random.MersenneTwister)::Float64                       ││││╻      randn
  │    %18 = (Base.mul_float)(0.1, %17)::Float64                                                  ││││╻      *
  │    %19 = (Base.add_float)(2.0, %18)::Float64                                                  ││││╻      +
  └───       goto #12                                                                             ││
  12 ─       return %19                                                                           │
) => Float64

Block 11 is exactly the non-kw version.

@JeffBezanson
Copy link
Sponsor Member

It looks like we just need to make it possible to eliminate the call to kwfunc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:optimizer Optimization passes (mostly in base/compiler/ssair/) keyword arguments f(x; keyword=arguments) performance Must go faster
Projects
None yet
Development

No branches or pull requests

3 participants