Setfield.@set
is a great tool for manipulating StaticArrays
efficiently
using Setfield
using StaticArrays
function SFib1(::Val{S}) where S
values = zeros(SVector{S, Int})
@inbounds for i in 1:S
values = @set values[i] = (i == 1 || i == 2) ? 1 : values[i-1] + values[i-2]
end
values
end
julia> @code_llvm SFib1(Val(4))
; Function SFib1
; Location: REPL[132]:2
define void @julia_SFib1_38655({ [4 x i64] }* noalias nocapture sret) {
L45.3:
; Location: REPL[132]:4
; Function macro expansion; {
; Location: /home/takafumi/.julia/packages/Setfield/X9IQb/src/sugar.jl:77
%1 = bitcast { [4 x i64] }* %0 to <4 x i64>*
store <4 x i64> <i64 1, i64 1, i64 2, i64 3>, <4 x i64>* %1, align 8
ret void
;}
}