Iβm playing with a SOA transform (AoS and SoA - Wikipedia) via StructArrays.jl. My code:
using StructArrays
function test1()
n = 10
v = Vector{Tuple{Int, Int}}(undef, n)
s = 0
for i in 1:n
v[i] = Base.setindex(v[i], 2 * i - 1, 1)
v[i] = Base.setindex(v[i], 2 * i, 2)
v1 = v[i][1]
v2 = v[i][2]
# v1, v2 = v[i]
s += v1 + v2
end
@assert s == n * (2 * n + 1)
end
function test2()
n = 100000
v = StructArray{Tuple{Int, Int}}(undef, n)
s = 0
for i in 1:n
v[i] = Base.setindex(v[i], 2 * i - 1, 1)
v[i] = Base.setindex(v[i], 2 * i, 2)
v1 = v[i][1]
v2 = v[i][2]
# v1, v2 = v[i]
s += v1 + v2
end
@assert s == n * (2 * n + 1)
end
@btime test1()
@btime test2()
Result:
33.132 ns (1 allocation: 240 bytes)
134.200 ΞΌs (4 allocations: 1.53 MiB)
What am I missing?