Inline function returns tuple of mixed type, assigned to a tuple of variables in the caller

Dear Julia community,

I wish to learn the appropriate usage of Julia tuples.

Consider the following scenario. One needs to make an inline function to separate a block of code inside a very complicated function. Naturally, this block of code operates with several local variables. When making the inline function, these local variables are passed into the inline function as arguments. More importantly, the inline function returns several variables after some operations. Since the inline function has its own scope, inside the inline function one cannot directly assign values to the variables from the caller, but rather return some variables. For example, I have made the following inline function:

@inline function compute_something(v1, v2, v3, v4)
    #...
    # some computations
    return (r1, r2, r3, r4, r5)
end

This inline function compute_something() separates the code inside a complicated function COMP(), which has local variables v1, v2, v3, v4, r1, r2, r3, r4, r5. I hope to use it inside COMP() as following

function COMP()
    # ...
    # some computations that prepares v1, v2, v3, v4
    (u1, u2, u3, u4, u5) = compute_something(v1, v2, v3, v4)
    # continue ...
end

Now the real question is performance: does this tuple assignment actually create a tuple and assign? Or it just paste the code of inline function into COMP() and assign the results of compute_something() to the local variables u1, u2, u3, u4, u5 inside COMP(), as if I write u1 = r1, u2 = r2, … ?

I think so. Using the following example functions:

julia> @noinline function compute_something(v1, v2, v3, v4)
           return (v1/1, v2/2, v3/3, v4/4, (v1+v2)/5)
       end
compute_something (generic function with 1 method)

julia> function COMP(v1, v2, v3, v4)
         (u1, u2, u3, u4, u5) = compute_something(v1, v2, v3, v4)
         return u1+u2+u3+u4+u5
       end
COMP (generic function with 1 method)

@code_llvm COMP(1,2,3,4) shows [5 x double] everywhere, which would be a tuple. But when you change that to @inline like your example, then there’s no [5 x double] anywhere, only individual doubles.

1 Like

Thank you @Benny ! I can use @code_llvm macro to inspect the actual code :smiley: