Optimize open dict check.

A dict is open and with keys if its size is greater than 0. Go through
the dicts one by one with lists:any/2 and use that to optimize the
check. Ought to run faster than traversing the full set.
This commit is contained in:
Jesper Louis Andersen 2012-01-08 00:01:18 +01:00
parent 60af669400
commit 793f8e3a3a

View file

@ -265,14 +265,9 @@ cleanup_tree(Tree) ->
%% Various Helper routines %% Various Helper routines
%% ---------------------------------------------------------------------- %% ----------------------------------------------------------------------
%% @todo optimize this call. You can fast-exit as soon as you know
%% there is a non-empty dict.
open_dicts_with_keys(#state { open = Open}) -> open_dicts_with_keys(#state { open = Open}) ->
keysum_of_dicts(Open) > 0. lists:any(fun({_, D}) -> dict:size(D) > 0 end,
dict:to_list(Open)).
keysum_of_dicts(DictOfDict) ->
Dicts = [ V || {_, V} <- dict:to_list(DictOfDict)],
lists:sum([dict:size(D) || D <- Dicts]).
open_dicts(#state { open = Open}) -> open_dicts(#state { open = Open}) ->
dict:size(Open) > 0. dict:size(Open) > 0.