Skip to content

Commit

Permalink
dedups: prefer maps:fold/3 to maps:filter/2
Browse files Browse the repository at this point in the history
  • Loading branch information
the-mikedavis committed Sep 19, 2024
1 parent 97040aa commit 006c194
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/khepri_machine.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1545,10 +1545,18 @@ drop_expired_dedups(
{State, Result, SideEffects},
#{system_time := Timestamp}) ->
Dedups = get_dedups(State),
Dedups1 = maps:filter(
fun(_CommandRef, {_Reply, Expiry}) ->
Expiry >= Timestamp
end, Dedups),
%% This would look cleaner written with `maps:filter/2' but it turns out
%% that function is very inefficient.
%% TODO: explain why.
Dedups1 = maps:fold(
fun(CommandRef, {_Reply, Expiry}, Acc) ->
case Expiry >= Timestamp of
true ->
maps:remove(CommandRef, Acc);
false ->
Acc
end
end, Dedups, Dedups),
State1 = set_dedups(State, Dedups1),
{State1, Result, SideEffects}.

Expand Down

0 comments on commit 006c194

Please sign in to comment.