Skip to content

Commit 9aacf5e

Browse files
committed
Merge pull request #11060 from JuliaLang/jcb/modserialize
Modularize serialization code
2 parents d318efd + 85aa337 commit 9aacf5e

12 files changed

+82
-74
lines changed

base/Enums.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ macro enum(T,syms...)
9191
immutable $(esc(T)) <: Enum
9292
val::$enumT
9393
function $(esc(typename))(x::Integer)
94-
$(membershiptest(:x, values)) || enum_argument_error($(Meta.quot(typename)), x)
94+
$(membershiptest(:x, values)) || enum_argument_error($(Expr(:quote, typename)), x)
9595
new(x)
9696
end
9797
end
@@ -100,7 +100,7 @@ macro enum(T,syms...)
100100
Base.length{E<:$(esc(typename))}(x::Type{E}) = $(length(vals))
101101
Base.next{E<:$(esc(typename))}(x::Type{E},s) = (E($values[s]),s+1)
102102
Base.done{E<:$(esc(typename))}(x::Type{E},s) = s > $(length(values))
103-
Base.names{E<:$(esc(typename))}(x::Type{E}) = [$(map(x->Meta.quot(x[1]), vals)...)]
103+
Base.names{E<:$(esc(typename))}(x::Type{E}) = [$(map(x->Expr(:quote, (x[1])), vals)...)]
104104
Base.isless{E<:$(esc(typename))}(x::E, y::E) = isless(x.val, y.val)
105105
function Base.print{E<:$(esc(typename))}(io::IO,x::E)
106106
for (sym, i) in $vals

base/dict.jl

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -404,26 +404,6 @@ function convert{K,V}(::Type{Dict{K,V}},d::Associative)
404404
end
405405
convert{K,V}(::Type{Dict{K,V}},d::Dict{K,V}) = d
406406

407-
function serialize(s, t::Dict)
408-
serialize_type(s, typeof(t))
409-
write(s, Int32(length(t)))
410-
for (k,v) in t
411-
serialize(s, k)
412-
serialize(s, v)
413-
end
414-
end
415-
416-
function deserialize{K,V}(s, T::Type{Dict{K,V}})
417-
n = read(s, Int32)
418-
t = T(); sizehint!(t, n)
419-
for i = 1:n
420-
k = deserialize(s)
421-
v = deserialize(s)
422-
t[k] = v
423-
end
424-
return t
425-
end
426-
427407
hashindex(key, sz) = ((hash(key)%Int) & (sz-1)) + 1
428408

429409
isslotempty(h::Dict, i::Int) = h.slots[i] == 0x0

base/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export
1414
LinAlg,
1515
BLAS,
1616
LAPACK,
17+
Serializer,
1718
SparseMatrix,
1819
Docs,
1920
Markdown,

base/gmp.jl

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import Base: *, +, -, /, <, <<, >>, >>>, <=, ==, >, >=, ^, (~), (&), (|), ($),
66
binomial, cmp, convert, div, divrem, factorial, fld, gcd, gcdx, lcm, mod,
77
ndigits, promote_rule, rem, show, isqrt, string, isprime, powermod,
88
sum, trailing_zeros, trailing_ones, count_ones, base, tryparse_internal,
9-
serialize, deserialize, bin, oct, dec, hex, isequal, invmod,
10-
prevpow2, nextpow2, ndigits0z, widen, signed
9+
bin, oct, dec, hex, isequal, invmod, prevpow2, nextpow2, ndigits0z, widen, signed
1110

1211
if Clong == Int32
1312
typealias ClongMax Union(Int8, Int16, Int32)
@@ -224,15 +223,6 @@ convert(::Type{Float16}, n::BigInt) = Float16(n,RoundNearest)
224223

225224
promote_rule{T<:Integer}(::Type{BigInt}, ::Type{T}) = BigInt
226225

227-
# serialization
228-
229-
function serialize(s, n::BigInt)
230-
Base.serialize_type(s, BigInt)
231-
serialize(s, base(62,n))
232-
end
233-
234-
deserialize(s, ::Type{BigInt}) = get(tryparse_internal(BigInt, deserialize(s), 62, true))
235-
236226
# Binary ops
237227
for (fJ, fC) in ((:+, :add), (:-,:sub), (:*, :mul),
238228
(:fld, :fdiv_q), (:div, :tdiv_q), (:mod, :fdiv_r), (:rem, :tdiv_r),

base/mpfr.jl

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ import
1818
gamma, lgamma, digamma, erf, erfc, zeta, eta, log1p, airyai,
1919
eps, signbit, sin, cos, tan, sec, csc, cot, acos, asin, atan,
2020
cosh, sinh, tanh, sech, csch, coth, acosh, asinh, atanh, atan2,
21-
serialize, deserialize, cbrt, typemax, typemin, unsafe_trunc,
22-
realmin, realmax, get_rounding, set_rounding, maxintfloat, widen,
23-
significand, frexp, tryparse
21+
cbrt, typemax, typemin, unsafe_trunc, realmin, realmax, get_rounding,
22+
set_rounding, maxintfloat, widen, significand, frexp, tryparse
2423

2524
import Base.Rounding: get_rounding_raw, set_rounding_raw
2625

@@ -197,15 +196,6 @@ function convert(::Type{Rational{BigInt}}, x::FloatingPoint)
197196
BigInt(ldexp(x,s)) // (BigInt(1) << s)
198197
end
199198

200-
# serialization
201-
202-
function serialize(s, n::BigFloat)
203-
Base.serialize_type(s, BigFloat)
204-
serialize(s, string(n))
205-
end
206-
207-
deserialize(s, ::Type{BigFloat}) = BigFloat(deserialize(s))
208-
209199
# Basic arithmetic without promotion
210200
for (fJ, fC) in ((:+,:add), (:*,:mul))
211201
@eval begin

base/regex.jl

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,3 @@ function eachmatch(re::Regex, str::AbstractString, ovr::Bool=false)
244244
end
245245

246246
eachmatch(re::Regex, str::AbstractString) = RegexMatchIterator(re,str)
247-
248-
# Don't serialize the pointers
249-
function serialize(s, r::Regex)
250-
serialize_type(s, typeof(r))
251-
serialize(s, r.pattern)
252-
serialize(s, r.options)
253-
end
254-
255-
function deserialize(s, t::Type{Regex})
256-
pattern = deserialize(s)
257-
options = deserialize(s)
258-
Regex(pattern, options)
259-
end

base/serialize.jl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
module Serializer
2+
3+
import Base: GMP, Bottom, svec, unsafe_convert, uncompressed_ast
4+
5+
export serialize, deserialize
6+
17
## serializing values ##
28

39
# dummy types to tell number of bytes used to store length (4 or 1)
@@ -158,6 +164,29 @@ function serialize{T,N,A<:Array}(s, a::SubArray{T,N,A})
158164
serialize_array_data(s, a)
159165
end
160166

167+
function serialize{T<:AbstractString}(s, ss::SubString{T})
168+
# avoid saving a copy of the parent string, keeping the type of ss
169+
invoke(serialize, (Any,Any), s, convert(SubString{T}, convert(T,ss)))
170+
end
171+
172+
# Don't serialize the pointers
173+
function serialize(s, r::Regex)
174+
Serializer.serialize_type(s, typeof(r))
175+
serialize(s, r.pattern)
176+
serialize(s, r.options)
177+
end
178+
179+
function serialize(s, n::BigInt)
180+
Serializer.serialize_type(s, BigInt)
181+
serialize(s, base(62,n))
182+
end
183+
184+
185+
function serialize(s, n::BigFloat)
186+
Serializer.serialize_type(s, BigFloat)
187+
serialize(s, string(n))
188+
end
189+
161190
function serialize(s, e::Expr)
162191
l = length(e.args)
163192
if l <= 255
@@ -174,6 +203,26 @@ function serialize(s, e::Expr)
174203
end
175204
end
176205

206+
function serialize(s, t::Dict)
207+
serialize_type(s, typeof(t))
208+
write(s, Int32(length(t)))
209+
for (k,v) in t
210+
serialize(s, k)
211+
serialize(s, v)
212+
end
213+
end
214+
215+
function deserialize{K,V}(s, T::Type{Dict{K,V}})
216+
n = read(s, Int32)
217+
t = T(); sizehint!(t, n)
218+
for i = 1:n
219+
k = deserialize(s)
220+
v = deserialize(s)
221+
t[k] = v
222+
end
223+
return t
224+
end
225+
177226
function serialize_mod_names(s, m::Module)
178227
p = module_parent(m)
179228
if m !== p
@@ -572,3 +621,18 @@ function deserialize(s, t::DataType)
572621
return x
573622
end
574623
end
624+
625+
deserialize(s, ::Type{BigFloat}) = BigFloat(deserialize(s))
626+
627+
deserialize(s, ::Type{BigInt}) = get(GMP.tryparse_internal(BigInt, deserialize(s), 62, true))
628+
629+
deserialize(s, ::Type{BigInt}) = get(GMP.tryparse_internal(BigInt, deserialize(s), 62, true))
630+
631+
function deserialize(s, t::Type{Regex})
632+
pattern = deserialize(s)
633+
options = deserialize(s)
634+
Regex(pattern, options)
635+
end
636+
637+
638+
end

base/sharedarray.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ end
188188
# Don't serialize s (it is the complete array) and
189189
# pidx, which is relevant to the current process only
190190
function serialize(s, S::SharedArray)
191-
serialize_type(s, typeof(S))
191+
Serializer.serialize_type(s, typeof(S))
192192
for n in SharedArray.name.names
193193
if n in [:s, :pidx, :loc_subarr_1d]
194-
writetag(s, UndefRefTag)
194+
Serializer.writetag(s, Serializer.UndefRefTag)
195195
else
196196
serialize(s, getfield(S, n))
197197
end

base/string.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -634,11 +634,6 @@ convert{T<:AbstractString}(::Type{SubString{T}}, s::T) = SubString(s, 1, endof(s
634634

635635
bytestring{T <: ByteString}(p::SubString{T}) = bytestring(pointer(p.string.data)+p.offset, nextind(p, p.endof)-1)
636636

637-
function serialize{T}(s, ss::SubString{T})
638-
# avoid saving a copy of the parent string, keeping the type of ss
639-
invoke(serialize, (Any,Any), s, convert(SubString{T}, convert(T,ss)))
640-
end
641-
642637
function getindex(s::AbstractString, r::UnitRange{Int})
643638
if first(r) < 1 || endof(s) < last(r)
644639
throw(BoundsError(s, r))

base/sysimg.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ importall .Enums
202202

203203
# concurrency and parallelism
204204
include("serialize.jl")
205+
importall .Serializer
205206
include("multi.jl")
206207
include("managers.jl")
207208

test/remote.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Check that serializer hasn't gone out-of-frame
2-
@test Base.ser_tag[Symbol] == 2
3-
@test Base.ser_tag[()] == 47
4-
@test Base.ser_tag[false] == 123
2+
@test Serializer.ser_tag[Symbol] == 2
3+
@test Serializer.ser_tag[()] == 47
4+
@test Serializer.ser_tag[false] == 123
55

66
# issue #1770
77
let

test/serialize.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ end
88

99
# Tags
1010
create_serialization_stream() do s
11-
Base.writetag(s, Bool)
12-
@test takebuf_array(s)[end] == UInt8(Base.ser_tag[Bool])
11+
Serializer.writetag(s, Bool)
12+
@test takebuf_array(s)[end] == UInt8(Serializer.ser_tag[Bool])
1313
end
1414

1515
create_serialization_stream() do s
16-
Base.write_as_tag(s, Bool)
17-
@test takebuf_array(s)[end] == UInt8(Base.ser_tag[Bool])
16+
Serializer.write_as_tag(s, Bool)
17+
@test takebuf_array(s)[end] == UInt8(Serializer.ser_tag[Bool])
1818
end
1919

2020
create_serialization_stream() do s
21-
Base.write_as_tag(s, Symbol)
21+
Serializer.write_as_tag(s, Symbol)
2222
data = takebuf_array(s)
2323
@test data[end-1] == 0x00
24-
@test data[end] == UInt8(Base.ser_tag[Symbol])
24+
@test data[end] == UInt8(Serializer.ser_tag[Symbol])
2525
end
2626

2727
# Boolean & Empty & Nothing

0 commit comments

Comments
 (0)