From 5fc89373290a88d5c6706a0e0be3456da8b73044 Mon Sep 17 00:00:00 2001 From: Jakob Nybo Nissen Date: Mon, 8 Jul 2024 22:05:57 +0200 Subject: [PATCH] Add fast method for copyto!(::Memory, ::Memory) Previously, this method hit the slow generic AbstractArray fallback. --- base/genericmemory.jl | 1 + test/copy.jl | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/base/genericmemory.jl b/base/genericmemory.jl index db930d0de23c8..32c15a22e0db1 100644 --- a/base/genericmemory.jl +++ b/base/genericmemory.jl @@ -163,6 +163,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) diff --git a/test/copy.jl b/test/copy.jl index 12ac286b3b24f..b6ad53600027a 100644 --- a/test/copy.jl +++ b/test/copy.jl @@ -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