From 006c194d00d048a873beccbf3d7c01950a6f22ab Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Thu, 19 Sep 2024 11:10:07 -0400 Subject: [PATCH] dedups: prefer maps:fold/3 to maps:filter/2 --- src/khepri_machine.erl | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/khepri_machine.erl b/src/khepri_machine.erl index 5e016159..72f500c7 100644 --- a/src/khepri_machine.erl +++ b/src/khepri_machine.erl @@ -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}.