Skip to content

Commit 102e840

Browse files
committed
faster copy! between arrays of different types (ref #11004)
1 parent 252bb1b commit 102e840

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

base/abstractarray.jl

+17-3
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ end
211211

212212
squeeze(A::AbstractArray, dim::Integer) = squeeze(A, (Int(dim),))
213213

214+
## from general iterable to any array
215+
214216
function copy!(dest::AbstractArray, src)
215217
i = 1
216218
for x in src
@@ -220,7 +222,6 @@ function copy!(dest::AbstractArray, src)
220222
return dest
221223
end
222224

223-
# copy with minimal requirements on src
224225
# if src is not an AbstractArray, moving to the offset might be O(n)
225226
function copy!(dest::AbstractArray, doffs::Integer, src)
226227
doffs < 1 && throw(BoundsError())
@@ -247,7 +248,7 @@ function copy!(dest::AbstractArray, doffs::Integer, src, soffs::Integer)
247248
dn = done(src, st)
248249
dn && throw(BoundsError())
249250
i, dmax = doffs, length(dest)
250-
@inbounds while !dn
251+
@inbounds while !dn
251252
i > dmax && throw(BoundsError())
252253
val, st = next(src, st)
253254
dest[i] = val
@@ -280,7 +281,20 @@ function copy!(dest::AbstractArray, doffs::Integer, src, soffs::Integer, n::Inte
280281
return dest
281282
end
282283

283-
# if src is an AbstractArray and a source offset is passed, use indexing
284+
## copy between abstract arrays - generally more efficient
285+
## since a single index variable can be used.
286+
287+
function copy!(dest::AbstractArray, src::AbstractArray)
288+
n = length(src)
289+
if n > length(dest)
290+
throw(BoundsError())
291+
end
292+
@inbounds for i = 1:n
293+
dest[i] = src[i]
294+
end
295+
return dest
296+
end
297+
284298
function copy!(dest::AbstractArray, doffs::Integer, src::AbstractArray)
285299
copy!(dest, doffs, src, 1, length(src))
286300
end

0 commit comments

Comments
 (0)