[erlang-questions] message ring test (memory allocation question)

Allen McPherson mcpherson@REDACTED
Tue Mar 4 16:54:04 CET 2008


-- Re-post.  Hope I don't generate a duplicate post but I just signed  
up.


Hello,

First post here from a new Erlang programmer.  As an exercise I
have written the message ring benchmark that Joe talked about in
his book:

-module(cleanring).
-compile(export_all).

start(N, Msg) ->
     StartPid = launch(self(), N),
     StartPid ! {Msg, self(), 0},
     receive
	{MsgRecvd, _FromPid, _Hop} -> done
     end,
     StartPid ! die,
     MsgRecvd.

launch(Pid, N) ->
     FromPid = spawn(fun() -> loop(Pid) end),
     if
	N =:= 1 -> FromPid;
	true    -> launch(FromPid, N-1)
     end.

loop(ToPid) ->
     receive
	{Term, _FromPid, Hop} ->
	    ToPid ! {Term, self(), Hop+1},
	    loop(ToPid);
	die ->
	    ToPid ! die
     end.


If I run as follows, it dies in malloc:

1> c(cleanring).
2> L = lists:seq(1,100000).
3> cleanring:start(32000, L).

If I make L a binary (in a restarted erl shell) it runs fine (and too  
fast to believe):

1> c(cleanring).
2> L = lists:seq(1,100000).
3> LL = term_to_binary(L).
4> cleanring:start(32000, LL).

Is the runtime somehow just passing around a pointer to LL by virtue
of it being a binary?  I'm not sure why it doesn't crash as the first
run did.

Also, if you spot anything really wrong with the program I'd  
appreciate a
heads up.  I'm new to Erlang and am sometimes not sure if the code I  
write
is in the "right mindset" (functional programming-wise).

Thanks
--
Al




More information about the erlang-questions mailing list