Skip to content

Commit ee12f29

Browse files
Avoid splatting in closures_to_opaque for faster Expr construction
Replace `Expr(head, array...)` with direct `.args` assignment to avoid the overhead of splatting. This is the hot path in recursive AST traversal and reduces RGF creation time by ~19% and allocations by ~10%. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 972bdd7 commit ee12f29

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

src/RuntimeGeneratedFunctions.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,13 @@ function closures_to_opaque(ex::Expr, return_type = nothing)
359359
end
360360
return _ex
361361
elseif head === :generator
362-
f_args = Expr(:tuple, Any[x.args[1] for x in args[2:end]]...)
362+
f_args = Expr(:tuple)
363+
f_args.args = Any[x.args[1] for x in args[2:end]]
363364
iters = Any[x.args[2] for x in args[2:end]]
364-
return Expr(
365-
:call,
366-
GlobalRef(Base, :Generator),
367-
closures_to_opaque(Expr(:(->), f_args, args[1])),
368-
iters...
369-
)
365+
new_ex = Expr(:call, GlobalRef(Base, :Generator),
366+
closures_to_opaque(Expr(:(->), f_args, args[1])))
367+
append!(new_ex.args, iters)
368+
return new_ex
370369
elseif head === :opaque_closure
371370
return closures_to_opaque(args[1])
372371
elseif head === :return && return_type !== nothing
@@ -375,7 +374,9 @@ function closures_to_opaque(ex::Expr, return_type = nothing)
375374
_tconvert(return_type, closures_to_opaque(args[1], return_type))
376375
)
377376
end
378-
return Expr(head, Any[closures_to_opaque(x, return_type) for x in args]...)
377+
new_ex = Expr(head)
378+
new_ex.args = Any[closures_to_opaque(x, return_type) for x in args]
379+
return new_ex
379380
end
380381

381382
"""

0 commit comments

Comments
 (0)