JuliaArrays / AxisArrays.jl

Performant arrays where each dimension can have a named axis with values
http://JuliaArrays.github.io/AxisArrays.jl/latest/
Other
200 stars 41 forks source link

Axes are copied when taking a view #147

Open kcajf opened 5 years ago

kcajf commented 5 years ago

Having looked at the source code it is clear that when taking a view of an AxisArray, the indexed axes might be copied if _new_axes is called (https://github.com/JuliaArrays/AxisArrays.jl/blob/master/src/indexing.jl#L80). Is this the intended behaviour? It would make more sense to me for views to be taken of the original axes as well, to aid performance.

Example below (there is 16KB of allocation - i.e. the index copy)

using Dates
using AxisArrays
using BenchmarkTools

index = collect(DateTime(2018, 1, 1):Minute(1):DateTime(2018, 1, 10))
B = AxisArray(randn(Float64, (length(index), 10)), index)

@btime view(B, 1000:3000, :)
# 994.800 ns (6 allocations: 15.98 KiB)

If this should be fixed / changed, I'm happy to give a PR a go.

kcajf commented 5 years ago

Does anyone strongly oppose this? I think it's important to somehow have the ability to avoid copying the axes, even if not via this syntax.

timholy commented 5 years ago

In this example, do you need to call collect? If you skip the collect, presumably it's also fast and allocation-free?

But of course there may be cases where you can't use a range, and hence this seems like a reasonable idea. I'll comment over in #148.