Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 2 files changed
  • 0 comments
  • 2 contributors
Sep 27, 2012
Erik Søe Sørensen Bloom filter: make "don't set if already set" logic more fine-grained.
This results in a minor "bloom add" performance gain of ~10%.
e1fddd6
Kresten Krab Thorup Merge pull request #18 from eriksoe/bloom-sparse-representation
Bloom filter: make "don't set if already set" logic more fine-grained.
e3689c3
14  src/hanoidb_bloom.erl
@@ -150,10 +150,7 @@ masked_pair(Mask, X, Y) -> {X band Mask, Y band Mask}.
150 150
 
151 151
 all_set(_Mask, _I1, _I, []) -> true;
152 152
 all_set(Mask, I1, I, [H|T]) ->
153  
-    case bitmask_get(I, H) of
154  
-        true -> all_set(Mask, I1, (I+I1) band Mask, T);
155  
-        false -> false
156  
-    end.
  153
+    bitmask_get(I, H) andalso all_set(Mask, I1, (I+I1) band Mask, T).
157 154
 
158 155
 %% Adds element to set
159 156
 %%
@@ -177,10 +174,7 @@ add(Elem, #sbf{size=Size, r=R, s=S, b=[H|T]=Bs}=Sbf) ->
177 174
 hash_add(Hashes, #bloom{mb=Mb, a=A, size=Size} = B) ->
178 175
     Mask = 1 bsl Mb -1,
179 176
     {I1, I0} = make_indexes(Mask, Hashes),
180  
-    case all_set(Mask, I1, I0, A) of
181  
-        true -> B;
182  
-        false -> B#bloom{size=Size+1, a=set_bits(Mask, I1, I0, A, [])}
183  
-    end.
  177
+    B#bloom{size=Size+1, a=set_bits(Mask, I1, I0, A, [])}.
184 178
 
185 179
 set_bits(_Mask, _I1, _I, [], Acc) -> lists:reverse(Acc);
186 180
 set_bits(Mask, I1, I, [H|T], Acc) ->
@@ -236,7 +230,9 @@ bitarray_set(I, A1) ->
236 230
     AI = I div ?W,
237 231
     V = array:get(AI, A),
238 232
     V1 = V bor (1 bsl (I rem ?W)),
239  
-    array:set(AI, V1, A).
  233
+    if V =:= V1 -> A; % The bit is already set
  234
+       true -> array:set(AI, V1, A)
  235
+    end.
240 236
 
241 237
 -spec bitarray_get( non_neg_integer(), array() ) -> boolean().
242 238
 bitarray_get(I, A) ->
6  src/hanoidb_dense_bitmap.erl
@@ -18,7 +18,11 @@ set(I, {dense_bitmap_ets, _,_, Tab}=DBM) ->
18 18
     BitInCell = I rem ?BITS_PER_CELL,
19 19
     Old = ets:lookup_element(Tab, ?REPR_NAME, Cell),
20 20
     New = Old bor (1 bsl BitInCell),
21  
-    ets:update_element(Tab, ?REPR_NAME, {Cell,New}),
  21
+    if New =:= Old ->
  22
+            ok; % The bit is already set
  23
+       true ->
  24
+            ets:update_element(Tab, ?REPR_NAME, {Cell,New})
  25
+    end,
22 26
     DBM.
23 27
 
24 28
 build({dense_bitmap_ets, _, _, Tab}) ->

No commit comments for this range

Something went wrong with that request. Please try again.