Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/swc_node.erl
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,22 @@ add(BVV, {Id, Counter}) ->
Entry = maps:get(Id, BVV, {0,0}),
maps:put(Id, add_aux(Entry, Counter), BVV).

%% Maximum bitmap shift, kept under OTP's bignum system_limit (~4 Mbit).
%% Above this gap we advance the base directly instead of building a giant
%% bitmap, which would crash with {system_limit, ...} on `bsl`.
-define(MAX_BSL_BITS, 3000000).

%% @doc Adds a dot to a BVV entry, returning the normalized entry.
-spec add_aux(entry(), counter()) -> entry().
add_aux({N,B}, M) ->
case N < M of
false -> norm(N, B);
true -> M2 = B bor (1 bsl (M-N-1)),
norm(N, M2)
true ->
Gap = M - N - 1,
case Gap > ?MAX_BSL_BITS of
true -> {M, 0};
false -> norm(N, B bor (1 bsl Gap))
end
end.

%% @doc Merges all entries from the two BVVs.
Expand Down Expand Up @@ -253,7 +262,8 @@ add_aux_test() ->
?assertEqual( add_aux({5,3}, 4), {7,0} ),
?assertEqual( add_aux({2,5}, 4), {5,0} ),
?assertEqual( add_aux({2,5}, 6), {3,6} ),
?assertEqual( add_aux({2,4}, 6), {2,12} ).
?assertEqual( add_aux({2,4}, 6), {2,12} ),
?assertEqual( add_aux({0,0}, 5000000), {5000000, 0} ).

merge_test() ->
?assertEqual( merge( #{"a" => {5,3}} , #{"a" => {2,4}} ), #{"a" => {7,0}} ),
Expand Down