From b699b8a46efcf2df395bb19dc1027726ce08a9cb Mon Sep 17 00:00:00 2001 From: Siraaj Khandkar Date: Fri, 27 Jul 2012 23:01:28 -0400 Subject: [PATCH] Stashing early msgs instead of immediately resending. HUGE performance difference! ~50% less CPU utilization for ~16k cells. --- 001/src/life_cell.erl | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/001/src/life_cell.erl b/001/src/life_cell.erl index e7cebcd..2ebdf7b 100644 --- a/001/src/life_cell.erl +++ b/001/src/life_cell.erl @@ -23,6 +23,7 @@ ,num_neighbors :: integer() ,replies_pending :: integer() ,gen_id :: integer() + ,early_msgs :: list() }). @@ -49,6 +50,7 @@ init([{CellID, Name, NeighborNames}]) -> ,num_neighbors = length(NeighborNames) ,live_neighbors = 0 ,replies_pending = 0 + ,early_msgs = [] }, {ok, State}. @@ -66,22 +68,39 @@ handle_call(_Msg, _From, State) -> handle_cast({next_gen, GenID}, - #state{cell_state=CellState + #state{name=Name + ,cell_state=CellState ,neighbors=Neighbors ,num_neighbors=NumNeighbors + ,early_msgs=EarlyMsgs }=State) -> ok = cast_all(Neighbors, {state_broadcast, GenID, CellState}), - {noreply, State#state{replies_pending=NumNeighbors, gen_id=GenID}}; + + % Put stashed messages back in the mailbox, + % now that we're ready to handle them + ok = cast_to(Name, EarlyMsgs), + + NewState = State#state{replies_pending=NumNeighbors + ,gen_id=GenID + ,early_msgs=[] + }, + + {noreply, NewState}; -%% If we receive 'state_broadcast' before we receive 'next_gen', throw it back -%% in the queue. (Took me a while to realize this, but sometimes it is -%% possible. The more there're cells, the more likely this is to happen.) +%% If we receive 'state_broadcast' before we receive 'next_gen', +%% stash it until we do. +%% +%% Took me a while to realize this, but sometimes it is possible. The more +%% there're cells, the more likely this is to happen. +%% handle_cast({state_broadcast, ReceivedGenID, _NeighborState}=Msg, - #state{gen_id=GenID, name=Name}=State) when GenID =/= ReceivedGenID-> - ok = gen_server:cast(Name, Msg), - {noreply, State}; + #state{gen_id=GenID + ,early_msgs=EarlyMsgs + }=State) when GenID =/= ReceivedGenID -> + + {noreply, State#state{early_msgs=[Msg|EarlyMsgs]}}; %% Now that we can be sure that this request is for the current generation, we @@ -128,6 +147,14 @@ handle_info(_Msg, State) -> %% Internal %% ============================================================================ +% Cast different messages to a single destination +cast_to(_, []) -> ok; +cast_to(Server, [Msg | Msgs]) -> + ok = gen_server:cast(Server, Msg), + cast_to(Server, Msgs). + + +% Cast the same message to multiple destinations cast_all([], _) -> ok; cast_all([Server | Servers], Msg) -> ok = gen_server:cast(Server, Msg), -- 2.20.1