Skip to content

Commit 1bd2334

Browse files
JeffBezansonKristofferC
authored andcommitted
speed up (de)serialization of Base bits types in abstract containers (#30221)
helps #30148 (cherry picked from commit 11c5680)
1 parent 9f7b4d1 commit 1bd2334

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

stdlib/Serialization/src/Serialization.jl

+47-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function sertag(@nospecialize(v))
9090
end
9191
return Int32(-1)
9292
end
93-
desertag(i::Int32) = TAGS[i]
93+
desertag(i::Int32) = @inbounds(TAGS[i])
9494

9595
# tags >= this just represent themselves, their whole representation is 1 byte
9696
const VALUE_TAGS = sertag(())
@@ -102,6 +102,7 @@ const EMPTYTUPLE_TAG = sertag(())
102102
const TUPLE_TAG = sertag(Tuple)
103103
const SIMPLEVECTOR_TAG = sertag(SimpleVector)
104104
const SYMBOL_TAG = sertag(Symbol)
105+
const INT8_TAG = sertag(Int8)
105106
const ARRAY_TAG = sertag(Array)
106107
const EXPR_TAG = sertag(Expr)
107108
const MODULE_TAG = sertag(Module)
@@ -337,7 +338,7 @@ end
337338

338339
function serialize_mod_names(s::AbstractSerializer, m::Module)
339340
p = parentmodule(m)
340-
if p === m
341+
if p === m || m === Base
341342
key = Base.root_module_key(m)
342343
serialize(s, key.uuid === nothing ? nothing : key.uuid.value)
343344
serialize(s, Symbol(key.name))
@@ -586,6 +587,13 @@ function serialize(s::AbstractSerializer, n::Int64)
586587
nothing
587588
end
588589

590+
for i in 0:13
591+
tag = Int32(INT8_TAG + i)
592+
ty = TAGS[tag]
593+
(ty === Int32 || ty === Int64) && continue
594+
@eval serialize(s::AbstractSerializer, n::$ty) = (writetag(s.io, $tag); write(s.io, n); nothing)
595+
end
596+
589597
serialize(s::AbstractSerializer, ::Type{Bottom}) = write_as_tag(s.io, BOTTOM_TAG)
590598

591599
function serialize(s::AbstractSerializer, u::UnionAll)
@@ -748,6 +756,9 @@ function handle_deserialize(s::AbstractSerializer, b::Int32)
748756
return unwrap_unionall(tname.wrapper)
749757
elseif b == OBJECT_TAG
750758
t = deserialize(s)
759+
if t === Missing
760+
return missing
761+
end
751762
return deserialize(s, t)
752763
elseif b == REF_OBJECT_TAG
753764
slot = s.counter; s.counter += 1
@@ -788,8 +799,36 @@ function handle_deserialize(s::AbstractSerializer, b::Int32)
788799
read(s.io, UInt8)
789800
end
790801
return deserialize(s)
802+
elseif b == INT8_TAG
803+
return read(s.io, Int8)
804+
elseif b == INT8_TAG+1
805+
return read(s.io, UInt8)
806+
elseif b == INT8_TAG+2
807+
return read(s.io, Int16)
808+
elseif b == INT8_TAG+3
809+
return read(s.io, UInt16)
810+
elseif b == INT32_TAG
811+
return read(s.io, Int32)
812+
elseif b == INT8_TAG+5
813+
return read(s.io, UInt32)
814+
elseif b == INT64_TAG
815+
return read(s.io, Int64)
816+
elseif b == INT8_TAG+7
817+
return read(s.io, UInt64)
818+
elseif b == INT8_TAG+8
819+
return read(s.io, Int128)
820+
elseif b == INT8_TAG+9
821+
return read(s.io, UInt128)
822+
elseif b == INT8_TAG+10
823+
return read(s.io, Float16)
824+
elseif b == INT8_TAG+11
825+
return read(s.io, Float32)
826+
elseif b == INT8_TAG+12
827+
return read(s.io, Float64)
828+
elseif b == INT8_TAG+13
829+
return read(s.io, Char)
791830
end
792-
t = desertag(b)
831+
t = desertag(b)::DataType
793832
if t.mutable && length(t.types) > 0 # manual specialization of fieldcount
794833
slot = s.counter; s.counter += 1
795834
push!(s.pending_refs, slot)
@@ -949,6 +988,11 @@ function deserialize_array(s::AbstractSerializer)
949988
A = Array{elty, length(dims)}(undef, dims)
950989
s.table[slot] = A
951990
sizehint!(s.table, s.counter + div(length(A),4))
991+
deserialize_fillarray!(A, s)
992+
return A
993+
end
994+
995+
function deserialize_fillarray!(A::Array{T}, s::AbstractSerializer) where {T}
952996
for i = eachindex(A)
953997
tag = Int32(read(s.io, UInt8)::UInt8)
954998
if tag != UNDEFREF_TAG

0 commit comments

Comments
 (0)