From 98258ae661e5df0fb168a1a4cb247a33977ed10c Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Tue, 16 May 2017 17:44:12 -0400 Subject: [PATCH] copy-free delegating of rest keywords --- base/boot.jl | 2 -- base/essentials.jl | 6 +++++- base/methodshow.jl | 2 +- src/julia-syntax.scm | 10 +++++++++- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/base/boot.jl b/base/boot.jl index d0a686e761bf50..8440465167315d 100644 --- a/base/boot.jl +++ b/base/boot.jl @@ -147,8 +147,6 @@ export # constants nothing, Main -const AnyVector = Array{Any,1} - abstract type Number end abstract type Real <: Number end abstract type AbstractFloat <: Real end diff --git a/base/essentials.jl b/base/essentials.jl index 805fa564ef2a1e..0e4299f7032a5c 100644 --- a/base/essentials.jl +++ b/base/essentials.jl @@ -336,7 +336,9 @@ function vector_any(xs::ANY...) a end -function as_kwargs(xs::Union{AbstractArray,Associative}) +as_kwargs(xs::Union{AbstractArray,Associative}) = collect_as_kwargs(xs) + +function collect_as_kwargs(xs::Union{AbstractArray,Associative}) n = length(xs) to = Vector{Any}(n*2) i = 1 @@ -382,6 +384,8 @@ end const KWDict = ImmutableDict{Symbol,Any} const EmptyKWDict = KWDict() +as_kwargs(xs::KWDict) = xs + """ ImmutableDict diff --git a/base/methodshow.jl b/base/methodshow.jl index c9f304a509c9e9..a258b9df7b6270 100644 --- a/base/methodshow.jl +++ b/base/methodshow.jl @@ -67,7 +67,7 @@ function arg_decl_parts(m::Method) end function kwarg_decl(m::Method, kwtype::DataType) - sig = rewrap_unionall(Tuple{kwtype, Core.AnyVector, unwrap_unionall(m.sig).parameters...}, m.sig) + sig = rewrap_unionall(Tuple{kwtype, Any, unwrap_unionall(m.sig).parameters...}, m.sig) kwli = ccall(:jl_methtable_lookup, Any, (Any, Any, UInt), kwtype.name.mt, sig, typemax(UInt)) if kwli !== nothing kwli = kwli::Method diff --git a/src/julia-syntax.scm b/src/julia-syntax.scm index ef42464fc7cc29..0f5e2f18b3f840 100644 --- a/src/julia-syntax.scm +++ b/src/julia-syntax.scm @@ -472,7 +472,7 @@ `((|::| ;; if there are optional positional args, we need to be able to reference the function name ,(if (any kwarg? pargl) (gensy) UNUSED) - (call (core kwftype) ,ftype)) (:: ,kw (core AnyVector)) ,@pargl ,@vararg) + (call (core kwftype) ,ftype)) ,kw ,@pargl ,@vararg) `(block ;; initialize keyword args to their defaults, or set a flag telling ;; whether this keyword needs to be set. @@ -486,6 +486,13 @@ flags) ,@(if (null? restkw) '() `((= ,rkw (top EmptyKWDict)))) + ,(if (and (not (null? restkw)) (null? keynames)) + `(if (call (core isa) ,kw (top KWDict)) + (block + (= ,rkw ,kw) ;; reuse kwarg list for delegation if possible + (symbolicgoto do_call))) + `(if (call (core isa) ,kw (top KWDict)) + (= ,kw (call (top collect_as_kwargs) ,kw)))) ;; for i = 1:(length(kw)>>1) (for (= ,i (: (call (top >>) (call (top length) ,kw) 1) -1 1)) (block @@ -546,6 +553,7 @@ '() `((if ,flag (= ,name ,dflt))))) keynames vals flags)) + (symboliclabel do_call) ;; finally, call the core function (return (call ,mangled ,@keynames