Skip to content

Commit bb67ff2

Browse files
committed
faster and more compact serialization of symbols and strings
1 parent a90fd40 commit bb67ff2

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

base/io.jl

+5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ function write(s::IO, p::Ptr, n::Integer)
9494
n
9595
end
9696

97+
function write(io::IO, s::Symbol)
98+
pname = convert(Ptr{Uint8}, s)
99+
write(io, pname, int(ccall(:strlen, Csize_t, (Ptr{Uint8},), pname)))
100+
end
101+
97102
# all subtypes should implement this
98103
read(s::IO, x::Type{Uint8}) = error(typeof(s)," does not support byte I/O")
99104

base/serialize.jl

+12-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
## serializing values ##
22

33
# dummy types to tell number of bytes used to store length (4 or 1)
4-
abstract LongSymbol
54
abstract LongTuple
65
abstract LongExpr
76
abstract UndefRefTag
@@ -14,11 +13,11 @@ let i = 2
1413
for t = {Symbol, Int8, Uint8, Int16, Uint16, Int32, Uint32,
1514
Int64, Uint64, Int128, Uint128, Float32, Float64, Char, Ptr,
1615
DataType, UnionType, Function,
17-
Tuple, Array, Expr, LongSymbol, LongTuple, LongExpr,
16+
Tuple, Array, Expr, :reserved21, LongTuple, LongExpr,
1817
LineNumberNode, SymbolNode, LabelNode, GotoNode,
1918
QuoteNode, TopNode, TypeVar, Box, LambdaStaticData,
20-
Module, UndefRefTag, Task, :reserved4,
21-
:reserved5, :reserved6, :reserved7, :reserved8,
19+
Module, UndefRefTag, Task, ASCIIString, UTF8String,
20+
:reserved6, :reserved7, :reserved8,
2221
:reserved9, :reserved10, :reserved11, :reserved12,
2322

2423
(), Bool, Any, :Any, None, Top, Undef, Type,
@@ -31,7 +30,7 @@ let i = 2
3130
:mul_float, :unbox, :box,
3231
:eq_int, :slt_int, :sle_int, :ne_int,
3332
:arrayset, :arrayref,
34-
:reserved13, :reserved14, :reserved15, :reserved16,
33+
:Core, :Base, :reserved15, :reserved16,
3534
:reserved17, :reserved18, :reserved19, :reserved20,
3635
false, true, nothing, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
3736
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
@@ -79,16 +78,9 @@ function serialize(s, x::Symbol)
7978
if haskey(ser_tag, x)
8079
return write_as_tag(s, x)
8180
end
82-
name = string(x)
83-
ln = sizeof(name)
84-
if ln <= 255
85-
writetag(s, Symbol)
86-
write(s, uint8(ln))
87-
else
88-
writetag(s, LongSymbol)
89-
write(s, int32(ln))
90-
end
91-
write(s, name)
81+
writetag(s, Symbol)
82+
write(s, x)
83+
write(s, 0x00)
9284
end
9385

9486
function serialize_array_data(s, a)
@@ -323,8 +315,11 @@ end
323315

324316
deserialize_tuple(s, len) = ntuple(len, i->deserialize(s))
325317

326-
deserialize(s, ::Type{Symbol}) = symbol(read(s, Uint8, int32(read(s, Uint8))))
327-
deserialize(s, ::Type{LongSymbol}) = symbol(read(s, Uint8, read(s, Int32)))
318+
function deserialize(s, ::Type{Symbol})
319+
r = readuntil(s,0x00)
320+
pop!(r)
321+
symbol(r)
322+
end
328323

329324
function deserialize(s, ::Type{Module})
330325
path = deserialize(s)

base/show.jl

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11

22
show(x) = show(STDOUT::IO, x)
33

4-
function print(io::IO, s::Symbol)
5-
pname = convert(Ptr{Uint8}, s)
6-
write(io, pname, int(ccall(:strlen, Csize_t, (Ptr{Uint8},), pname)))
7-
end
4+
print(io::IO, s::Symbol) = (write(io,s);nothing)
85

96
function show(io::IO, x::ANY)
107
t = typeof(x)::DataType

0 commit comments

Comments
 (0)