Skip to content

Commit 5bbe070

Browse files
committed
Merge pull request #13323 from JuliaLang/teh/deepcopy
Circumvent type-instability of deepcopy
2 parents 61f2d2b + 32e7d75 commit 5bbe070

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

base/deepcopy.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Note: deepcopy_internal(::Any, ::ObjectIdDict) is
66
# only exposed for specialization by libraries
77

8-
deepcopy(x) = deepcopy_internal(x, ObjectIdDict())
8+
deepcopy(x) = deepcopy_internal(x, ObjectIdDict())::typeof(x)
99

1010
deepcopy_internal(x::Union{Symbol,LambdaInfo,TopNode,GlobalRef,DataType,Union,Task},
1111
stackdict::ObjectIdDict) = x

base/sharedarray.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ convert{TS,TA,N}(::Type{SharedArray{TS,N}}, A::Array{TA,N}) = (S = SharedArray(T
281281

282282
function deepcopy_internal(S::SharedArray, stackdict::ObjectIdDict)
283283
haskey(stackdict, S) && return stackdict[S]
284-
# Note: copy can be used here because SharedArrays are restricted to isbits types
285-
R = copy(S)
284+
R = SharedArray(eltype(S), size(S); pids = S.pids)
285+
copy!(sdata(R), sdata(S))
286286
stackdict[S] = R
287287
return R
288288
end

test/copy.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ end
5050

5151
# test behavior of shallow and deep copying
5252
let a = Any[[1]], q = QuoteNode([1])
53-
ca = copy(a); dca = deepcopy(a)
53+
ca = copy(a); dca = @inferred(deepcopy(a))
5454
@test ca !== a
5555
@test ca[1] === a[1]
5656
@test dca !== a

test/parallel_exec.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,15 @@ pids_d = procs(d)
401401
remotecall_fetch(setindex!, pids_d[findfirst(id->(id != myid()), pids_d)], d, 1.0, 1:10)
402402
@test ds != d
403403
@test s != d
404+
copy!(d, s)
405+
@everywhere setid!(A) = A[localindexes(A)] = myid()
406+
@sync for p in procs(ds)
407+
@async remotecall_wait(setid!, p, ds)
408+
end
409+
@test d == s
410+
@test ds != s
411+
@test first(ds) == first(procs(ds))
412+
@test last(ds) == last(procs(ds))
404413

405414

406415
# SharedArray as an array
@@ -841,4 +850,3 @@ end
841850
v15406 = remotecall_wait(() -> 1, id_other)
842851
fetch(v15406)
843852
remotecall_wait(t -> fetch(t), id_other, v15406)
844-

0 commit comments

Comments
 (0)