-
Notifications
You must be signed in to change notification settings - Fork 44
Closed
Description
For slightly more complex structs than the one provided in the examples, iterating over LazyRows actually allocates and hurts performance:
using StructArrays, BenchmarkTools
struct Foo
idx::Int
val::Float64
end
sarr = StructArray(Foo(i, 0.0) for i in 1:10000)
val = 1.0
function testalloc(sarr, val)
for f in LazyRows(sarr)
f.val = val
end
end
# This function does not allocate, I just replaced the f.val call by the inlined equivalent.
function testnoalloc(sarr, val)
for f in LazyRows(sarr)
getproperty(getfield(f, 1), :val)[getfield(f, 2)] = val
end
end
@btime testalloc($sarr, $val)
# 220.941 μs (19489 allocations: 304.52 KiB)
@btime testnoalloc($sarr, $val)
# 6.110 μs (0 allocations: 0 bytes)
This is caused by a type instability in the setproperty! function. Any idea how to prevent this?
Thanks
Metadata
Metadata
Assignees
Labels
No labels