Assigning values of different types to unused variable


#1

Sometimes it is convenient to use _ as a placehold for return values you do not care about. I wonder if there is any “danger” in something similar to:

f() = 1.0, 1, "foo"

function g()
    a, _, _ = f()
end

Here the variable _ is assigned to twice with different types so it is not type stable. Will this be a performance problem even if I never use _ later in the function?


#2

Unused variables will be optimized out in this case, but I’m not sure whether this always happens. Checking @code_llvm would be helpful.

julia> function g()
           a, _, _ = f()
           return a + 1
       end
g (generic function with 1 method)

julia> g()
2.0

julia> @code_llvm g()

define double @julia_g_70869() #0 {
top:
  %0 = load double, double* inttoptr (i64 4584977520 to double*), align 16
  %1 = fadd double %0, 1.000000e+00
  ret double %1
}

#3

Please use code_warntype instead of these.


#4

@code_warntype will show the type instability but not if it is a performance problem during run time?


#5

I think the point of showing LLVM code is that the ignored variable is clearly completely eliminated in this case, which makes sense to me. Even if @code_warntype is really unhappy about the inferred types, if the LLVM code is fine it won’t affect performance.


#6

The analysis in https://github.com/JuliaLang/julia/pull/15558#issuecomment-265533514 seems to indicate that it can sometimes be a problem.