From dbec1a3dfa5e4ea4d136eff32fdd2b6795e5720c Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Thu, 14 Jan 2016 18:16:46 -0500 Subject: [PATCH] serialize pointer- and padding-free objects in one write fixes #14106 --- base/serialize.jl | 19 ++++++++++++++----- src/alloc.c | 5 +++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/base/serialize.jl b/base/serialize.jl index 54922d2e86803..0803f7184e12a 100644 --- a/base/serialize.jl +++ b/base/serialize.jl @@ -420,11 +420,15 @@ function serialize_any(s::SerializationState, x) else t.mutable && serialize_cycle(s, x) && return serialize_type(s, t) - for i in 1:nf - if isdefined(x, i) - serialize(s, getfield(x, i)) - else - writetag(s.io, UNDEFREF_TAG) + if t.pointerfree && ccall(:jl_datatype_haspadding, Cint, (Any,), t)==0 + write(s.io, pointer_from_objref(x), sizeof(x)) + else + for i in 1:nf + if isdefined(x, i) + serialize(s, getfield(x, i)) + else + writetag(s.io, UNDEFREF_TAG) + end end end end @@ -674,6 +678,11 @@ function deserialize(s::SerializationState, t::DataType) end if nf == 0 return ccall(:jl_new_struct, Any, (Any,Any...), t) + elseif t.pointerfree && ccall(:jl_datatype_haspadding, Cint, (Any,), t)==0 + x = ccall(:jl_new_struct_uninit, Any, (Any,), t) + a_ = pointer_to_array(convert(Ptr{UInt8},pointer_from_objref(x)), sizeof(t)) + read!(s.io, a_) + return x elseif isbits(t) if nf == 1 return ccall(:jl_new_struct, Any, (Any,Any...), t, deserialize(s)) diff --git a/src/alloc.c b/src/alloc.c index 7ae8d2ebdc1b8..c523740d01b2a 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -237,6 +237,11 @@ JL_DLLEXPORT int jl_field_isdefined(jl_value_t *v, size_t i) return 1; } +JL_DLLEXPORT int jl_datatype_haspadding(jl_datatype_t *dt) +{ + return dt->haspadding; +} + JL_DLLEXPORT jl_value_t *jl_new_struct(jl_datatype_t *type, ...) { if (type->instance != NULL) return type->instance;