Skip to content

Commit

Permalink
Add fast method for copyto!(::Memory, ::Memory) (#55082)
Browse files Browse the repository at this point in the history
Previously, this method hit the slow generic AbstractArray fallback.

Closes #55079

This is an ad-hoc bandaid that really ought to be fixed by resolving
#54581.

(cherry picked from commit ec90012)
  • Loading branch information
jakobnissen authored and KristofferC committed Jul 12, 2024
1 parent 4929aa2 commit 7e5214d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions base/genericmemory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ end

copy(a::T) where {T<:Memory} = ccall(:jl_genericmemory_copy, Ref{T}, (Any,), a)

copyto!(dest::Memory, src::Memory) = copyto!(dest, 1, src, 1, length(src))
function copyto!(dest::Memory, doffs::Integer, src::Memory, soffs::Integer, n::Integer)
n < 0 && _throw_argerror("Number of elements to copy must be non-negative.")
unsafe_copyto!(dest, doffs, src, soffs, n)
Expand Down
9 changes: 9 additions & 0 deletions test/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ chnlprod(x) = Channel(c->for i in x; put!(c,i); end)

@test_throws Union{BoundsError, ArgumentError} copyto!(dest, 1, src(), 2, 2)
end

v = rand(Float32, 4)
a = Memory{Float32}(v)
b = similar(a)
copyto!(b, a)
@test a == b

c = Memory{Float32}(undef, 3)
@test_throws BoundsError copyto!(c, a)
end

@testset "with CartesianIndices" begin
Expand Down

0 comments on commit 7e5214d

Please sign in to comment.