Skip to content

Commit b4f5ef9

Browse files
committed
copy-free delegating of rest keywords
1 parent 4f41043 commit b4f5ef9

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

base/essentials.jl

+5-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,9 @@ function vector_any(xs::ANY...)
336336
a
337337
end
338338

339-
function as_kwargs(xs::Union{AbstractArray,Associative})
339+
as_kwargs(xs::Union{AbstractArray,Associative}) = collect_as_kwargs(xs)
340+
341+
function collect_as_kwargs(xs::Union{AbstractArray,Associative})
340342
n = length(xs)
341343
to = Vector{Any}(n*2)
342344
i = 1
@@ -382,6 +384,8 @@ end
382384
const KWDict = ImmutableDict{Symbol,Any}
383385
const EmptyKWDict = KWDict()
384386

387+
as_kwargs(xs::KWDict) = xs
388+
385389
"""
386390
ImmutableDict
387391

src/julia-syntax.scm

+9-1
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@
472472
`((|::|
473473
;; if there are optional positional args, we need to be able to reference the function name
474474
,(if (any kwarg? pargl) (gensy) UNUSED)
475-
(call (core kwftype) ,ftype)) (:: ,kw (core AnyVector)) ,@pargl ,@vararg)
475+
(call (core kwftype) ,ftype)) ,kw ,@pargl ,@vararg)
476476
`(block
477477
;; initialize keyword args to their defaults, or set a flag telling
478478
;; whether this keyword needs to be set.
@@ -486,6 +486,13 @@
486486
flags)
487487
,@(if (null? restkw) '()
488488
`((= ,rkw (top EmptyKWDict))))
489+
,(if (and (not (null? restkw)) (null? keynames))
490+
`(if (call (core isa) ,kw (top KWDict))
491+
(block
492+
(= ,rkw ,kw) ;; reuse kwarg list for delegation if possible
493+
(symbolicgoto do_call)))
494+
`(if (call (core isa) ,kw (top KWDict))
495+
(= ,kw (call (top collect_as_kwargs) ,kw))))
489496
;; for i = 1:(length(kw)>>1)
490497
(for (= ,i (: (call (top >>) (call (top length) ,kw) 1) -1 1))
491498
(block
@@ -546,6 +553,7 @@
546553
'()
547554
`((if ,flag (= ,name ,dflt)))))
548555
keynames vals flags))
556+
(symboliclabel do_call)
549557
;; finally, call the core function
550558
(return (call ,mangled
551559
,@keynames

0 commit comments

Comments
 (0)