Commits

Fred T-H committed 4eb7a62

Changed history accumulator from list to queue

Switching from the list to the queue avoids useless traversals.
On a history list of 10 max, this gives a boost of about 3% in
speed in the stress benchmark. It is expected that longer
history would be even longer when dropping events without the
queue module.

Comments (0)

Files changed (1)

src/chut_user_history_handler.erl

 %% a limit (how many messages need to be kept in history).
 %%--------------------------------------------------------------------
 init({UserId, Limit}) ->
-    {ok, {UserId, Limit, []}}.
+    {ok, {UserId, Limit, queue:new()}}.
 
 %%--------------------------------------------------------------------
 %% Function:
     Event = {sent, To, Msg},
     {ok, {UserId, Limit, add_to_history(Event, History, Limit)}};
 handle_event({From, history}, S={UserId, _, History}) ->
-    From ! {UserId, {history, lists:reverse(History)}},
+    From ! {UserId, {history, queue:to_list(History)}},
     {ok, S};
 handle_event(_Event, State) ->
     {ok, State}.
 %%-------------------------------------------------------------------
 add_to_history(_, _, 0) -> [];
 add_to_history(Event, History, Limit) ->
-    case length(History) of
-        X when X >= Limit-1 ->
-            [Event|lists:sublist(History,Limit-1)];
+    case queue:len(History) of
+        Limit ->
+            queue:in(Event, queue:drop(History));
         _ ->
-            [Event|History]
+            queue:in(Event, History)
     end.