jling
July 31, 2021, 12:01pm
7
that’s literally the only purpose it serves… (comparing 2 versions of function is same idea)
there’s really a ton of discussion on this, and the answer is yes, don’t use value as types but also don’t be afraid to write branch if you need to, sometimes constant folding gives you a ride:
Yes, it generally will.
julia> f() = ntuple(x -> x + 5, Val(2))
f (generic function with 2 methods)
julia> @code_llvm f()
; @ REPL[45]:1 within `f'
define void @julia_f_12684([2 x i64]* noalias nocapture sret) {
top:
%1 = bitcast [2 x i64]* %0 to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %1, i8* bitcast ([2 x i64]* @0 to i8*), i64 16, i32 8, i1 false)
ret void
}
julia> @code_native f()
.section __TEXT,__text,regular,pure_instructions
; ┌ @ REPL[49]:1 within `f'
…
In general, type computations in Julia are made for things that should be done at compilation time. If you cannot do the calculation at compilation time, then you will get performance hits by using the dispatch system (but if you can do things at compilation time, then you will get performance gains!). So it’s a tool to be used wisely.
In this case, it sounds like you want to dispatch on runtime values. This means you are in the case where you will get performance losses. From what I measured …
3 Likes