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 args + default args = type instability #15468

Closed
ssfrr opened this issue Mar 12, 2016 · 1 comment · Fixed by #15511
Closed

keyword args + default args = type instability #15468

ssfrr opened this issue Mar 12, 2016 · 1 comment · Fixed by #15511
Assignees
Labels
compiler:lowering Syntax lowering (compiler front end, 2nd stage) potential benchmark Could make a good benchmark in BaseBenchmarks

Comments

@ssfrr
Copy link
Contributor

ssfrr commented Mar 12, 2016

I'm not sure if this is a dup of some of the more general keyword argument performance issues, so feel free to insta-close if so.

It seems there's an interaction between positional arguments with default values and keyword arguments. For instance, without a keyword arg, everything's fine with or without giving the optional arg:

julia> foo(a, b=1) = a+b
foo (generic function with 2 methods)

julia> code_warntype(foo, (Int, Int))
Variables:
  a::Int64
  b::Int64

Body:
  begin  # none, line 1:
      return (Base.box)(Base.Int,(Base.add_int)(a::Int64,b::Int64))
  end::Int64

julia> code_warntype(foo, (Int,))
Variables:
  a::Int64

Body:
  begin  # none, line 1:
      return (Base.box)(Base.Int,(Base.add_int)(a::Int64,1))
  end::Int64

If I include a keyword argument though, I get type instability:

julia> bar(a, b=1; kwarg=42) = a + b + kwarg
bar (generic function with 2 methods)

julia> code_warntype(bar, (Int, Int))
Variables:
  a::Int64
  b::Int64
  ####xs#7097#7098::Tuple{}

Body:
  begin
      $(Expr(:line, 1, :none, symbol("")))
      return (Base.box)(Base.Int,(Base.add_int)((Base.box)(Base.Int,(Base.add_int)(a::Int64,b::Int64)),42))
  end::Int64

julia> code_warntype(bar, (Int,))
Variables:
  a::Int64

Body:
  begin
      $(Expr(:line, 1, :none, symbol("")))
      GenSym(8) = (top(ccall))(:jl_alloc_array_1d,(top(apply_type))(Base.Array,Any,1)::Type{Array{Any,1}},(top(svec))(Base.Any,Base.Int)::SimpleVector,Array{Any,1},0,0,0)::Array{Any,1}
      GenSym(9) = GenSym(8)
      return (Main.__bar#0__)(GenSym(9),a::Int64)::Any
  end::Any

Now the 1-argument version returns Any. Based on @simonster's comment here, I think the problem is that the 1-argument version basically becomes bar(a; kwarg=42) = bar(a, 1; kwarg=kwarg), which triggers the creation of the Any array for keyword argument parsing.

@JeffBezanson JeffBezanson added the compiler:lowering Syntax lowering (compiler front end, 2nd stage) label Mar 12, 2016
@JeffBezanson JeffBezanson self-assigned this Mar 12, 2016
@JeffBezanson
Copy link
Member

I think the lowering can be changed to be more efficient here, and possibly also generate less code while we're at it.

@tkelman tkelman added the potential benchmark Could make a good benchmark in BaseBenchmarks label Mar 15, 2016
JeffBezanson added a commit that referenced this issue Mar 15, 2016
fix #15468, inefficient lowering of keyword+optional args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:lowering Syntax lowering (compiler front end, 2nd stage) potential benchmark Could make a good benchmark in BaseBenchmarks
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants