ArrayLayouts.jl icon indicating copy to clipboard operation
ArrayLayouts.jl copied to clipboard

Memory allocation with `copyto!` in some circumstances

Open junyuan-chen opened this issue 1 year ago • 0 comments

I notice that copyto! sometimes allocates. I guess this could be avoided by simply adding @inline in front of the copyto!s?

Here is an example with BlockArrays:

using BenchmarkTools
using BlockArrays
dest = rand(4, 4)
src = BlockMatrix(rand(4, 4), [2, 2], [2, 2])
@btime copyto!($dest, $src)

Staring from a fresh Julia session, the above results in one allocation:

julia> @btime copyto!($dest, $src)
  87.933 ns (1 allocation: 48 bytes)

There is no allocation if I do the following, which is exactly what copyto! does:

using ArrayLayouts: MemoryLayout, _copyto!
@btime _copyto!(MemoryLayout($dest), MemoryLayout($src), $dest, $src)
julia> @btime _copyto!(MemoryLayout($dest), MemoryLayout($src), $dest, $src)
  73.099 ns (0 allocations: 0 bytes)

junyuan-chen avatar Mar 09 '23 22:03 junyuan-chen