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

Robert Virding <>
Mon Aug 9 02:53:33 CEST 2010


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 <> 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