Skip to content

Commit

Permalink
Flatten message types
Browse files Browse the repository at this point in the history
This Reduces allocations a tiny bit.
  • Loading branch information
savq committed Sep 23, 2022
1 parent e72897d commit 3347b79
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/Malt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,22 @@ end

_new_call_msg(send_result::Bool, f::Function, args...; kwargs...) = (
header = :call,
body = (f=f, args=args, kwargs=kwargs),
f=f,
args=args,
kwargs=kwargs,
send_result = send_result,
)

_new_do_msg(f::Function, args...; kwargs...) = (
header = :remote_do,
body = (f=f, args=args, kwargs=kwargs),
f=f,
args=args,
kwargs=kwargs,
)

_new_channel_msg(expr) = (
header = :channel,
body = expr,
expr = expr,
)

function _send_msg(port::UInt16, msg)
Expand Down
9 changes: 3 additions & 6 deletions src/worker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ end


function handle(::Val{:call}, socket, msg)
body = msg.body # Don't use destructuring to support v1.6
try
result = body.f(body.args...; body.kwargs...)
result = msg.f(msg.args...; msg.kwargs...)
# @debug("Result", result)
serialize(socket, (status=:ok, result=(msg.send_result ? result : nothing)))
catch e
Expand All @@ -65,17 +64,15 @@ function handle(::Val{:call}, socket, msg)
end

function handle(::Val{:remote_do}, socket, msg)
body = msg.body
try
# @debug("Remote do:", body)
body.f(body.args...; body.kwargs...)
msg.f(msg.args...; msg.kwargs...)
finally
close(socket)
end
end

function handle(::Val{:channel}, socket, msg)
channel = eval(msg.body)
channel = eval(msg.expr)
while isopen(channel) && isopen(socket)
serialize(socket, take!(channel))
end
Expand Down

0 comments on commit 3347b79

Please sign in to comment.