Efficiency: registered process or loop-function argument?

Jérémie Lumbroso jeremie@REDACTED
Mon Mar 27 20:31:19 CEST 2006


Hello,

I have server that spawns a new 'handler' process/function for every
incoming socket.

Those 'handler' process must communicate with the server process. I am
wondering which of the two following solutions is best.

1. Should the server process register itself with the server atom, as in:

start() ->
    [...]
    register(server, spawn(test, server_loop, [LSock])).

server_loop(LSock) ->
    case gen_tcp:accept(LSock, 1000) of
        {ok, Socket} ->
            [...]
            Client_Pid = spawn(?MODULE, relay, [Socket]),
            gen_tcp:controlling_process(Socket, Client_Pid),
            server_loop(LSock);

        [...]
    end.

relay(Socket) ->
    receive
        {tcp, Socket, Bin} ->
            Data = binary_to_list(Bin),
            server ! {self(), Data},
            ?MODULE:relay(Socket);

        [...]
    end.


2. Should I pass the server's pid as an argument to the client loop, such
as:

start() ->
    [...]
    spawn(test, server_loop, [LSock]).

server_loop(LSock) ->
    case gen_tcp:accept(LSock, 1000) of
        {ok, Socket} ->
            [...]
            Client_Pid = spawn(?MODULE, relay, [Socket, self()]),
            gen_tcp:controlling_process(Socket, Client_Pid),
            server_loop(LSock);

        [...]
    end.

relay(Socket, Server) ->
    receive
        {tcp, Socket, Bin} ->
            Data = binary_to_list(Bin),
            Server ! {self(), Data},
            ?MODULE:relay(Socket, Server);

        [...]
    end.


Or rather, I realize this question is on a per-case basis mostly ... so
really, what I would like to ask, is whether there are any side-effects to
doing it the second way (by passing an argument). Is the overhead for an
extra argument on a loop function like that big?

Thanks,

Jérémie Lumbroso
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20060327/6f71459e/attachment.htm>


More information about the erlang-questions mailing list