Skip to content

Commit

Permalink
Merge pull request #13323 from JuliaLang/teh/deepcopy
Browse files Browse the repository at this point in the history
Circumvent type-instability of deepcopy
  • Loading branch information
timholy committed Apr 6, 2016
2 parents 61f2d2b + 32e7d75 commit 5bbe070
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion base/deepcopy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Note: deepcopy_internal(::Any, ::ObjectIdDict) is
# only exposed for specialization by libraries

deepcopy(x) = deepcopy_internal(x, ObjectIdDict())
deepcopy(x) = deepcopy_internal(x, ObjectIdDict())::typeof(x)

deepcopy_internal(x::Union{Symbol,LambdaInfo,TopNode,GlobalRef,DataType,Union,Task},
stackdict::ObjectIdDict) = x
Expand Down
4 changes: 2 additions & 2 deletions base/sharedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ convert{TS,TA,N}(::Type{SharedArray{TS,N}}, A::Array{TA,N}) = (S = SharedArray(T

function deepcopy_internal(S::SharedArray, stackdict::ObjectIdDict)
haskey(stackdict, S) && return stackdict[S]
# Note: copy can be used here because SharedArrays are restricted to isbits types
R = copy(S)
R = SharedArray(eltype(S), size(S); pids = S.pids)
copy!(sdata(R), sdata(S))
stackdict[S] = R
return R
end
Expand Down
2 changes: 1 addition & 1 deletion test/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ end

# test behavior of shallow and deep copying
let a = Any[[1]], q = QuoteNode([1])
ca = copy(a); dca = deepcopy(a)
ca = copy(a); dca = @inferred(deepcopy(a))
@test ca !== a
@test ca[1] === a[1]
@test dca !== a
Expand Down
10 changes: 9 additions & 1 deletion test/parallel_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,15 @@ pids_d = procs(d)
remotecall_fetch(setindex!, pids_d[findfirst(id->(id != myid()), pids_d)], d, 1.0, 1:10)
@test ds != d
@test s != d
copy!(d, s)
@everywhere setid!(A) = A[localindexes(A)] = myid()
@sync for p in procs(ds)
@async remotecall_wait(setid!, p, ds)
end
@test d == s
@test ds != s
@test first(ds) == first(procs(ds))
@test last(ds) == last(procs(ds))


# SharedArray as an array
Expand Down Expand Up @@ -841,4 +850,3 @@ end
v15406 = remotecall_wait(() -> 1, id_other)
fetch(v15406)
remotecall_wait(t -> fetch(t), id_other, v15406)

0 comments on commit 5bbe070

Please sign in to comment.