[erlang-questions] cost of passing a gen_server state between functions

Pablo Platt pablo.platt@REDACTED
Mon Aug 9 03:10:43 CEST 2010


Thank you Hynek and Robert.

The reason I asked this question is because I have one gen_server that handles 
several sessions for the same user.
I have several session types for different types of connections: socket, http...
When the gen_server receive a message it passes it to an handler along with the 
gen_server's state.
Each session type defines a different handler (module).
The handler process the message and reply with a new state.

Most of the operations on the state are setting a new value in a record, reading 
values and adding new items to lists.
So from your explanation I understand that there is no copy. The state is passed 
by reference 

and that as long as I append new values to the head of lists in the state I'm 
safe.

Simplified example:

% In the gen_server:
handle_cast({message, Message, SessionType}, State) ->
    NewState = case SessionType of
        socket ->
            socket_session:handle_message(State, Message);
        other_type ->
            other_type_session:handle_message(State, Message);
        _ ->
            State
    end,
    {noreply, NewState}.

% inside the socket session module:
handle_message(State, Message) ->
    do_something_with_message(Message),
    Log = State#state.log,
    NewLog = [Message | Log],
    NewState = State#state{log=NewLog},
    NewState.




________________________________
From: Robert Virding <rvirding@REDACTED>
To: Pablo Platt <pablo.platt@REDACTED>
Cc: erlang-questions@REDACTED
Sent: Mon, August 9, 2010 3:53:33 AM
Subject: Re: [erlang-questions] cost of passing a gen_server state between 
functions

The cost is negligible, literally the cost of one function call. A
call to a function in another module costs a little more, *very*
little more. This is irrespective of the size of the gen_server state.
You should do that which results in the cleanest code, it will be a
Big Win in the future.

Robert

P.S. What is the cleanest code is, of course, very subjective. I, for
one, tend to avoid very short, one line, functions. This not because
of efficiency but because too many small functions tend to clutter up
the code. I think.

On 8 August 2010 03:21, Pablo Platt <pablo.platt@REDACTED> wrote:
> Hi,
>
> Is there a difference in manipulating the state of a gen_server inside a 
single
> function
> and passing the state to another function that manipulate it?
> Is there a difference if the othe function is in another module?
>
> %The state is been manipulated in the handl_cast function:
> handle_cast({something, I}, State1) ->
>    OldList = State1#state.l,
>    NewList = [I | OldList],
>    State2 = State#state1{l=NewList},
>    {noreply, State2}.
>
> %handle_cast pass the state to another function that changes it:
> handle_cast({something, I}, State) ->
>    State2 = do_something(I, State),
>    {noreply, State2}.
>
> do_something(I, State) ->
>    State2 = State#state1{l=NewList},
>    State2.
>
> Thanks
>
>
>
>



      


More information about the erlang-questions mailing list