Skip to content

Commit

Permalink
Now using IdDict almost everywhere where ObjectIdDict was used
Browse files Browse the repository at this point in the history
Also added some tests
  • Loading branch information
mauro3 committed Dec 19, 2017
1 parent a2aea11 commit ac95324
Show file tree
Hide file tree
Showing 17 changed files with 95 additions and 54 deletions.
25 changes: 12 additions & 13 deletions base/deepcopy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# deep copying

# Note: deepcopy_internal(::Any, ::ObjectIdDict) is
# Note: deepcopy_internal(::Any, ::IdDict) is
# only exposed for specialization by libraries

"""
Expand All @@ -19,21 +19,21 @@ i.e. functions which may contain hidden internal references.
While it isn't normally necessary, user-defined types can override the default `deepcopy`
behavior by defining a specialized version of the function
`deepcopy_internal(x::T, dict::ObjectIdDict)` (which shouldn't otherwise be used),
`deepcopy_internal(x::T, dict::IdDict)` (which shouldn't otherwise be used),
where `T` is the type to be specialized for, and `dict` keeps track of objects copied
so far within the recursion. Within the definition, `deepcopy_internal` should be used
in place of `deepcopy`, and the `dict` variable should be
updated as appropriate before returning.
"""
deepcopy(x) = deepcopy_internal(x, ObjectIdDict())::typeof(x)
deepcopy(x) = deepcopy_internal(x, IdDict())::typeof(x)

deepcopy_internal(x::Union{Symbol,Core.MethodInstance,Method,GlobalRef,DataType,Union,Task},
stackdict::ObjectIdDict) = x
deepcopy_internal(x::Tuple, stackdict::ObjectIdDict) =
stackdict::IdDict) = x
deepcopy_internal(x::Tuple, stackdict::IdDict) =
ntuple(i->deepcopy_internal(x[i], stackdict), length(x))
deepcopy_internal(x::Module, stackdict::ObjectIdDict) = error("deepcopy of Modules not supported")
deepcopy_internal(x::Module, stackdict::IdDict) = error("deepcopy of Modules not supported")

function deepcopy_internal(x::SimpleVector, stackdict::ObjectIdDict)
function deepcopy_internal(x::SimpleVector, stackdict::IdDict)
if haskey(stackdict, x)
return stackdict[x]
end
Expand All @@ -42,7 +42,7 @@ function deepcopy_internal(x::SimpleVector, stackdict::ObjectIdDict)
return y
end

function deepcopy_internal(x::String, stackdict::ObjectIdDict)
function deepcopy_internal(x::String, stackdict::IdDict)
if haskey(stackdict, x)
return stackdict[x]
end
Expand All @@ -51,7 +51,7 @@ function deepcopy_internal(x::String, stackdict::ObjectIdDict)
return y
end

function deepcopy_internal(@nospecialize(x), stackdict::ObjectIdDict)
function deepcopy_internal(@nospecialize(x), stackdict::IdDict)
T = typeof(x)::DataType
nf = nfields(x)
(isbits(T) || nf == 0) && return x
Expand All @@ -71,14 +71,14 @@ function deepcopy_internal(@nospecialize(x), stackdict::ObjectIdDict)
return y::T
end

function deepcopy_internal(x::Array, stackdict::ObjectIdDict)
function deepcopy_internal(x::Array, stackdict::IdDict)
if haskey(stackdict, x)
return stackdict[x]
end
_deepcopy_array_t(x, eltype(x), stackdict)
end

function _deepcopy_array_t(@nospecialize(x), T, stackdict::ObjectIdDict)
function _deepcopy_array_t(@nospecialize(x), T, stackdict::IdDict)
if isbits(T)
return (stackdict[x]=copy(x))
end
Expand All @@ -96,7 +96,7 @@ function _deepcopy_array_t(@nospecialize(x), T, stackdict::ObjectIdDict)
return dest
end

function deepcopy_internal(x::Dict, stackdict::ObjectIdDict)
function deepcopy_internal(x::Dict, stackdict::IdDict)
if haskey(stackdict, x)
return stackdict[x]::typeof(x)
end
Expand All @@ -112,4 +112,3 @@ function deepcopy_internal(x::Dict, stackdict::ObjectIdDict)
end
dest
end

10 changes: 5 additions & 5 deletions base/docs/Docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ export doc
const modules = Module[]
const META = gensym(:meta)

meta(m::Module) = isdefined(m, META) ? getfield(m, META) : ObjectIdDict()
meta(m::Module) = isdefined(m, META) ? getfield(m, META) : IdDict()

function initmeta(m::Module)
if !isdefined(m, META)
eval(m, :(const $META = $(ObjectIdDict())))
eval(m, :(const $META = $(IdDict())))
push!(modules, m)
end
nothing
Expand Down Expand Up @@ -216,9 +216,9 @@ mutable struct MultiDoc
"Ordered (via definition order) vector of object signatures."
order::Vector{Type}
"Documentation for each object. Keys are signatures."
docs::ObjectIdDict
docs::IdDict

MultiDoc() = new(Type[], ObjectIdDict())
MultiDoc() = new(Type[], IdDict())
end

# Docstring registration.
Expand Down Expand Up @@ -712,7 +712,7 @@ function docm(source::LineNumberNode, mod::Module, meta, ex, define = true)

# All other expressions are undocumentable and should be handled on a case-by-case basis
# with `@__doc__`. Unbound string literals are also undocumentable since they cannot be
# retrieved from the module's metadata `ObjectIdDict` without a reference to the string.
# retrieved from the module's metadata `IdDict` without a reference to the string.
docerror(ex)
end

Expand Down
2 changes: 1 addition & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export
MergeSort,
Missing,
NTuple,
ObjectIdDict,
IdDict,
OrdinalRange,
Pair,
PartialQuickSort,
Expand Down
2 changes: 1 addition & 1 deletion base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ Base.add_with_overflow(a::BigInt, b::BigInt) = a + b, false
Base.sub_with_overflow(a::BigInt, b::BigInt) = a - b, false
Base.mul_with_overflow(a::BigInt, b::BigInt) = a * b, false

function Base.deepcopy_internal(x::BigInt, stackdict::ObjectIdDict)
function Base.deepcopy_internal(x::BigInt, stackdict::IdDict)
if haskey(stackdict, x)
return stackdict[x]
end
Expand Down
2 changes: 1 addition & 1 deletion base/libuv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ disassociate_julia_struct(handle::Ptr{Void}) =

# A dict of all libuv handles that are being waited on somewhere in the system
# and should thus not be garbage collected
const uvhandles = ObjectIdDict()
const uvhandles = IdDict()
preserve_handle(x) = uvhandles[x] = get(uvhandles,x,0)::Int+1
unpreserve_handle(x) = (v = uvhandles[x]::Int; v == 1 ? pop!(uvhandles,x) : (uvhandles[x] = v-1); nothing)

Expand Down
4 changes: 2 additions & 2 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ function require(mod::Symbol)
return root_module(mod)
end

const loaded_modules = ObjectIdDict()
const module_keys = ObjectIdDict()
const loaded_modules = IdDict()
const module_keys = IdDict()

function register_root_module(key, m::Module)
if haskey(loaded_modules, key)
Expand Down
2 changes: 1 addition & 1 deletion base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ get_emin_max() = ccall((:mpfr_get_emin_max, :libmpfr), Clong, ())
set_emax!(x) = ccall((:mpfr_set_emax, :libmpfr), Void, (Clong,), x)
set_emin!(x) = ccall((:mpfr_set_emin, :libmpfr), Void, (Clong,), x)

function Base.deepcopy_internal(x::BigFloat, stackdict::ObjectIdDict)
function Base.deepcopy_internal(x::BigFloat, stackdict::IdDict)
haskey(stackdict, x) && return stackdict[x]
prec = precision(x)
y = BigFloat(zero(Clong), zero(Cint), zero(Clong), C_NULL)
Expand Down
2 changes: 1 addition & 1 deletion base/pkg/resolve/versionweight.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function Base.copy(a::VWPreBuild)
VWPreBuild(a.nonempty, copy(a.w))
end

function Base.deepcopy_internal(a::VWPreBuild, dict::ObjectIdDict)
function Base.deepcopy_internal(a::VWPreBuild, dict::IdDict)
haskey(dict, a) && return dict[a]
b = (a === _vwprebuild_zero) ? _vwprebuild_zero : VWPreBuild(a.nonempty, Base.deepcopy_internal(a.w, dict))
dict[a] = b
Expand Down
2 changes: 1 addition & 1 deletion base/pkg/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ end

==(A::VersionSet, B::VersionSet) = A.intervals == B.intervals
hash(s::VersionSet, h::UInt) = hash(s.intervals, h + (0x2fd2ca6efa023f44 % UInt))
deepcopy_internal(vs::VersionSet, ::ObjectIdDict) = copy(vs)
deepcopy_internal(vs::VersionSet, ::IdDict) = copy(vs)

const Requires = Dict{String,VersionSet}

Expand Down
10 changes: 6 additions & 4 deletions base/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -908,13 +908,14 @@ precompile(Tuple{typeof(Base.setindex!), Base.Dict{Any, Any}, Type{Union{}}, Sym
precompile(Tuple{typeof(Base.Docs.aliasof), Base.Docs.Binding})
precompile(Tuple{typeof(Base.Docs.summarize), Base.Docs.Binding, Type{Union{}}})
precompile(Tuple{typeof(Base.Docs.doc), Base.Docs.Binding, Type{Union{}}})
precompile(Tuple{typeof(Base.haskey), Base.ObjectIdDict, Base.Docs.Binding})
precompile(Tuple{typeof(Base.getindex), Base.ObjectIdDict, Base.Docs.Binding})
precompile(Tuple{typeof(Base.haskey), Base.IdDict, Base.Docs.Binding})
precompile(Tuple{typeof(Base.getindex), Base.IdDict, Base.Docs.Binding})
precompile(Tuple{typeof(Base.push!), Array{Base.Docs.MultiDoc, 1}, Base.Docs.MultiDoc})
precompile(Tuple{typeof(Base.start), Array{Type{T} where T, 1}})
precompile(Tuple{typeof(Base.done), Array{Type{T} where T, 1}, Int64})
precompile(Tuple{typeof(Base.next), Array{Type{T} where T, 1}, Int64})
precompile(Tuple{typeof(Base.getindex), Base.ObjectIdDict, Type{Tuple{Any}}})
precompile(Tuple{typeof(Base.getindex), Base.IdDict, Type{Tuple{Any}}})
precompile(Tuple{typeof(getindex), ObjectIdDict, Type{Tuple{Any}}})
precompile(Tuple{typeof(Base.push!), Array{Base.Docs.DocStr, 1}, Base.Docs.DocStr})
precompile(Tuple{typeof(Base.Docs.formatdoc), Base.GenericIOBuffer{Array{UInt8, 1}}, Base.Docs.DocStr, String})
precompile(Tuple{typeof(Base.Markdown.hashheader), Base.GenericIOBuffer{Array{UInt8, 1}}, Base.Markdown.MD})
Expand Down Expand Up @@ -1431,7 +1432,8 @@ precompile(Tuple{typeof(Base.Filesystem.unlink), String})
precompile(Tuple{typeof(Base.find_all_in_cache_path), Symbol})
precompile(Tuple{typeof(Base.find_package), String})
precompile(Tuple{typeof(Base.find_source_file), String})
precompile(Tuple{typeof(Base.getindex), Base.ObjectIdDict, Symbol})
precompile(Tuple{typeof(getindex), ObjectIdDict, Symbol})
precompile(Tuple{typeof(Base.getindex), Base.IdDict, Symbol})
precompile(Tuple{typeof(Base.getindex), Type{Tuple{String, Float64}}, Tuple{String, Float64}})
precompile(Tuple{typeof(Base.Grisu._show), Base.IOContext{Base.GenericIOBuffer{Array{UInt8, 1}}}, Float64, Int64, Int64, Bool, Bool})
precompile(Tuple{typeof(Base.hash), Tuple{String, Float64}, UInt64})
Expand Down
4 changes: 2 additions & 2 deletions base/serialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export serialize, deserialize, SerializationState
mutable struct SerializationState{I<:IO} <: AbstractSerializer
io::I
counter::Int
table::ObjectIdDict
table::IdDict
pending_refs::Vector{Int}
known_object_data::Dict{UInt64,Any}
SerializationState{I}(io::I) where I<:IO = new(io, 0, ObjectIdDict(), Int[], Dict{UInt64,Any}())
SerializationState{I}(io::I) where I<:IO = new(io, 0, IdDict(), Int[], Dict{UInt64,Any}())
end

SerializationState(io::IO) = SerializationState{typeof(io)}(io)
Expand Down
4 changes: 2 additions & 2 deletions base/summarysize.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

struct SummarySize
seen::ObjectIdDict
seen::IdDict
frontier_x::Vector{Any}
frontier_i::Vector{Int}
exclude::Any
Expand All @@ -24,7 +24,7 @@ function summarysize(obj;
exclude = Union{DataType, TypeName, Method},
chargeall = Union{TypeMapEntry, Core.MethodInstance})
@nospecialize obj exclude chargeall
ss = SummarySize(ObjectIdDict(), Any[], Int[], exclude, chargeall)
ss = SummarySize(IdDict(), Any[], Int[], exclude, chargeall)
size::Int = ss(obj)
while !isempty(ss.frontier_x)
# DFS heap traversal of everything without a specialization
Expand Down
8 changes: 4 additions & 4 deletions base/task.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ task_result(t::Task) = t.result
task_local_storage() = get_task_tls(current_task())
function get_task_tls(t::Task)
if t.storage === nothing
t.storage = ObjectIdDict()
t.storage = IdDict()
end
(t.storage)::ObjectIdDict
(t.storage)::IdDict
end

"""
Expand Down Expand Up @@ -186,7 +186,7 @@ function wait(t::Task)
return task_result(t)
end

suppress_excp_printing(t::Task) = isa(t.storage, ObjectIdDict) ? get(get_task_tls(t), :SUPPRESS_EXCEPTION_PRINTING, false) : false
suppress_excp_printing(t::Task) = isa(t.storage, IdDict) ? get(get_task_tls(t), :SUPPRESS_EXCEPTION_PRINTING, false) : false

function register_taskdone_hook(t::Task, hook)
tls = get_task_tls(t)
Expand All @@ -213,7 +213,7 @@ function task_done_hook(t::Task)
end

# Execute any other hooks registered in the TLS
if isa(t.storage, ObjectIdDict) && haskey(t.storage, :TASKDONE_HOOKS)
if isa(t.storage, IdDict) && haskey(t.storage, :TASKDONE_HOOKS)
foreach(hook -> hook(t), t.storage[:TASKDONE_HOOKS])
delete!(t.storage, :TASKDONE_HOOKS)
handled = true
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Distributed/src/cluster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ const LPROC = LocalProcess()
const HDR_VERSION_LEN=16
const HDR_COOKIE_LEN=16
const map_pid_wrkr = Dict{Int, Union{Worker, LocalProcess}}()
const map_sock_wrkr = ObjectIdDict()
const map_sock_wrkr = IdDict()
const map_del_wrkr = Set{Int}()

# cluster management related API
Expand Down
5 changes: 2 additions & 3 deletions stdlib/Distributed/src/clusterserialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Base.Serializer: lookup_object_number, remember_object
mutable struct ClusterSerializer{I<:IO} <: AbstractSerializer
io::I
counter::Int
table::ObjectIdDict
table::IdDict
pending_refs::Vector{Int}

pid::Int # Worker we are connected to.
Expand All @@ -20,7 +20,7 @@ mutable struct ClusterSerializer{I<:IO} <: AbstractSerializer
anonfunc_id::UInt64

function ClusterSerializer{I}(io::I) where I<:IO
new(io, 0, ObjectIdDict(), Int[], worker_id_from_socket(io),
new(io, 0, IdDict(), Int[], worker_id_from_socket(io),
Set{UInt64}(), Dict{UInt64, UInt64}(), Dict{UInt64, Vector{Symbol}}(), 0)
end
end
Expand Down Expand Up @@ -250,4 +250,3 @@ clear!(syms, pid::Int; mod=Main) = clear!(syms, [pid]; mod=mod)

clear_impl!(syms, mod::Module) = foreach(x->clear_impl!(x,mod), syms)
clear_impl!(sym::Symbol, mod::Module) = isdefined(mod, sym) && @eval(mod, global $sym = nothing)

2 changes: 1 addition & 1 deletion stdlib/SharedArrays/src/SharedArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ function convert(::Type{SharedArray{TS,N}}, A::Array{TA,N}) where {TS,TA,N}
copyto!(S, A)
end

function deepcopy_internal(S::SharedArray, stackdict::ObjectIdDict)
function deepcopy_internal(S::SharedArray, stackdict::IdDict)
haskey(stackdict, S) && return stackdict[S]
R = SharedArray{eltype(S),ndims(S)}(size(S); pids = S.pids)
copyto!(sdata(R), sdata(S))
Expand Down
Loading

0 comments on commit ac95324

Please sign in to comment.