Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: serialize pointer- and padding-free objects in one write #14678

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions base/serialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the pointer returned here may be to a box that is not gc-rooted. in general, pointer_from_objref should never be called on something that is immutable and pointerfree (I think we should make it an error)

i'm looking into possible alternatives that would provide the same benefit, but doesn't require calling pointer_from_objref

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
Expand Down Expand Up @@ -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))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is regrettable but there are no read! methods that accept a pointer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've thought they should be unsafe_read! and implemented consistently instead of the current ad-hoc implementations

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me.

read!(s.io, a_)
return x
elseif isbits(t)
if nf == 1
return ccall(:jl_new_struct, Any, (Any,Any...), t, deserialize(s))
Expand Down
5 changes: 5 additions & 0 deletions src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down