Open
Description
Run
using Sockets, Distributed, Serialization
function srvr()
ls = listen(10000)
while true
as = accept(ls)
@async begin
while true
msg=deserialize(as)
if isa(msg, Function)
msg();
serialize(as, ()->1)
else
serialize(as, msg)
end
end
end
end
end
srvr()
in one process.
In a REPL
using Sockets, Distributed, Serialization
function testc_data(n)
c = connect("localhost", 10000)
@time for i in 1:n
msg = serialize(c, 1)
resp = deserialize(c)
@assert resp == 1
end
end
function testc_func(n)
c = connect("localhost", 10000)
@time for i in 1:n
msg = serialize(c, ()->1)
resp = deserialize(c)
@assert resp() == 1
end
end
Results
julia> testc_data(1000)
40.453 milliseconds (22002 allocations: 1298 KB, 4.31% gc time)
julia> testc_func(1000)
743.730 milliseconds (734 k allocations: 24448 KB, 0.19% gc time)
julia> testc_data(1000)
36.762 milliseconds (22001 allocations: 1298 KB)
julia> testc_func(1000)
741.483 milliseconds (741 k allocations: 24614 KB, 0.19% gc time)
Since the underlying model is function execution, as opposed to data messaging, just optimizing this will speed up parallel code that does a lot of remote calls.
@carnaval , originally the server code was not within a function. It is still slower but not as bad.