I believe I have the basic idea of when to use ifelse
instead of the ternary operator. Basically, ifelse
avoids an expensive branch, but computes the two possibilities β which may be more expensive. (Also ifelse
may be friendler to autodiff.) But now throw tuples into the mix, and I have questions. Specifically, looking at these lines in the Julia source:
max(x::T, y::T) where {T<:Real} = ifelse(y < x, x, y)
min(x::T, y::T) where {T<:Real} = ifelse(y < x, y, x)
minmax(x::T, y::T) where {T<:Real} = y < x ? (y, x) : (x, y)
For min
and max
, thereβs nothing to compute between x
and y
, so just use ifelse
. But for minmax
, would those tuples actually get created? I have this very fuzzy notion that tuples are basically free, and the compiler knows how to deal with them very well. So, is this code saying theyβre not that free?
For example, if x
and y
are BigFloat
s (which do require allocation), would max
and min
get away without allocating somehow? Would minmax
have to allocate four BigFloat
s to fill the two tuples, and then just return one of the tuples?