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

Unexpected allocation on views of AxisArray #191

Open bgroenks96 opened 3 years ago

bgroenks96 commented 3 years ago

Consider the following simple example:

const A = randn(100);
aa = AxisArray(A;x=SortedVector([i for i in 1:100]));
function axisarrays_alloc(array,xs)
           @view array[xs];
           nothing;
 end
xs = 2:10;
@benchmark axisarrays_alloc(aa,xs)

results in:

BenchmarkTools.Trial: 
  memory estimate:  352 bytes
  allocs estimate:  7
  --------------
  minimum time:     393.592 ns (0.00% GC)
  median time:      399.507 ns (0.00% GC)
  mean time:        443.488 ns (4.64% GC)
  maximum time:     15.236 μs (96.24% GC)
  --------------
  samples:          10000
  evals/sample:     201

This occurs if x is a SortedVector or plain Vector but not if it is a range. I believe this is related to #147 since the axes are being copied even in a view. I have not yet verified whether or not it is fixed by #148.