JuliaData / TypedTables.jl

Simple, fast, column-based storage for data analysis in Julia
Other
145 stars 25 forks source link

Performance issue on access Table with symbol not in function scope #101

Open waldie11 opened 1 year ago

waldie11 commented 1 year ago

How do I access allocation free AND flexible a TypedTable?

Nevermind the way quicker access at the NamedTuple.


using TypedTables
using BenchmarkTools

y = (a=1:1_000,)
x = Table(y)
v = zeros(eltype(getproperty(x,:a)),100)

#this fixes the column statically to label :a
function access_table!(v,x)
    for i in 1:length(x)
        v[i%length(v)+1] += getproperty(x,:a)[i]
    end
end
@btime access_table!($v,$x)
@btime access_table!($v,$y)

5.093 μs (0 allocations: 0 bytes) 4.200 ns (0 allocations: 0 bytes)

function access_table2!(v,x;label=:a)
    for i in 1:length(x)
        v[i%length(v)+1] += getproperty(x,label)[i]
    end
end
@btime access_table2!($v,$x)
@btime access_table2!($v,$y)

51.871 μs (2000 allocations: 62.50 KiB) 3.800 ns (0 allocations: 0 bytes)

function access_table3!(v,x::TypedTables.Table;label=:a)
    n = getproperty(columns(x),label)
    for i in 1:length(x)
        v[i%length(v)+1] += #= getproperty(x,label) =#n[i]
    end
end
@btime access_table3!($v,$x)

5.633 μs (2 allocations: 64 bytes)

waldie11 commented 1 year ago

Got it... Can there be done sth to help improve access natively? This does not sound like a job for AcceleratedArrays.jl, but I might be wrong.

using TypedTables
using BenchmarkTools

y = (a=1:1_000,)
x = Table(y)
v = zeros(eltype(getproperty(x,:a)),100)
function access_table!(v,x)
    for i in 1:length(x)
        v[i%length(v)+1] += getproperty(x,:a)[i]
    end
end
@btime access_table!($v,$x)
@btime access_table!($v,$y)

5.320 μs (0 allocations: 0 bytes) 4.110 ns (0 allocations: 0 bytes)

function access_table3!(v,x::NamedTuple;label=:a)
    for i in 1:length(x)
        v[i%length(v)+1] += getproperty(x,label)[i]
    end
end
function access_table3!(v,x::TypedTables.Table;label=:a)
    z = columns(x)
    return access_table3!(v,z,label=label)
end
@btime access_table3!($v,$x)
@btime access_table3!($v,$y)

4.230 ns (0 allocations: 0 bytes) 4.320 ns (0 allocations: 0 bytes)