Skip to content

LazyRow allocations #152

@touste

Description

@touste

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions