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

simpler and faster message (de)serialize #21543

Merged
merged 1 commit into from Apr 27, 2017
Merged

simpler and faster message (de)serialize #21543

merged 1 commit into from Apr 27, 2017

Conversation

JeffBezanson
Copy link
Sponsor Member

@JeffBezanson JeffBezanson commented Apr 25, 2017

This fixes a couple things about message serialization.

  1. We were adding a method to serialize whose output could only be consumed by deserialize_msg, so renamed to serialize_msg.
  2. Some calls to eval were added here as part of the automatic recompilation of dependent functions #265 fix, but they don't seem necessary to me. I don't think anything adds new message types or methods to these functions, but correct me if I'm wrong. Now uses invokelatest.
  3. Make the code for serialize_msg and deserialize_msg more specialized and efficient.

Benchmark:

addprocs(2)
x = [bitrand(4,2) for i = 1:10000];

Before:

julia> @time pmap(identity, x);
  1.306307 seconds (1.76 M allocations: 70.141 MiB, 1.64% gc time)

after:

julia> @time pmap(identity, x);
  1.050650 seconds (1.14 M allocations: 41.453 MiB, 1.20% gc time)

@JeffBezanson JeffBezanson added parallel Parallel or distributed computation performance Must go faster labels Apr 25, 2017
end
end

function deserialize_msg(s::AbstractSerializer)
idx = read(s.io, UInt8)
t = msgtypes[idx]
return eval(current_module(), Expr(:body, Expr(:return, Expr(:call, deserialize_msg, QuoteNode(s), QuoteNode(t)))))
if idx == 1
Copy link
Contributor

Choose a reason for hiding this comment

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

A comment that idx is the position of the type into the msgtypes array would be useful.

elseif idx == 9
return CallMsg{:call_fetch}(deserialize(s), deserialize(s), deserialize(s))
end
assert(false)
Copy link
Contributor

Choose a reason for hiding this comment

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

This definitely looks uglier, hence a comment stating the reason why it is being done - probably the PR description itself - would be nice.

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

I could generate this code automatically, but since there are only 9 cases I wasn't sure it would actually be easier to read. I can try it that way.

@@ -183,7 +190,7 @@ function send_msg_(w::Worker, header, msg, now::Bool)
try
reset_state(w.w_serializer)
serialize_hdr_raw(io, header)
eval(current_module(), Expr(:body, Expr(:return, Expr(:call, serialize, QuoteNode(w.w_serializer), QuoteNode(msg))))) # io is wrapped in w_serializer
serialize_msg(w.w_serializer, msg) # io is wrapped in w_serializer
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

?

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

Can you be more specific?

end
end

function deserialize_msg(s::AbstractSerializer)
idx = read(s.io, UInt8)
t = msgtypes[idx]
return eval(current_module(), Expr(:body, Expr(:return, Expr(:call, deserialize_msg, QuoteNode(s), QuoteNode(t)))))
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

the outer "deserialize_msg" function will now need to be called with this "invokelatest"-style wrapper in order for (de)serialize to work (for custom added serialization functions after the event loop is initialized)

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

I see, the issue is custom serialize methods added after starting workers. We need a way to do this without allocating a bunch of objects and calling eval.

@JeffBezanson
Copy link
Sponsor Member Author

Still working on this, and it will have to wait for #19784, but so far I have the pmap benchmark above down to allocating 41 MB.

also separate `serialize_msg` from `serialize`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parallel Parallel or distributed computation performance Must go faster
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants