Skip to content

Commit 0b70736

Browse files
author
Daniil Fedotov
committed
Move clients and cref_to_msg_ids from dicts to maps
Maps have better performance and readability then dicts.
1 parent 8dc4a18 commit 0b70736

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/rabbit_msg_store.erl

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ client_update_flying(Diff, MsgId, #client_msstate { flying_ets = FlyingEts,
703703

704704
clear_client(CRef, State = #msstate { cref_to_msg_ids = CTM,
705705
dying_clients = DyingClients }) ->
706-
State #msstate { cref_to_msg_ids = dict:erase(CRef, CTM),
706+
State #msstate { cref_to_msg_ids = maps:remove(CRef, CTM),
707707
dying_clients = maps:remove(CRef, DyingClients) }.
708708

709709

@@ -740,7 +740,7 @@ init([Type, BaseDir, ClientRefs, StartupFunState]) ->
740740
{CleanShutdown, IndexState, ClientRefs1} =
741741
recover_index_and_client_refs(IndexModule, FileSummaryRecovered,
742742
ClientRefs, Dir, Name),
743-
Clients = dict:from_list(
743+
Clients = maps:from_list(
744744
[{CRef, {undefined, undefined, undefined}} ||
745745
CRef <- ClientRefs1]),
746746
%% CleanShutdown => msg location index and file_summary both
@@ -790,7 +790,7 @@ init([Type, BaseDir, ClientRefs, StartupFunState]) ->
790790
clients = Clients,
791791
successfully_recovered = CleanShutdown,
792792
file_size_limit = FileSizeLimit,
793-
cref_to_msg_ids = dict:new(),
793+
cref_to_msg_ids = #{},
794794
credit_disc_bound = CreditDiscBound
795795
},
796796
%% If we didn't recover the msg location index then we need to
@@ -843,7 +843,7 @@ handle_call({new_client_state, CRef, CPid, MsgOnDiskFun, CloseFDsFun}, _From,
843843
flying_ets = FlyingEts,
844844
clients = Clients,
845845
gc_pid = GCPid }) ->
846-
Clients1 = dict:store(CRef, {CPid, MsgOnDiskFun, CloseFDsFun}, Clients),
846+
Clients1 = maps:put(CRef, {CPid, MsgOnDiskFun, CloseFDsFun}, Clients),
847847
erlang:monitor(process, CPid),
848848
reply({IndexState, IndexModule, Dir, GCPid, FileHandlesEts, FileSummaryEts,
849849
CurFileCacheEts, FlyingEts},
@@ -874,15 +874,15 @@ handle_cast({client_dying, CRef},
874874

875875
handle_cast({client_delete, CRef},
876876
State = #msstate { clients = Clients }) ->
877-
State1 = State #msstate { clients = dict:erase(CRef, Clients) },
877+
State1 = State #msstate { clients = maps:remove(CRef, Clients) },
878878
noreply(clear_client(CRef, State1));
879879

880880
handle_cast({write, CRef, MsgId, Flow},
881881
State = #msstate { cur_file_cache_ets = CurFileCacheEts,
882882
clients = Clients,
883883
credit_disc_bound = CreditDiscBound }) ->
884884
case Flow of
885-
flow -> {CPid, _, _} = dict:fetch(CRef, Clients),
885+
flow -> {CPid, _, _} = maps:get(CRef, Clients),
886886
%% We are going to process a message sent by the
887887
%% rabbit_amqqueue_process. Now we are accessing the
888888
%% msg_store process dictionary.
@@ -1003,7 +1003,7 @@ terminate(_Reason, State = #msstate { index_state = IndexState,
10031003
[true = ets:delete(T) || T <- [FileSummaryEts, FileHandlesEts,
10041004
CurFileCacheEts, FlyingEts]],
10051005
IndexModule:terminate(IndexState),
1006-
case store_recovery_terms([{client_refs, dict:fetch_keys(Clients)},
1006+
case store_recovery_terms([{client_refs, maps:keys(Clients)},
10071007
{index_module, IndexModule}], Dir) of
10081008
ok ->
10091009
rabbit_log:info("Message store for directory '~s' is stopped", [Dir]),
@@ -1035,12 +1035,12 @@ reply(Reply, State) ->
10351035

10361036
next_state(State = #msstate { sync_timer_ref = undefined,
10371037
cref_to_msg_ids = CTM }) ->
1038-
case dict:size(CTM) of
1038+
case maps:size(CTM) of
10391039
0 -> {State, hibernate};
10401040
_ -> {start_sync_timer(State), 0}
10411041
end;
10421042
next_state(State = #msstate { cref_to_msg_ids = CTM }) ->
1043-
case dict:size(CTM) of
1043+
case maps:size(CTM) of
10441044
0 -> {stop_sync_timer(State), hibernate};
10451045
_ -> {State, 0}
10461046
end.
@@ -1055,7 +1055,7 @@ stop_sync_timer(State) ->
10551055
internal_sync(State = #msstate { current_file_handle = CurHdl,
10561056
cref_to_msg_ids = CTM }) ->
10571057
State1 = stop_sync_timer(State),
1058-
CGs = dict:fold(fun (CRef, MsgIds, NS) ->
1058+
CGs = maps:fold(fun (CRef, MsgIds, NS) ->
10591059
case gb_sets:is_empty(MsgIds) of
10601060
true -> NS;
10611061
false -> [{CRef, MsgIds} | NS]
@@ -1327,7 +1327,7 @@ orddict_store(Key, Val, Dict) ->
13271327
update_pending_confirms(Fun, CRef,
13281328
State = #msstate { clients = Clients,
13291329
cref_to_msg_ids = CTM }) ->
1330-
case dict:fetch(CRef, Clients) of
1330+
case maps:get(CRef, Clients) of
13311331
{_CPid, undefined, _CloseFDsFun} -> State;
13321332
{_CPid, MsgOnDiskFun, _CloseFDsFun} -> CTM1 = Fun(MsgOnDiskFun, CTM),
13331333
State #msstate {
@@ -1337,21 +1337,22 @@ update_pending_confirms(Fun, CRef,
13371337
record_pending_confirm(CRef, MsgId, State) ->
13381338
update_pending_confirms(
13391339
fun (_MsgOnDiskFun, CTM) ->
1340-
dict:update(CRef, fun (MsgIds) -> gb_sets:add(MsgId, MsgIds) end,
1341-
gb_sets:singleton(MsgId), CTM)
1340+
maps:update_with(CRef,
1341+
fun (MsgIds) -> gb_sets:add(MsgId, MsgIds) end,
1342+
gb_sets:singleton(MsgId), CTM)
13421343
end, CRef, State).
13431344

13441345
client_confirm(CRef, MsgIds, ActionTaken, State) ->
13451346
update_pending_confirms(
13461347
fun (MsgOnDiskFun, CTM) ->
1347-
case dict:find(CRef, CTM) of
1348+
case maps:find(CRef, CTM) of
13481349
{ok, Gs} -> MsgOnDiskFun(gb_sets:intersection(Gs, MsgIds),
13491350
ActionTaken),
13501351
MsgIds1 = rabbit_misc:gb_sets_difference(
13511352
Gs, MsgIds),
13521353
case gb_sets:is_empty(MsgIds1) of
1353-
true -> dict:erase(CRef, CTM);
1354-
false -> dict:store(CRef, MsgIds1, CTM)
1354+
true -> maps:remove(CRef, CTM);
1355+
false -> maps:put(CRef, MsgIds1, CTM)
13551356
end;
13561357
error -> CTM
13571358
end
@@ -1419,7 +1420,7 @@ mark_handle_to_close(ClientRefs, FileHandlesEts, File, Invoke) ->
14191420
[ begin
14201421
case (ets:update_element(FileHandlesEts, Key, {2, close})
14211422
andalso Invoke) of
1422-
true -> case dict:fetch(Ref, ClientRefs) of
1423+
true -> case maps:get(Ref, ClientRefs) of
14231424
{_CPid, _MsgOnDiskFun, undefined} ->
14241425
ok;
14251426
{_CPid, _MsgOnDiskFun, CloseFDsFun} ->

0 commit comments

Comments
 (0)