Hi, I often have the following structure in my code: a function Bar calls a function Foo with multiple outputs and then uses all its outputs to return something (MWE below). Is there a performant way of golfing such functions into one-line functions? Performant here means: no loss in speed (or even an improvement), no decrease in type stability etc.
Very simple example (where it is of course not the purpose to unfold the definition of Foo in Bar, that defeats the purpose):
function Foo(x::Real)
return x, x^2, x^3
end
function Bar(x::Real)
x, y, z = Foo(x)
return Foo2(x, z, y)
end
You can do Foo2(getindex.(Foo(x), (1, 3, 2))...) and that should not lead to any overhead due to the literal tuple const folding away. If Foo is not scalar-broadcastable you can wrap it in Ref(...)
The performance concerns with splatting is when the compiler does not know the length and types of the elements. Splatting an NTuple{3, T} the compiler knows exactly which method to call.
Splatting a Vector{T}, on the other hand, has performance implications, since its length is not statically known.